Batch script to delete file according to a set time

Asked

Viewed 901 times

0

I am in need of a batch that deletes specific files from a backup folder after the files are 30 days old (example).

I found this code but it uses an external program:

@echo off
move c:\TESTE\*.* d:\TESTE2\
exit

If you need to create directories first go by inserting the mkdir director_name commands

To delete old files I used this one:

@echo off

REM ///////////////////////////////////////////////////////////
REM // Define as variaveis de data //
REM ///////////////////////////////////////////////////////////

@Rem make var nowDay
FOR /F "TOKENS=1* DELIMS=/" %%A IN ('date/t') DO SET nowDay=%%A

REM ///////////////////////////////////////////////////////////
REM // Lista em log os arquivos encontrados //
REM ///////////////////////////////////////////////////////////

FORFILES /S /p C:\teste1\ /d -3 /C "CMD /C echo @FILE @FDATE" > C:\Logs\%nowDay%.log

REM ///////////////////////////////////////////////////////////
REM // Remove os arquivos encontrados //
REM ///////////////////////////////////////////////////////////

FORFILES /S /p C:\teste1\ /d -3 /c "CMD /C DEL @FILE /Q"

exit

There is a simpler way?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.