Recursively delete folders with specific extension - batch-file

I have a file structure of images that is fairly flat although quite large (only goes 3-4 levels deep). In this structure I want to delete all folders that end with '.files' (no quotes).
Note that the files I want to delete are hidden.
I saw a relevant question (linked below) that suggested the following batch file (except using '_svn')
Command line tool to delete folder with a specified name recursively in Windows?
for /d /r . %d in (_svn) do #if exist "%d" rd /s/q "%d"
but it didnt work quite right for me. I got the following error when running at the directory I want to start:
d" rd /s/q "d" was unexpected at this time.
So to be clear, I'm looking for a command that I can put into a batch file which I can cd to my desired directory, run the command, and delete directories beneath the current directory that end in '.files'
Any suggestions?

for /f %a in ('dir C:\yourdir\*.files /b /s /a:hd') do rd /s /q "%a"
or if running it from a batch file use 2 %'s
for /f %%a in ('dir C:\yourdir\*.files /b /s /a:hd') do rd /s /q "%%a"

Related

" Access is denied." error while trying to delete TEMP files using batch file

I am trying to delete all my temp files by running below batch file. but unfortunately, i am receiving "Access is denied." error. I am not sure on the root cause or how to minimize this.
#echo off
cd %temp%
for /d %%D in (*) do rd /s /q "%%D"
del /f /q *
Also suggest a way to delete all internet temp files(IE).
The way you're doing it is really dangerous:
cd without /D changes directory but not the drive. So if you run this script from drive D: it will change the current directory of drive C then will proceed deleting everything in your D: drive.
If you are lucky, you don't have permissions on your other drive. If you're not you lose all your files.
You would have needed cd /D %TEMP% to make it safe, but it's even better to avoid to change current directory:
#echo off
for /d %%D in (%TEMP%\*) do rd /s /q "%%D"
del /f /q %TEMP%\*
Of course, temp cleanup can fail because some files are in use by running programs. In that case, just reboot and run the script again.
I just ran this script and now there's 3 dirs and 10 files in my temporary directory (there were a lot of old files before)
To delete temporary IE files, it's slightly different as the directories are hidden so we have to list hidden dirs with a special dir command (the FOR command does not see the hidden directories)
#echo off
set IETEMP=%LOCALAPPDATA%\Microsoft\Windows\INetCache
for /F %%D in ('dir /AHD /B %IETEMP%') do rd /s /q "%IETEMP%\%%D"
del /f /q %IETEMP%\*

Delete everything from Subfolders through Batch file

I have directories like:
c:\Project\Current\stage1\somefiles and some folders
c:\Project\Current\stage2\somefiles and some folders
c:\Project\Current\stage3\somefiles and some folders
c:\Project\Current\stage4\somefiles and some folders
c:\Project\Current\stage5\somefiles and some folders
.
.
.
c:\Project\Current\stage500\somefiles and some folders
I want to create a batch file so that everything inside stage1, stage2,..., stage500 will get deleted but not any of other folders so that I can still see the above directories but empty.
Can someone please help?
Try this:
#echo off
CD c:\Project\Current /d
for /f "tokens=*" %%f in ('dir /a-d /s /b') do (
del "%%f" /q /f
)
There are three important parts:
for /f "tokens=*" %%f means we are iterating over all lines that are generated by the following command and temporarily save each line in the variable %%f for each iteration.
dir /a-d /s /b is the core of the code. This will list all files inside c:\Project\Current\ including all subfolders. /a-d means that directories will be ignored as we don't want them to be erased. /s means we are searching any subfolder. /b sets the output format to simple mode so that each line of the output will contain nothing but the full path to a file.
del "%%f" /q /f simply deletes the file which is stored in %%f. /q means "don't ask me if I'm sure, just erase it" and /f means that any file - even if it is marked as system file or as invisible or protected - will be deleted. Don't miss the quotation marks around %%f as otherwise paths containing spaces will cause trouble.
I found the answer and is very simple
for /d %%X in (c:\Project\Current*) Do (
for /D %%I in ("%%X\*") do rmdir /s/q "%%I"
del /F /q "%%X\*")
Thanks for everyone's help..

Batch script copy folder using wildcard

I need to copy a folder matching a wildcard, for instance FOLDER_*. That folder will be in the presence of other files, so I need the command to segregate it from everything else. Also, the command needs to recursively search through the directory, and return only the FOLDER that matches the wildcard, with its contents intact. Then it needs to copy it to another folder. Any ideas? I've tried quite a few variants - here is the last thing I tried.
for /D /R %%f in (FOLDER_*) do xcopy %%f %~dp0\TestResults
Next code snippet should copy all found FOLDER_* folders including their subfolders but omits all their parent folder(s) like upfolder\ in upfolder\FOLDER_* (however could be improved to include it, of course).
for /F loop against dir /b /s /ad (a static list of subfolders) is used instead of for /D /R as the option /d /r is undocumented.
Pay your attention to recursion like FOLDER_main\FOLDER_sub1\FOLDER_sub11 etc.
Operational mkdir and xcopy commands are merely echoed for debugging purposes only:
#echo OFF
for /F "delims=" %%f in ('dir /B /S /AD FOLDER_*') do (
echo mkdir "%~dp0\TestResults\%%~nxf" 2>NUL
echo xcopy /S /E /C "%%~ff" "%~dp0\TestResults\%%~nxf\"
)
Consider adding more switches to xcopy, for instance
/H Copies hidden and system files also.
/R Overwrites read-only files.
/Y Suppresses prompting to confirm you want to overwrite an existing destination file.

Batch command to delete all subfolders with a specific name

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.

Bat File Delete All Directories except the last created

I want to create .bat file which allow me to delete all folder of one directory (C:\Test), except the last created folder (the folder which is the most recent in time). It's easy to me to delete all folder of one directory but try to find the most recent it's not.
for /f "skip=1 delims=" %%a in ('dir /b /ad /tc /o-d "c:\test"') do echo rmdir /s /q "c:\test\%%a"
Sort the list of directories in creation time descending, skip the first one, remove the rest.
rmdir commands are echoed to console. If output is correct, remove the echo command.
To use it from command line, replace all %% with %

Resources