• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 565
  • Last Modified:

Need a batch file to delete files every day.

The below list of files gets created on a daily basis and I need help creating a script (batch) to delete these files at the start of the second day.  So the previous days files are deleted.  The problem is I have multiple files with different file names and types created in that folder and cannot delete these files.  The only files I need to delete are the ones listed below.

To Delete
20081114_BO14orderdepth.log
20081114_BO2order.log
20081114_CB16trade.log
20081114_BD2price.log
20081114_InitialQueries.log
20081117_BO1415order.log

Not to be deleted
6092_1120.txt
6190_1114.txt
6191_1118.txt
ReferencePoint (Folder)

The above files are also stored in the same directory and cannot be deleted
0
viper22
Asked:
viper22
  • 4
  • 3
  • 2
  • +1
1 Solution
 
sirbountyCommented:
So, something like:
(it won't delete, just displays the name - remove the echo if it looks correct)
set filehead=%date:~-4%%date:~4,2%%date:~7,2%_
echo del %filehead%*.*

Open in new window

0
 
CoyotesITCommented:
This will probably take some tweaking but try this

From your post, only .log files older than 1 day need to be deleted. The below should get this done

FORFILES will loop through files that are a day old (/D -1) and match (/M *.log) and run the del /f /q on that file.

Good luck!
@ECHO OFF
 
SET WORKDIR=C:\PATH TO YOUR FOLDER\
 
ECHO Cleaning up %WORKDIR%
 
FORFILES /P "%WORKDIR%" /D -1 /M *.log /C "CMD /C DEL /F /Q @FILE"

Open in new window

0
 
viper22Author Commented:
Hi CoyotesIT
Sorry for the questions..can you please explain each one of the syntax below

FORFILES /P "%WORKDIR%" /D -1 /M *.log /C "CMD /C DEL /F /Q @FILE"

Regards
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
CoyotesITCommented:
I did explain what the parameters in the command were doing, here is the actual help of the forfiles command

The /D [Date] parameter which tests the file age
           /D    date          Selects files with a last modified date greater
                    than or equal to (+), or less than or equal to
                    (-), the specified date using the
                    "MM/dd/yyyy" format; or selects files with a
                    last modified date greater than or equal to (+)
                    the current date plus "dd" days, or less than or
                    equal to (-) the current date minus "dd" days. A
                    valid "dd" number of days can be any number in
                    the range of 0 - 32768.
                    "+" is taken as default sign if not specified.

/M [searchMask] this in my example is *.log which will match all files ending with .log in your directory
       /M    searchmask    Searches files according to a searchmask.
                    The default searchmask is '*' .

and finally the /C parameter executes the command which I have as "CMD /D DEL /F /Q @FILE"
/C    command       Indicates the command to execute for each file.
                    Command strings should be wrapped in double
                    quotes.

                    The default command is "cmd /c echo @file".

                    The following variables can be used in the
                    command string:
                    @file    - returns the name of the file.
                    @fname   - returns the file name without
                               extension.
                    @ext     - returns only the extension of the
                               file.
                    @path    - returns the full path of the file.
                    @relpath - returns the relative path of the
                               file.
                    @isdir   - returns "TRUE" if a file type is
                               a directory, and "FALSE" for files.
                    @fsize   - returns the size of the file in
                               bytes.
                    @fdate   - returns the last modified date of the
                               file.
                    @ftime   - returns the last modified time of the
                               file.

                    To include special characters in the command
                    line, use the hexadecimal code for the character
                    in 0xHH format (ex. 0x09 for tab). Internal
                    CMD.exe commands should be preceded with
                    "cmd /c".

FORFILES won't work if you just call     .... /C "DEL /F /Q @FILE" you need to first spawn the cmd.exe and the /C to that tells it once it is opened to execute the command, in this case "DEL /F /Q @FILE" the FORFILES loops through the directory, for each match it makes the filename is stored in the @FILE variable...

The command DEL /F /Q is just deleting the file /F /Q (forcing the deletion of read-only files, and /Q supressing the confirmation [are you sure you want to delete this file you just told me to delete????]

0
 
AmazingTechCommented:
Try this one.

Remove the second ECHO when it looks OK.

Change Set Logfolder= to where your logs are.

As long as your file names
SETLOCAL ENABLEDELAYEDEXPANSION
set Today=%date:~-4%%date:~-10,2%%date:~-7,2%
Set LogFolder=C:\AT
 
FOR /f "Tokens=*" %%a in ('Dir /a-d /b "%LogFolder%\*_*.log"') do (
    Set LogFileDate=%%~na
    IF "!LogFileDate:~8,1!" == "_" (
        Set LogFileDate=!LogFileDate:~0,8!
        IF !LogFileDate! LSS %Today% (
            ECHO Deleting %%a
            ECHO DEL "%LogFolder%\%%a"
        )
    )
)

Open in new window

0
 
AmazingTechCommented:
Sorry.

As long as your file names are not in a subfolder. If you want to do subfolders then use this one.
SETLOCAL ENABLEDELAYEDEXPANSION
set Today=%date:~-4%%date:~-10,2%%date:~-7,2%
Set LogFolder=C:\AT
 
FOR /f "Tokens=*" %%a in ('Dir /a-d /b /s "%LogFolder%\*_*.log"') do (
    Set LogFileDate=%%~na
    IF "!LogFileDate:~8,1!" == "_" (
        Set LogFileDate=!LogFileDate:~0,8!
        IF !LogFileDate! LSS %Today% (
            ECHO Deleting %%a
            ECHO DEL "%%a"
        )
    )
)

Open in new window

0
 
CoyotesITCommented:
AmazingTech

The problem with this is your Today variable will not remain consistent. Dealing with dates via batch are a nightmare because there is not a true built-in feature to manipulate the format or output. Once the day is a single digit the output will not be correct, as it will include the / either in the month when it is 1-9, or the day when it is 1-9. Using the FORFILES to test the actual file creation date is solid as this will correctly determine the date of the file, and this is built in to the system. There are of course other utilities, or even vbscript that could accomplish this, but given that FOREFILES will do this in a one liner the extra confusion of dealing with formatting a date to match is not needed.


0
 
CoyotesITCommented:
I will say though, your solution will work if the sShortDate of the machine is set to MM/dd/yyyy or similar, however if it is set to M/d/yyyy or any other variation then this could cause issues with the manipulating %date% by assuming where the correct digits will be.
0
 
viper22Author Commented:
Thank You everyone for your help but this one was what I was looking for.  As CoyoteIT said the date format was going to be an issue for me.  Thank You All once again...
0
 
AmazingTechCommented:
We could set the sShortDate format to exactly what we want and then set it back.
SETLOCAL ENABLEDELAYEDEXPANSION
 
FOR /f "Tokens=3" %%a in ('REG QUERY "HKCU\Control Panel\international" ^| FIND /i "sShortDate"') DO Set csShortDate=%%a
reg add "HKCU\Control Panel\international" /V sShortDate /d "yyyy/MM/dd" /f
 
Set Today=%Date:/=%
 
reg add "HKCU\Control Panel\international" /V sShortDate /d "%csShortDate%" /f
 
Set LogFolder=C:\AT
 
FOR /f "Tokens=*" %%a in ('Dir /a-d /b /s "%LogFolder%\*_*.log"') do (
    Set LogFileDate=%%~na
    IF "!LogFileDate:~8,1!" == "_" (
        Set LogFileDate=!LogFileDate:~0,8!
        IF !LogFileDate! LSS %Today% (
            ECHO Deleting %%a
            ECHO DEL "%%a"
        )
    )
)

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now