Solved

I need a Batch file to delete files older than 20 minutes.

Posted on 2009-04-14
9
2,919 Views
Last Modified: 2012-05-06
We have a scans folder that used to maintain all scans and I would periodically go in and delete the files out.

Now I need to clean it out every 20 minutes.  I have a scheduled task in Windows 2003 Server that runs every 20 minutes and deletes everything out of the folder.  The issue is that if someone scans in a document at 10:19, at 10:20 it will be deleted.  My users were informed of this but they didn't "get it" and are now complaining that the files are being deleted before they have a chance to get back to their desk.

Is it possible to delete files that are older than 20 minutes and not delete the ones that are newer than 20 minutes?  I know there is a /tc command for DOS that will tell you the timestamp of when the file was created, but I need to know how to use that information in my delete statment.

Does anyone know how to do this?

I was thinking that I would copy the files into an archive folder then delete them.  This would give them 20 more minutes just in case they got busy.  I will tell it to automatically overwrite the files inside the Archive folder.

Below is the code I have so far, and it is working; but not deleting files older than 20 minutes; just copying and purging.

Any help is appreciated.
D:
 

cd Documents\Public\Scans
 

xcopy *.pdf D:\Documents\Public\Scans\archive /y
 

del *.pdf

Open in new window

0
Comment
Question by:roadnrail
  • 6
  • 2
9 Comments
 

Author Comment

by:roadnrail
ID: 24139005
I'm not sure why this is listed in the MS Office Zone.
0
 

Expert Comment

by:Roy_
ID: 24139088
0
 

Author Comment

by:roadnrail
ID: 24140065
That didn't help.  For some reason, the only thing it deleted was itself; which I was wondering how to tell it to point to another location.

I changed the ECHO to DEL and the only thing that appears is ".pdf was unexpected at this time."

Not really sure what that means, but I have other files inside the folder that was created in 2006 and it isn't deleting those either..

Also, I have 3 PDF's and 1 RTF inside the folder.  All of them more than 20 minutes old CODE: SET MyMins=20.

the entire code is below.
@ECHO OFF

:: ------------

:: This is where you set the number

:: of minutes you want subtracted

:: from the current date/time.

:: ------------

SET MyMins=20
 
 

:: ------------

:: Get current date/time

:: ------------

FOR /F "TOKENS=2-4 DELIMS=/ " %%F IN ('DATE /T') DO (

 SET YYYY=%%H

 SET MM=%%F

 SET DD=%%G

)

FOR /F "TOKENS=5-6 DELIMS=: " %%F IN ('ECHO.^|TIME') DO (

 SET HR=%%F

 SET MN=%%G

)
 

IF %DD% LSS 10 (SET DD=%DD:~1%)

IF %MM% LSS 10 (SET MM=%MM:~1%)

IF %HR% LSS 10 (SET HR=%HR:~1%)

IF %MN% LSS 10 (SET MN=%MN:~1%)
 

: ------------

:: Subtract minutes from current time.

:: ------------

SET /A MN=%MN% - %MyMins%
 

:: ------------

:: Do the massively painful

:: reverse calculations.. :(

:: ------------

:LoopMins

IF /I %MN% GEQ 0 (GOTO LoopHrs)

SET /A MN=%MN% + 60

SET /A HR=%HR% - 1

GOTO LoopMins

:LoopHrs

IF /I %HR% GTR 0 (GOTO LoopDate)

SET /A HR=%HR% + 23

SET /A DD=%DD% - 1

GOTO LoopHrs

:LoopDate

IF /I %DD% GTR 0 (GOTO DONE)

set /A mm=%mm% - 1

if /I %mm% GTR 0 goto ADJUSTDAY

set /A mm=12

set /A yyyy=%yyyy% - 1

:ADJUSTDAY

if %mm%==1 goto SET31

if %mm%==2 goto LEAPCHK

if %mm%==3 goto SET31

if %mm%==4 goto SET30

if %mm%==5 goto SET31

if %mm%==6 goto SET30

if %mm%==7 goto SET31

if %mm%==8 goto SET31

if %mm%==9 goto SET30

if %mm%==10 goto SET31

if %mm%==11 goto SET30

if %mm%==12 goto SET31

goto ERROR

:SET31

set /A dd=31 + %dd%

goto LoopDate

:SET30

set /A dd=30 + %dd%

goto LoopDate

:LEAPCHK

set /A tt=%yyyy% %% 4

if not %tt%==0 goto SET28

set /A tt=%yyyy% %% 100

if not %tt%==0 goto SET29

set /A tt=%yyyy% %% 400

if %tt%==0 goto SET29

:SET28

set /A dd=28 + %dd%

goto LoopDate

:SET29

set /A dd=29 + %dd%

goto LoopDate

:DONE

IF %dd% LSS 10 set dd=0%dd%

IF %mm% LSS 10 set mm=0%mm%

IF %HR% LSS 10 SET HR=0%HR%

IF %MN% LSS 10 SET MN=0%MN%
 
 

for %%i in (*.*) do (

 set FileName=%%i

 SET FTIME=%%~ti

 CALL :PROCESSFILE

)
 

set mm=

set yyyy=

set dd=

set thedate=

goto :EOF
 

:PROCESSFILE

set fyyyy=%FTIME:~6,4%

set fmm=%FTIME:~0,2%

set fdd=%FTIME:~3,2%

SET fhr=%FTIME:~11,2%

SET fmn=%FTIME:~14,2%
 

if /I %fyyyy% GTR 2069 set fyyyy=19%FTIME:~6,2%
 

:: --------

:: Deal with File times

:: reported as AM/PM vs.

:: calculated times as 24hr.

:: --------

IF /I "%FTIME:~17,1%" == "P" (

 IF %fhr% LSS 10 (

  SET /A fhr=%fhr:~1,1% + 12

 ) ELSE (

  IF %fhr% LEQ 11 (

   SET /A fhr=%fhr% + 12

  )

 )

)

IF /I "%FTIME:~17,1%" == "A" (

 IF %fhr%==12 (

  SET fhr=00

 )

)
 

:: +*************************************+

:: | This is where the files are deleted |

:: | Change the ECHO command to DEL to   |

:: | delete. ECHO is used for test.      |

:: +*************************************+

if /I %yyyy%%mm%%dd% GEQ %fyyyy%%fmm%%fdd% (

 IF /I 1%hr%%mn% GEQ 1%fhr%%fmn% (
 

  DEL %FileName%
 

 )

)

ECHO Calcdate=%yyyy%%mm%%dd%%hr%%mn%
 

set temp=

set fyyyy=

set fmm=

set fdd=
 

:EXIT

Open in new window

0
 
LVL 38

Expert Comment

by:Shift-3
ID: 24140851
Doing math with dates and times is much easier in vbscript than in batch.
 
 Paste the script below into a text file with a .vbs extension.  Customize the value of the strFolder variable on line 1 with the location of the folder containing the files.  Customize the value of the strArchive variable on line 2 with the folder to move the files into.

Running the script will move any .pdf files which are over 20 minutes old.


strFolder = "D:\Documents\Public\Scans"

strArchive = "D:\Documents\Public\Scans\archive"

strExt = "pdf"

intMinutes = 20
 

On Error Resume Next
 

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFolder = objFSO.GetFolder(strFolder)
 

For Each objFile in objFolder.Files

    strFileExt = objFSO.GetExtensionName(objFile.Path)

    

    If LCase(strFileExt) = LCase(strExt) Then

        intAge = DateDiff("n", objFile.DateLastModified, Now)

        

        If intAge > intMinutes Then

            strBackup = strArchive & "\" & objFile.Name

            

            If objFSO.FileExists(strBackup) Then

                objFSO.DeleteFile strBackup, True

            End If

            

            objFile.Move strBackup

        End If        

    End If

Next

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:roadnrail
ID: 24141771
That works great.  I showed it to my boss and he doesn't want it to archive now.  So, how can I get rid of that?  Would I just need to set the strArchive equal to the same path as the strFolder?
0
 

Author Comment

by:roadnrail
ID: 24142237
or modify this line:
        If intAge > intMinutes Then
            strBackup = strArchive & "\" & objFile.Name

to read this:

        If intAge > intMinutes Then
            strBackup = strFolder & "\" & objFile.Name
0
 
LVL 38

Accepted Solution

by:
Shift-3 earned 250 total points
ID: 24142809
This revision will echo the names of the files to be deleted.  Once you have tested it successfully, remove the apostrophe from line 18 to execute the delete commands.


strFolder = "D:\Documents\Public\Scans"

strExt = "pdf"

intMinutes = 20
 

On Error Resume Next
 

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFolder = objFSO.GetFolder(strFolder)
 

For Each objFile in objFolder.Files

    strFileExt = objFSO.GetExtensionName(objFile.Path)

    

    If LCase(strFileExt) = LCase(strExt) Then

        intAge = DateDiff("n", objFile.DateLastModified, Now)

        

        If intAge > intMinutes Then

            WScript.Echo objFile.Path

            'objFile.Delete True

        End If        

    End If

Next

Open in new window

0
 

Author Comment

by:roadnrail
ID: 24147448
The first one worked great and deleted the files it should have AND archived the older files; which I thought my boss wanted.

He changed his mind so we don't need to archive the files anymore and just need to delete the files older than 20 minutes.

Thanks for the help and I'll test this out now.


WORKS GREAT.  thanks so much for the speed and quality of your assistance.
0
 

Author Closing Comment

by:roadnrail
ID: 31569944
Great job.  Speedy and insightful help.  I really appreciated his assistance in this issue.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

895 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now