简体   繁体   中英

Is it possible to use a Windows batch file to delete certain rar files?

I am trying to use a batch file to delete .rar files from a certain folder which are older than 30 days. However the batch file I have right now deletes any file older than 30 days, I want to limit this just to .rar files. My code is below.

REM *******************************************************************
REM Deleting database backups older than 30 days. 

@echo off
@Echo Deleting database backups older than 30 days...
rem echo y|forfiles /p C:\Batch\Archives\*.rar /d -30 /c "cmd /c del @path"

pause  

EXIT

If you look at forfiles help, you will see that the /P parameter is just for the path. You need to use the /M parameter for a file mask. So you should use this:

forfiles /p C:\Batch\Archives /m *.rar /d -30 /c "cmd /c del @path"

In my case, there's no need to echo an 'y' to confirm. But if in your case del is asking for confirmation, just use its /Q parameter.

On a side not, there's no need to use @ again after using @echo off . You either use @echo off in the first line or place a @ before each line, doing both is not necessary and makes your file uglier. Furthermore, I would place the @echo off before those REM s so they are not displayed. If you want to show those messages, just use echo . The final EXIT is also not needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM