I want to create a batch file which should delete all subfolders of a folder which are older than 10 days, using Windows 7
Any help would be appreciated.
Adapted from this answer to a very similar question:
FORFILES /S /D -10 /C "cmd /c IF #isdir == TRUE rd /S /Q #path"
You should run this command from within your d:\study folder. It will delete all subfolders which are older than 10 days.
The /S /Q after the rd makes it delete folders even if they are not empty, without prompting.
I suggest you put the above command into a .bat file, and save it as d:\study\cleanup.bat.
FORFILES /S /D -10 /C "cmd /c IF #isdir == TRUE rd /S /Q #path"
I could not get Blorgbeard's suggestion to work, but I was able to get it to work with RMDIR instead of RD:
FORFILES /p N:\test /S /D -10 /C "cmd /c IF #isdir == TRUE RMDIR /S /Q #path"
Since RMDIR won't delete folders that aren't empty so I also ended up using this code to delete the files that were over 10 days and then the folders that were over 10 days old.
FOR /d %%K in ("n:\test*") DO (
FOR /d %%J in ("%%K*") DO (
FORFILES /P %%J /S /M . /D -10 /C "cmd /c del #file"
)
)
FORFILES /p N:\test /S /D -10 /C "cmd /c IF #isdir == TRUE RMDIR /S /Q #path"
I used this code to purge out the sub folders in the folders within test (example n:\test\abc\123 would get purged when empty, but n:\test\abc would not get purged
If you want using it with parameter (ie. delete all subdirs under the given directory), then put this two lines into a *.bat or *.cmd file:
#echo off
for /f "delims=" %%d in ('dir %1 /s /b /ad ^| sort /r') do rd "%%d" 2>nul && echo rmdir %%d
and add script-path to your PATH environment variable. In this case you can call your batch file from any location (I suppose UNC path should work, too).
Eg.:
YourBatchFileName c:\temp
(you may use quotation marks if needed)
will remove all empty subdirs under c:\temp folder
YourBatchFileName
will remove all empty subdirs under the current directory.
Related
FORFILES /P "C:\Temp\" /D -3 /S /C "cmd /c if #isdir==FALSE del /F /Q #path"
above script is working fine and delete all files under Temp and its subdirectory older than 3 days.
I want to exclude all files from specific folder say all files from folder XYZ or full path-> C:\Temp\ABC\XYZ
Note : all files under XYZ folders are having pattern say Test*.*.csv
forfiles does not have an exclude option. You have to use something like findstr /V to exclude the results, but that will not form part of the forfiles command in itself. We simply incorporate a for loop and ecxlude using findstr /V, then delete:
#echo off
for /f "delims=" %%i in ('FORFILES /P "%temp%" /D -3 /S /C "cmd /c if #isdir==FALSE echo #path"^| findstr /VI "C:\Temp\ABC\XYZ"') do del /F /Q "%%~i"
with the help of batch file I am able to delete files from the main directory and sub directories but I am finding it difficult to delete empty sub directories left after files inside it got deleted . I have written script but it is not working for deleting empty sub directories part
MY Batch Script
#echo off
Set _Extentions=".LOG"
Set _FolderList= "D:\FCS"
Set _FolderList=%_FolderList% "D:\SMSGateway\SMSLogs"
Set _NoOfDays=15
for %%s in (%_FolderList%) do (
for %%d in (%_Extentions%) do (
call :process_Deletion %%s %%d
)
)
:process_Deletion
IF [%1] == [] GOTO EndOfFun
set _ParaPath=%1
set _ParaExtn=%2
echo forfiles /P %_ParaPath% /S /m *%_ParaExtn% /D -%_NoOfDays% /C "cmd /c if #isdir==FALSE echo #path #fdate #ftime"
echo forfiles /P %_ParaPath% /S /m *%_ParaExtn% /D -%_NoOfDays% /C "cmd /c if #isdir==FALSE del #file"
echo for /f "delims=" %%d in ('dir %_FolderList% /s /b /ad ^| sort /r') do rd "%%d"
:EndOfFun
EXIT /B
What am I doing wrong here ? Please help..
Empty directories require rd or rmdir
https://www.computerhope.com/rmdirhlp.htm
The rd and rmdir commands remove empty directories in MS-DOS. To delete directories with files or directories within them, you must use the deltree command. If you are running Microsoft Windows 2000 or Windows XP, use the /S option.
This question already has answers here:
Batch file to delete files older than N days
(25 answers)
Delete sub directories older than 30 days
(7 answers)
How to delete files older than x days with FORFILES?
(1 answer)
Closed 4 years ago.
I need a batch file to delete folders old of 1 day.
The folders contains inside files that will be removed together with the folders
I tryed 3 different code but do not remove the old folders.
Path where are located folders to delete (path have space):
D:\Programmi Installati\
Example folder names (start with log_)
log_1
log_10-12-2019
log_2008-10000
log_222222211111
Days old: 1
:: Code 1
#echo off
setlocal
set target="D:\Programmi Installati\"
set days=1
for /f "usebackq delims=" %%G in (
'forfiles /p "%target%" /c "cmd /c if /i #isdir == true echo #path" /d -%days% 2^>nul'
) do rd /s /q "%%~G"
pause
endlocal & exit /b
:: Code 2
forfiles /p "D:\Programmi Installati\" /d -1 /c "cmd /c if #isdir==true rd /s /q #path"
:: Code 3
FORFILES /P "D:\Programmi Installati\" /S /C "cmd /c IF #isdir == TRUE rmdir /S #path /Q" -D -1
I'm interested a fix of code already indicate or a new code that works.
It seems that you want to check all log directories starting with log_ only and delete them if they are older than a day, or at least have a date of yesterday, if so, I believe that this is what you want to to:
forfiles /P "D:\Programmi Installati" /M "log_*" /D -1 /C "cmd /c if #isdir==TRUE echo rd /s /q #path"
Note! This will only echo the command rd /s /q path in order for you to verify it does what you intended it to. Once you are satisfied, remove echo from the end of the line in echo rd /s /q #path
i would like to know how you can delete multiple files in different folders with batch commands.
I have the following code, this code works fine for 1 map but i need to do it for multiple maps :
forfiles /p "D:\CHILI_Publisher\Data\Environments\Adecco\Cache_Data\Assets" /s /d -10 /c "cmd /c echo #file"
PAUSE
This is the code for the various maps and various file types with wildcards (this one gives an error : The directory name is invalid:
forfiles /p "D:\CHILI_Publisher\Data\Environments\*.*\Cache_Data\*.*" /s /d -10 /c "cmd /c echo #file"
PAUSE
Tl;DR : I have an error and would like to know how to use a wildcard correctly in batch files.
You can wrap FORFILES in a FOR loop:
for /d %D in (c:\temp\a*;c:\temp\b*;c:\temp\c*) do forfiles /p %D /s /c "cmd /c echo #file" /d -10
If you need to find all folders named CACHE_DATA under a super folder you can navigate to the super folder (cd D:\CHILI_Publisher\Data\Environments) and run this:
for /f %F in ('dir /B /S /AD cache_data') do for /d %D in (%F) do forfiles /p %D /s /c "cmd /c echo #file" /d -10
If you put the script in a BATCH file remember to escape % with %%.
I used this command:
FORFILES /S /D -10 /C "cmd /c IF #isdir == TRUE rd /S /Q #path"
to delete all folders older than 10 days ,
and it is working fine.
problem is that I get an error and it's failing my build:
ERROR: The system cannot find the file specified.
I need to be able to exit with ERRORLEVEL=0.
Add echo before the command to see what's wrong (maybe it's trying to delete a subfolder of an already deleted folder):
FORFILES /S /D -10 /C "cmd /c IF #isdir==TRUE echo #path & rd /S /Q #path"
Or you can simply make it fail-proof by explicitly checking if the folder exists:
FORFILES /S /D -10 /C "cmd /c IF #isdir==TRUE if exist #path rd /S /Q #path"
BTW, forfiles method doesn't seem reliable as a folder date isn't updated for its 'grandchild' files. I would use robocopy in list-mode to generate the list of old files and then process it: get the folder path of each file and delete it if it still exists.