Windows script delete specific subfolders inside a path - windows

I need your help for writing a little batch script in order to delete specific named folders inside a path.
Let's image we have multiple folders called "pippo" inside the path tree "C:\Users\myUser\Desktop\StartFolder"
How can I write a script that browses all the folder tree of that path and delete all fsubolders called "pippo" ?
Thanks!
I found this command in other site
for /d /r "%d" %d in (_svn) do #if exist "%d" rmdir "%d"
So I tried to adapt it to my target, but it doesn't work.
for /d /r "C:\Users\myUser\Desktop\StartFolder" "pippo" in (_svn) do #if exist "pippo" rmdir "pippo"

It's unclear to me what the _svn is meant for in your tries.
( or is _svn
your real pippo ?)
for /r "C:\Users\myUser\Desktop\StartFolder" /d %%A in (pippo
) do if exist "%%~fA" echo rmdir "%%~fA"
If the output looks OK, remove the echo in front of rmdir
Sample output on my test tree:
> for /r "q:\Test\2018" /D %A in (05) do #if exist "%~fA" #echo rd "%~fA"
rd "q:\Test\2018\05"
rd "q:\Test\2018\04\05"
rd "q:\Test\2018\05\05"

Related

How do I drive through folders recursively in Windows Batch 'FOR /D /R' including root without '\.'?

Is there a way to recursively drive through folders in Windows Batch, and include the root folder you specify?
For example if I do:
FOR /D /R "C:\Users\Mark\Desktop" %%a in (*) do echo %%a
Results in:
C:\Users\Mark\Desktop\Folder1
C:\Users\Mark\Desktop\Folder2
C:\Users\Mark\Desktop\Folder2\SubFolder1
C:\Users\Mark\Desktop\Folder3
But no
C:\Users\Mark\Desktop
If I do:
FOR /D /R "C:\Users\Mark\Desktop" %%a in (.) do echo %%a
Results in:
C:\Users\Mark\Desktop\.
C:\Users\Mark\Desktop\Folder1\.
C:\Users\Mark\Desktop\Folder2\.
C:\Users\Mark\Desktop\Folder2\SubFolder1\.
C:\Users\Mark\Desktop\Folder3\.
Which kinda works, but I don't want the .\ in there.
Is there a way to get root folder and all subfolders to display like this:
C:\Users\Mark\Desktop
C:\Users\Mark\Desktop\Folder1
C:\Users\Mark\Desktop\Folder2
C:\Users\Mark\Desktop\Folder2\Subfolder1
C:\Users\Mark\Desktop\Folder3
I'm sure it's something simple but I haven't figured it out. Thanks.
try this one:
for /D /R "c:\users\jason\desktop" %%I in (.) do echo %%~fI >> out.txt

How to delete an intermediate directory in windows using cmd?

For example to go from this:
directory1/
101/online/img1.png
102/online/img2.png
103/online/img3.png
To this:
directory1/
101/img1.png
102/img2.png
103/img3.png
In this example, 101 (and 102, 103...) directories contain only a single directory online, which is always of the same name. However, online directory may contain multiple files inside, but no directories.
I am looking for an automated way to manipulate my files like that, as I have a big list of such cases. I am looking for a Windows Command Prompt or Powershell solution.
New answer and updated
This from the command line:
pushd directory1
for /D %D in (*) do move /Y %D\online\*.* %D && rmdir /s /q %D\online
popd
Or this from a cmd file:
pushd directory1
for /D %%D in (*) do move /Y %%D\online\*.* %%D && rmdir /s /q %%D\online
popd

Deleting Contents of Multiple Directories, Only if They Exist (Batch File)

