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
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.
i have a question. I need to delete all files in subfolders but not the sub folder or the main folder older than 1 day.
#echo off
forfiles -p "C:\Users\remote\Downloads" -s -m *.* /D -1 -c "cmd /c del #path"
i have this code which delete all files older than 1 day in downloads but what i need /seek is:
C:\users\remote\downloads\%variable%
and that only the files whitin variable will be deleted. i have many "variables".
it is for my work and as a test i do it local.
FOR /D will allow you to iterate over subdirectories. How about
FOR /D %d IN (C:\Users\remote\Downloads\*) DO ( forfiles /P "%~d" /D -1 /C "cmd /c del #path" )
You'll need to "escape" the percent sign in the iteration variable if you put this command in a script:
FOR /D %%d IN (C:\Users\remote\Downloads\*) DO (
FORFILES /P "%%~d" /D -1 /C "cmd /c del #path"
)
I am just getting into Bat files.
I am trying to delete old folders on a network shared drive but skip 2 of the containing folders by name.
Basically I need to all files that I make daily and always keep 2 old files.
Code that deletes all files that are older than 3 days:
PushD "\\****-****\build" &&(
ForFiles /D -3 /C "CMD /C if #ISDIR==TRUE echo RD #FILE &RD /S #FILE
) & PopD
And I was thinking something like this: if NOT #FNAME == %name%. I don't totally understand the process, am I able to have two conditions in the forFiles? do I have to have /c before?
PushD "\\****-****\build" &&(
ForFiles /D -3 /C "CMD /C if NOT #FNAME == %name% if #ISDIR==TRUE echo RD #FILE &RD /S #FILE
) & PopD
I can't seem to get it, would you mind helping me out?
Thanks!
Yes, nesting if commands in their then branches is the way how-to have logical AND. Note proper quoting in next code snippet:
#ECHO ON >NUL
#SETLOCAL enableextensions
set "name=SO"
set "nam2=SU"
pushd "D:\VB_scripts"
#rem all directories
ForFiles /D -2 /C "CMD /C if #ISDIR==TRUE echo #FILE"
#rem all directories except "SO"
ForFiles /D -2 /C "CMD /C if #ISDIR==TRUE if not #FNAME=="""%name%""" echo #FILE"
#rem all directories except "SO" and "SU"
ForFiles /D -2 /C "CMD /C if #ISDIR==TRUE if not #FNAME=="""%name%""" if not #FNAME=="""%nam2%""" echo #FILE"
popd
#ENDLOCAL
Output:
==>D:\bat\SO\31346676.bat
==>set "name=SO"
==>set "nam2=SU"
==>pushd "D:\VB_scripts"
==>ForFiles /D -2 /C "CMD /C if #ISDIR==TRUE echo #FILE"
"Class Pack"
"Oldies"
"SO"
"SU"
"WMI"
==>ForFiles /D -2 /C "CMD /C if #ISDIR==TRUE if not #FNAME=="""SO""" echo #FILE"
"Class Pack"
"Oldies"
"SU"
"WMI"
==>ForFiles /D -2 /C "CMD /C if #ISDIR==TRUE if not #FNAME=="""SO""" if not #FNAME=="
""SU""" echo #FILE"
"Class Pack"
"Oldies"
"WMI"
==>popd
It is a little bit messy but I was able to do it on a local directory:
#echo off
mkdir Temp\Temp
REM Copy all old file to Temp dir
forfiles -p "%cd%" -m *.* /D -2 /C "cmd /c xcopy #path %cd%\Temp"
REM copy a random file from Temp dir to Temp/Temp dir and then delete it
FOR %%A in (%cd%\Temp\*) do (
COPY "%%A" %cd%\Temp\Temp\
DEL "%%A"
GOTO :Second
)
:Second
Rem copy second random file from Temp dir
FOR %%A in (%cd%\Temp\*) do (
COPY "%%A" %cd%\Temp\Temp\
GOTO :Del
)
:Del
Rem delete all old files from local dir
forfiles -p "%cd%" -m *.* /D -2 /C "cmd /c del #path"
Rem Copy back two random old files to local dir
xcopy %cd%\Temp\Temp\* %cd%
Rem remove Temp dir
rmdir /s /q Temp
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.