?
Solved

DOS Batch Script

Posted on 2009-02-13
11
Medium Priority
?
559 Views
Last Modified: 2012-05-06
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.
0
Comment
Question by:jmelcher
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 71

Expert Comment

by:Qlemo
ID: 23633241
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

0
 
LVL 2

Expert Comment

by:alfil_28
ID: 23633248
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
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 23633259
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

0
Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

 

Author Comment

by:jmelcher
ID: 23633430
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.
0
 

Author Comment

by:jmelcher
ID: 23633464
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"

0
 

Author Comment

by:jmelcher
ID: 23633512
whops, I meant to say that I changed the findstr to
findstr /L /I /c:"Example Report"
0
 
LVL 21

Expert Comment

by:AmazingTech
ID: 23633861
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

0
 
LVL 21

Expert Comment

by:AmazingTech
ID: 23633883
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 (
0
 
LVL 71

Accepted Solution

by:
Qlemo earned 2000 total points
ID: 23634131
This will handle spaces in filename.

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

Open in new window

0
 

Author Comment

by:jmelcher
ID: 23635025
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?
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 23635090
Use it instead of the two dots after "robocopy", e.g.

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

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Loops Section Overview
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question