Link to home
Start Free TrialLog in
Avatar of jmelcher
jmelcher

asked on

DOS Batch Script

I need a batch script to delete all files in a folder older then 7 days from today. The date of the file is based off the file name. The file names all have this standard naming convention:
Report_YYYYMMDD.xls.gpg
I would imagine I could do a DEL command inside a for loop, but  I need some help with the tokens to use.
Avatar of Qlemo
Qlemo
Flag of Germany image

There are some approaches. One is to use robocopy to get a list of files older than x days (following line is for direct execution in cmd.exe. If you want to use in a batch, replace %F with %%F):
 

robocopy . . /minage:8 /L /IS /NJH /NJS /NS /NC | findstr get | for /F %F in ('findstr /L /I /c:"Report"') do del %F

Open in new window

Hi

Create a batch file with this name delbydate.bat :

echo off
echo Delete files of %1
pause
del /q ???????%1.xls.gpg
echo done

To run it from prompt you should type the name of the bat file and then the date you want to delete:

C :\> delbydate 200090213

remember the bat file has to live in the same folder as the files you want to delete.
Try this with a copy of the folder and some of the files.

Let me know if this helped
Sorry, was my testcase, here is correct version:

robocopy . . /minage:8 /L /IS /NJH /NJS /NS /NC | findstr /L /I /c:"Report" | for /F %F in ('more') do del %F

Open in new window

Avatar of jmelcher
jmelcher

ASKER

Qlemo...I like the robocopy approch, but when it tries to run the del command, it only puts the first word "Report" in the del command, not the whole filename, so obviously it does not work.
I might want to add that the name of the file is not Report, but Example Report_YYYYMMDD.xls.gpg

so i tried to change the piece findstr /L /I /c:"Report" to findstr /L /I /c:"Example Report_YYYYMMDD.xls.gpg"

whops, I meant to say that I changed the findstr to
findstr /L /I /c:"Example Report"
Give this a try.

Remove the ECHO before the Del command when it looks OK.
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
 
Set DaysAgo=7
 
call :SubDays %DaysAgo%
 
for /f "tokens=1,2,3 delims=_." %%a in ('dir /a-d /b "C:\Folder\Report_*.xls"') do (
    If %%b LSS %Year%%Month:~-2%%Day:~-2% (
        ECHO Deleting %a_%b.%c more than %DaysAgo% days ago.
        ECHO Del /f /q %a_%b.%c
    )
)
 
 
 
goto :EOF
 
:SubDays
Set DaysInMonth=31,28,31,30,31,30,31,31,30,31,30,31
 
Set Year=%date:~-4%
Set Day=1%date:~-7,2%
Set Month=1%date:~-10,2%
 
Set /a Day-=%1
:FixDay
If !Day! LEQ 100 (
    Set /a Month-=1
    if !Month! == 100 (
        Set /a Year-=1
        Set Month=112
    )
    Set MCounter=101
    Set DaysInThisMonth=
 
    for %%d in (%DaysInMonth%) do if not defined DaysInThisMonth if !MCounter! == !Month! (Set DaysInThisMonth=%%d) else (Set /a MCounter+=1)
 
    if !Month! == 102 (
        if %Year:~-2% == 00 (
            Set /a Mod=!Year:~0,2! %% 4
            if !Mod! == 0 Set /a DaysInThisMonth+=1
        ) else (
            Set /a Mod=!Year! %% 4
            if !Mod! == 0 Set /a DaysInThisMonth+=1
        )
    )
    Set /a Day+=!DaysInThisMonth!
)
If !Day! LEQ 100 GOTO FixDay
GOTO :EOF 
Open in New Window Select All 

Open in new window

Sorry. Just got to reading some of your posts

Change Line 8 to this

for /f "tokens=1,2,3 delims=_." %%a in ('dir /a-d /b "C:\Folder\Example Report_*.xls"') do (
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Qlemo,
One more question before I give the points to you; since that is the solution I will be using. Thanks for the help from everyone else.

If I wanted to use the robocopy and specify a full unc path for the folder where would I do that?
Use it instead of the two dots after "robocopy", e.g.

robocopy \\this\is\my\path \\this\is\my\path /minage (aso.)