I'm trying to write a simple batch file that will clean up disk space. I have to delete the entire contents (folders and files) of 4 different directories, but only if they exist. I've been testing trying to delete one, but I know nothing about writing batch files. After all the research I've done, I came up with a couple lines of code that doesn't work.
#echo off
IF EXIST "C:\Windows\TestFolder\TestSubFolder\*.*"
DEL /s /q "C:\Windows\TestFolder\TestSubFolder\*.*"
for /d %%p in ("C:\Windows\TestFolder\TestSubFolder\*.*") do rmdir "%%p" /s /q
exit
In this scenario, I need to be able to delete the contents of TestSubFolder, if TestSubFolder exists. Whether it exists or not, after that action is complete, I need the code to do the same thing to a TestSubFolder2.
Thanks
The main problem in your code is the improper usage of the if command. If there is only one command to execute if the condition is true, it can be written in the same line, but to write the command in the next line you need to use parenthesis. It should be something like
IF EXIST "C:\Windows\TestFolder\TestSubFolder\*.*" (
DEL /s /q "C:\Windows\TestFolder\TestSubFolder\*.*"
for /d %%p in ("C:\Windows\TestFolder\TestSubFolder\*.*") do rmdir "%%p" /s /q
)
But this can be simplified as
2>nul pushd "C:\Windows\TestFolder\TestSubFolder" && (
rmdir . /s /q
popd
)
That is, we try to change to the indicated folder (pushd) and if there was not any problem (conditional execution operator && means execute next command if the previous one did not fail) them remove all the contents of the folder (rmdir) and return to the previous active directory (popd). The 2>nul is just hidding any error message (ex. the folder does not exist, locked files that can not be removed, ...)
Now, if the process has to be repeated for more than one folder, we can use the for command to iterate over the list of the folders
for %%a in ( "folder1" "folder2" ) do ....
Placing the previous code into this for loop we have
#echo off
setlocal enableextensions disabledelayedexpansion
2>nul (
for %%a in (
"C:\Windows\TestFolder\TestSubFolder"
"C:\Windows\TestFolder\TestSubFolder2"
) do pushd "%%~fa" && (
rmdir . /s /q
popd
)
)
The error hidding has been moved to cover all the for execution, and now, for each of the folders (referenced by the for replaceable parameter %%a), we try to change to the folder using the full path (%%~fa) and if we can change to it, then remove all the folder contents before returning to the original active directory.
CD "C:\Windows\TestFolder\TestSubFolder"
RD /s /q "C:\Windows\TestFolder\TestSubFolder"
Works for me.
or from anywhere
RD /s /q "C:\Windows\TestFolder\TestSubFolder"
MD "C:\Windows\TestFolder\TestSubFolder"

Making files writable recursively

I am trying to make every file writable under certain folders using this
for /d /r . %%d in (nbproject) do #if exist "%%d" dir "%%d" /S | attrib -R /S /D "%%d"
when inside a folder
dir "%%d" /S | attrib -R
works perfectly fine. But when runing from a batch script, it is unable to do things right. Please advise.
Use nested loops like this: Just change the folder names to the folders of your choosing.
#echo off
for /D /R %%D in (nbproject Folder2 Folder3) do if exist "%%~fD" (
pushd "%%~fD"
for /R %%F in (*) do if exist "%%~fF" attrib -R "%%~fF" /D
popd
)
One liner:
for /D /R %%D in (nbproject Folder2 Folder3) do #if exist "%%~fD" #cd /D "%%~fD" && for /R %%F in (*) do #if exist "%%~fF" attrib -R "%%~fF" /D
Let us also break down your first line of code for reasons where it can fail.
You are piping the output of dir into attrib and using the d variable. Only one is needed. Use the & symbol if using the d variable with attrib else remove the d variable.
Use the /b option with the dir command, especially when piping its output.
There is no need for the . in the for command. The current directory is assumed when not specified.
When quoting a loop variable use the tilde ~ to remove an existing surrounding quotations so that double quotes does not occur.
The /S option is not needed with attrib when using it in dir.
Example:
for /d /r %%d in (nbproject) do #if exist "%%~d" dir "%%~d" /B /S | attrib -R /D

Delete all files and folders in a directory

