简体   繁体   中英

Windows batch - Delete files of certain age and type and list them

I'm looking for a solution to allow users to delete certain file type older than X days from a specific path.
I managed to create this batch file so far:

@echo off

set /p FileAge= Delete files older than this many days: 
set /p FileType= File type to delete. Seperate with comma. (mov, mxf...): 
set /p FilePath= Full path of files location: 

for %%G in (%FileType%,) do forfiles -p "%FilePath%" -s -m *%%G -d -%FileAge% -c "cmd /c echo @path"

Choice /M "These files will be deleted. Do you want to continue?"
If Errorlevel 1 Goto Yes
If Errorlevel 2 Goto No

:Yes
for %%G in (%FileType%,) do forfiles -p "%FilePath%" -s -m *%%G -d -%FileAge% -c "cmd /c del @path"

:No
exit

PAUSE

There are several things missing:

  • In line 9 - What would be the best way to count how many files are going to be deleted?
  • I would like the batch to output a list of deleted files to a log file. Something like log_01312014.txt

Hello O_ren to Stack Overflow.

Assume you are using Windows 7 or later we will use a folder named "Files" to transfer desired file types inside "To_Delete" Folder + a log file with the name "Log_%date%.txt"

After that you can simply delete or keep your files inside "To_Delete" folder with del command.

in the example below this one liner solution will move ini files older than 7 days to our folder "To_Delete" and log the moved files.

  robocopy "C:\Files" "c:\Files\To_Remove" /mov *.ini /minage:7 >C:\Files\Log_%date:~-4,4%_%date:~-10,2%_%date:~-7,2%.txt

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