Question

I want to delete all files older than 1 day / 24 hours using a timed task in the OS and a batch file.

This is needed for a simple backup mechanic I've implemented, that saves a certain file every hour and I don't want to delete old files manually.

I already implemented the following batch code, which successfully create a backup every hour. However, the delete portion of the code doesn't seem to work.

Does anyone have any suggestions as to why?

PS: I'm using Win7 x64.

Code:

rem Get day of week
SETLOCAL enabledelayedexpansion
SET /a count=0
FOR /F "skip=1" %%D IN ('wmic path win32_localtime get dayofweek') DO (
if "!count!" GTR "0" GOTO next
set dow=%%D
SET /a count+=1
)

:next

rem Check if day of week is Saturday or Sunday. If so, goto end of file.
if %dow% == 6 GOTO eof
if %dow% == 7 GOTO eof

rem Set hour
set TIMESTAMP=%TIME:~0,2%"Uhr"

rem Copy file including timestamp
xcopy "SOURCE FOLDER" "TARGET FOLDER\filename_%date%_%TIMESTAMP%.accdb*" /Y

rem Delete files older than 1 day
forfiles -p "TARGET FOLDER (from above)" -s -m *.* /D -1 /C "cmd /c del @path"


:eof

Edit:

I found the cause of the original problem, it was the UNC paths. Instead of using them I just mapped the path to a drive letter now and deleting works fine now.

However, it deletes all my files now, instead of just the ones older than 1 day.

I suspect that forfiles uses the last modification date instead of the creation date, as the last modification is still on 26th and the creation was like an hour ago...

Is there a way to circumvent that behavior and use the creation date instead?

Alternatively, is there a way to set the modification date to a certain timestamp after copying the file?

Was it helpful?

Solution

I was able to solve the problem by using the getTimestamp.bat from dbenham: http://www.dostips.com/forum/viewtopic.php?f=3&t=4847&p=27422#p27422

This is the only working batch file I could find that is able to calculate the date of yesterday (potentially any day for that matter).

It is probably the only working one, because all others (and also all of the ones linked in the regarding StackOverflow threads) don't take locale settings into account, so they didn't even start for me.

call getTimeStamp -od -1 -f {dd}.{mm}.{yyyy} -r dt

rem Copy file including timestamp
xcopy "\\unc\folder\fileToBackup.accdb" "\\unc\folder\backupedFile_%date%_%TIMESTAMP%.accdb*" /Y

rem Delete yesterdays file
del "\\unc\folder\backupedFile_%dt%_%TIMESTAMP%.accdb*"

OTHER TIPS

Maybe a little "convoluted" but this should do the work

@echo off
    setlocal enableextensions disabledelayedexpansion

    set "folder=\\server\share\somewhere"

    for /f "tokens=*" %%a in (
        'robocopy "%folder%" "%folder%" /l /nocopy /is /minage:1 /njh /njs /ndl /ns /nc /fp'
    ) do echo del "%%a"

del commands are echoed to console. If the output is correct, remove the echo command.

It uses the robocopy command to search the required files in the UNC path. So, natively, only for vista and later OS. For XP, the Windows 2003 Resource Kit Tools include a working version of robocopy.

EDITED - One more. Trying to avoid the timestamp on files

@echo off
    setlocal enableextensions disabledelayedexpansion

    set "folder=\\server\share\somewhere"

    for /f "tokens=* skip=24" %%a in (
        'dir /b /a-d /tc /o-d "%folder%"'
    ) do echo del "%folder%\%%a"

With a file generated every hour, keep the last 24 and delete the rest.

forfiles does indeed use the modified timestamp, not the created timestamp, so you cannot use it for what you want.

You might like to check the answers to this question which parse the file creation date in a batch script, or if you're willing to use PowerShell then you could try this script with the arguments -CreateTime option.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top