I want to have a batch file that will delete all the folders and files in my cache folder for my wireless toolkit.
Currently I have the following:
cd "C:\Users\tbrollo\j2mewtk\2.5.2\appdb\RMS"
del *.db
This will delete all .db files in my RMS directory, however I want to delete every single thing from this directory. How can I do this?
Use:
Create a batch file
Copy the below text into the batch file
set folder="C:\test"
cd /d %folder%
for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)
It will delete all files and folders.
del *.* instead of del *.db. That will remove everything.
IF EXIST "C:\Users\tbrollo\j2mewtk\2.5.2\appdb\RMS" (
rmdir "C:\Users\tbrollo\j2mewtk\2.5.2\appdb\RMS" /s /q
)
This will delete everything from the folder (and the folder itself).
I just put this together from what morty346 posted:
set folder="C:\test"
IF EXIST "%folder%" (
cd /d %folder%
for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)
)
It adds a quick check that the folder defined in the variable exists first, changes directory to the folder, and deletes the contents.
del *.* will only delete files, but not subdirectories. To nuke the contents of a directory, you can use this script:
#echo off
setlocal enableextensions
if {%1}=={} goto :HELP
if {%1}=={/?} goto :HELP
goto :START
:HELP
echo Usage: %~n0 directory-name
echo.
echo Empties the contents of the specified directory,
echo WITHOUT CONFIRMATION. USE EXTREME CAUTION!
goto :DONE
:START
pushd %1 || goto :DONE
rd /q /s . 2> NUL
popd
:DONE
endlocal
The pushd changes into the directory of which you want to delete the children. Then when rd asks to delete the current directory and all sub directories, the deletion of the sub directories succeed, but the deletion of the current directory fails - because we are in it. This produces an error which 2> NUL swallows. (2 being the error stream).
You can do this using del and the /S flag (to tell it to recurse all files from all subdirectories):
del /S C:\Path\to\directory\*
The RD command can also be used. Recursively delete quietly without a prompt:
#RD /S /Q %VAR_PATH%
Rmdir (rd)
set "DIR_TO_DELETE=your_path_to_the_folder"
IF EXIST %DIR_TO_DELETE% (
FOR /D %%p IN ("%DIR_TO_DELETE%\*.*") DO rmdir "%%p" /S /Q
del %DIR_TO_DELETE%\*.* /F /Q
)
Use
set dir="Your Folder Path Here"
rmdir /s %dir%
mkdir %dir%
This version deletes without asking:
set dir="Your Folder Here"
rmdir /s /q %dir%
mkdir %dir%
Example:
set dir="C:\foo1\foo\foo\foo3"
rmdir /s /q %dir%
mkdir %dir%
This will clear C:\foo1\foo\foo\foo3.
(I would like to mention Abdullah Sabouin's answer. There was a mix up about me copying him. I did not notice his post. I would like to thank you melpomene for pointing out errors!)
Try the following; it works for me.
I have an application which dumps data in my "C:\tmp" folder, and the following works the best for me. It doesn't even ask Yes or No to delete the data. I have made a schedule for it to run after every 5 minutes
cd "C:\tmp"
del *.* /Q
Better yet, let's say I want to remove everything under the C:\windows\temp folder.
#echo off
rd C:\windows\temp /s /q
You could use robocopy to mirror an empty folder to the folder you are clearing.
robocopy "C:\temp\empty" "C:\temp\target" /E /MIR
It also works if you can't remove or recreate the actual folder.
It does require an existing empty directory.
I would like to suggest using simple tool like cleardir. So, in batch file you can write:
cleardir path/to/dir
And you'll get empty directory dir. A bit slow, but still resolves the "problem".
I'm an author of the tool =)
The easiest way is:
Create *.txt file
Write:
rmdir /q /s . dir
Save file as *.bat in folder which you want to clear (you can call the file NUKE.bat)
Turn it on
WARNING!
THIS DELETES EVERYTHING IN THE FOLDER WHERE IT IS WITHOUT ASKING FOR CONFIRMATION!!!
SO CHOOSE WISELY PLACE FOR SAVING IT.
Easy simple answer :
C:
del /S /Q C:\folderName\otherFolderName\*
C: Important in case you have to switch from D: to C: or C: to D: (or anything else)
/S Recursive, all subfolders are deleted along
/Q If you don't activate quiet mode, prompt will ask you to type y for every subfolders... you don't want that
Be carful, it's drastic.
You cannot delete everything with either rmdir or del alone:
rmdir /s /q does not accept wildcard params. So rmdir /s /q * will error.
del /s /f /q will delete all files, but empty subdirectories will remain.
My preferred solution (as I have used in many other batch files) is:
rmdir /s /q . 2>NUL
Just a modified version of GregM's answer:
set folder="C:\test"
cd /D %folder%
if NOT %errorlevel% == 0 (exit /b 1)
echo Entire content of %cd% will be deleted. Press Ctrl-C to abort
pause
REM First the directories /ad option of dir
for /F "delims=" %%i in ('dir /b /ad') do (echo rmdir "%%i" /s/q)
REM Now the files /a-d option of dir
for /F "delims=" %%i in ('dir /b /a-d') do (echo del "%%i" /q)
REM To deactivate simulation mode remove the word 'echo' before 'rmdir' and 'del'.
#echo off
#color 0A
echo Deleting logs
rmdir /S/Q c:\log\
ping 1.1.1.1 -n 5 -w 1000 > nul
echo Adding log folder back
md c:\log\
You was on the right track. Just add code to add the folder which is deleted back again.

Resources