1.

Solve : MSDOS batch to delete folders older than 30 days?

Answer»

Hello i have the following batch file:

echo off
:: variables
set drive=W:\JoseNunes\backup
set backupcmd=xcopy /s /c /d /E /h /i /r /y

SET dd=%DATE:~0,2%
SET mm=%DATE:~3,2%
SET yyyy=%DATE:~6,4%

SET hh=%TIME:~0,2%
SET mn=%TIME:~3,2%
SET ss=%TIME:~6,2%

set dayfolder="%yyyy%-%mm%-%dd%"
set timefolder="%hh%%mn%%ss%"

echo ###checking if daily folder exists
IF NOT EXIST "%drive%\%folder%\". (
   echo ###creating dayli folder.
   md "%drive%\%dayfolder%\".
   echo ###daily folder created.
)

IF NOT EXIST "%drive%\%folder%\%timefolder%\". (
   echo ###creating dayli folder.
   md "%drive%\%dayfolder%\%timefolder%\".
   echo ###daily folder created.
)
   
echo ### Gestware BD...
%backupcmd% "C:\20988" "%drive%\%dayfolder%\%timefolder%\Gestware"

echo Backup Complete!
pause


THis batch makes a backup of some files i NEED and creates a folder with the day and inside that one with the hour.
I need to delete folder older than 30 days.


Thanks in advanceIn general, batch code does not do date/time arithmetic. I did see a batch solution once but it was 7 pages of ponderous code and seemed like a lot of overhead for what really is a simple task in any other language.

If you can, download ForFiles. This is a Microsoft batch utility you can insert into your existing batch code. After unzipping the executable into a directory, run forfiles --h from the command prompt for the online help.

You might also consider a separate VBSCRIPT or Powershell SCRIPT to do the folder aging and deleting by calling it from your batch file. Lots of possibilities limited only by your imagination.

 



Discussion

No Comment Found