Batch file for recursively deleting files in a folder older than N days

Now I use a batch file to delete all files ending with .snpthat are older than 180 days. The code below works to delete all files ending .snpin the root folder

C: \ Program Files \ Snapshots

But I recently discovered that there are folders organized by date in the Snapshots folder.

"1-10-2014, 12-20-2014, 10-15-2014, etc."

and that the line of code below does not work for a recursive search in each directory and therefore does not delete.

What changes should be made to this code so that it recursively scans folders in the root folder and deletes files that exceed 180 days?

forfiles /M *.snp /P "C:\Program Files\Snapshots" /S /D -180 /C "cmd /c del /F /Q @path"
+4
2

/D ()

forfiles /M *.txt /P "C:\hlpme" /S /C "cmd /c del /f /q @path

, , , CMD

forfiles /D -180 /M *.txt /P "C:\hlpme" /S /C "cmd /c del /f /q @path

/D , 180

+1

- For FORFILES, , .

Set str_Ext=*.snp
Set int_Age=-180
For /R "%~dp0" %%D IN (.) DO (
    For /F "usebackq tokens=*" %%F IN (`FORFILES /P "%%~D" /m %str_Ext% /D %int_Age% 2^>nul`) DO (
        Call :s_Del_File "%%~D" "%%~F"
    )
)
Goto :EOF
:s_Del_File
Set "str_DIR=%~1"
Set "str_FIL=%~2"
Set "str_DIR=%str_DIR:~0,-1%"
DEL /F/Q/A "%str_DIR%%str_FIL%"
Goto :EOF

FOR backquote (~) FORFILES .

, ["] Set , .

+1

Source: https://habr.com/ru/post/1608877/


All Articles