I am trying to write a batch that deletes multiple directories/folders in a directory
I am using
for /D %f in (*) do rmdir %f /s /q
Now this works and deletes folder names that dont contain a space.
I recieve this message after running the above command for the folders that do.
d:\test\testworld\rmdir remove me /s /q
The system cannot find the file specified.
The system cannot find the file specified.
Most of the folders contain spaces. Is there anyway i could change the command to include folders with spaces?
use quotes.
rmdir remove me /s /q
fails, but
rmdir "remove me" /s /q
works.
The change to your command line would be for /D %f in (*) do rmdir "%f" /s /q
You can use " double quotation mark like rmdir "remove me" /s /q
Related
I have a directory as such:
D:\DATA\DATA2\Demo&_1\Demo\1\deployment
D:\DATA\DATA2\Demo&_1\Demo\2\deployment
D:\DATA\DATA2\Demo&_1\Demo\3\deployment
And I am running the below batch file command to delete all deployment folders present inside Demo folder:
cd /d D:\DATA\DATA2\Demo&_1\Demo
FOR /d /r . %%d IN (deployment) DO #IF EXIST "%%d" rd /s /q "%%d"
The above command is not working(not deleting deployment folder) when I have special character in in folder name (Demo&_1) and when I have do not have special then it is working(deleting deployment folder).
For e.g. if I change name of folder "Demo&_1" to "Demo1" the above batch file will work fine but not work if have special character.
Simply because the paths are not in quotes, but regardless here is a much simpler solution without having to do cd /d
for /D /R "D:\DATA\DATA2\Demo&_1\Demo" %%d IN (deployment) DO #IF EXIST "%%d" rd /s /q "%%d"
How can I remotely delete subfolders older than x days? Was able to find a syntax like this, but it deletes all of the subfolders in xFOLDER. Any idea how I can modify the syntax and delete only subfolders in xFOLDER older than 7 days?
FOR /D %i IN (\\IP ADDRESS\FOLDERA\FOLDERB\FOLDERC\FOLDERD\xFOLDER\*) DO RD /S /Q "%i"
DEL /Q \\IP ADDRESS\FOLDERA\FOLDERB\FOLDERC\FOLDERD\xFOLDER\*.*
I manage to delete directory and subdirectory using this command
RD remove directory
/S use to remove directory tree
/Q quiet mode
forfiles /D -7 /C "cmd /c rd /s /q #path"
change the code accordingly to your needs.
I have a directory as such:
D:\Movies
D:\Movies\MovieTitle1\backdrops\
D:\Movies\MovieTitle2\backdrops\
D:\Movies\MovieTitle3\backdrops\
D:\Movies\MovieTitle4\backdrops\
How could I have a batch file delete all folders named "Backdrops"? I would prefer it to run recursive from just the D:\ drive if possible.
Short answer:
FOR /d /r . %%d IN (backdrops) DO #IF EXIST "%%d" rd /s /q "%%d"
I got my answer from one of the countless answers to the same question on Stack Overflow:
Command line tool to delete folder with a specified name recursively in Windows?
This command is not tested, but I do trust this site enough to post this answer.
As suggested by Alex in a comment, this batch script should be foolproof:
D:
FOR /d /r . %%d IN (backdrops) DO #IF EXIST "%%d" rd /s /q "%%d"
Above answer didn't quite work for me. I had to use a combination of #itd solution and #Groo comment. Kudos to them.
Final solution for me was (using the backdrop folder example):
FOR /d /r . %%d IN ("backdrops") DO #IF EXIST "%%d" rd /s /q "%%d"
I will open a different answer, because it would be too cramped in the comments. It was asked what to do, if you want to execute from/to a different folder and I want to give an example for non-recursive deletion.
First of all, when you use the command in cmd, you have to use %d, but when you use it in a .bat, you have to use %%d.
You can use a wildcard to just process folders that for example start with "backdrops": "backdrops*".
Recursive deletion of folders starting in the folder the .bat is in:
FOR /d /r . %d IN ("backdrops") DO #IF EXIST "%d" rd /s /q "%d"
Non-recursive deletion of folders in the folder the .bat is in (used with wildcard, as you cannot have more than one folder with the same name anyway):
FOR /d %d IN ("backdrops*") DO #IF EXIST "%d" rd /s /q "%d"
Recursive deletion of folders starting in the folder of your choice:
FOR /d /r "PATH_TO_FOLDER" %d IN ("backdrops") DO #IF EXIST "%d" rd /s /q "%d"
Non-recursive deletion of folders in the folder of your choice (used with wildcard, as you cannot have more than one folder with the same name anyway):
FOR /d %d IN ("PATH_TO_FOLDER/backdrops*") DO #IF EXIST "%d" rd /s /q "%d"
I look at this question from the .Net developer's point of view. Sometimes it is needed to wipe all */bin/ and */obj/ subfolders recursively starting from the directory from which the batch script is executed.
I tried abovementioned solutions and sighted a crutial point:
Unlike other variants of the FOR command you must include a wildcard (either * or ?) in the 'folder_set' to get consistent results returned.
Source: https://ss64.com/nt/for_d.html
When adding echo for each found result before deleting it we can ensure that there are no false positive matches. When I have done so, I found out that using (obj) folder_set without a wildcard triggers DO expression for each subfolder even if it doesn't match a mask. E.g. deleting the "/.git/objects/" dir which is bad. Adding a question mark (0 or 1 occurrence of any symbol except dot) at the end of the mask solves this issue:
#echo off
FOR /d /r %%F IN (obj?) DO (
echo deleting folder: %%F
#IF EXIST %%F RMDIR /S /Q "%%F"
)
FOR /d /r %%F IN (bin?) DO (
echo deleting folder: %%F
#IF EXIST %%F RMDIR /S /Q "%%F"
)
The same goes for any other masks. E.g. (packages?) and (node_modules?) to wipe cached libraries for making a backup archive more lightweight.
Given the directory structure c:\a\b\c\d\ - what DOS command will delete all files contained in c:\a along with any files in the subdirectories b, c, and d? We do not want to delete any directories - just files.
The DEL command only deletes files, not directories, so the following command will do as you asked:
DEL /S C:\a
You need to use RMDIR to remove directories.
Use:
del *.* /s /q
Check the directories are still there:
tree
#del /S /Q /F a\* >nul
#for /d %%i in (a\*) do #rmdir /s /q "%%i"
I know we can write programs to do it.
I know we can write other scripts (perl/vbscript/ etc.) to do it.
I am looking for a command prompt/batch file solution to delete all folders matching sub_* (like sub_1, sub_2 ... ) to be deleted. rmdir or rd doesn't support wildcards, and I'm not able to figure out how to pipe the output of dir sub_*/ad command to delete command one by one as well. Any loop syntax etc. I can use?
for /d %x in (sub_*) do rd /s /q "%x"
You need to double the % if used in a batch file:
for /d %%x in (sub_*) do rd /s /q "%%x"
Untested, make sure to first use echo or something else that doesn't immediately wipe the directories ;)
forfiles /P C:\where\my\dirs\at /M sub_* /C "cmd /c if #isdir==TRUE rmdir /s /q #file"