We help IT Professionals succeed at work.

30 Day Batch File

Medium Priority
445 Views
Last Modified: 2012-05-12
I am trying to write a script that will look at a shared diirectory and pull out any file that hasn't been accessed in the last 30 days.  I have been working with VBscripts, Robocopy, Forfiles, and none of them seem to work correctly for what I am trying to do.  I can't be the only person who is trying to write a script to clean out a folder after 30 days.  I want it to copy the files and folder structure to another location, and then from their I have a script that will delete them.
Comment
Watch Question

Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
Do you actually want to COPY them from the source to destination, or MOVE them?

~bp
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
Seems like ROBOCOPY with the /maxlad:30 option would work, as well as the /S option, did you try that and run into a problem?

~bp
dsackerContract ERP Admin/Consultant
CERTIFIED EXPERT

Commented:
This will list files older than days passed. Execute from a Command Prompt as follows:

cscript //nologo FilesOlderThanDays.vbs folder 30
Option Explicit

Dim mintDays
Dim mstrFolder

Call Get_Args
Call List_Files(mstrFolder, mintDays)

WScript.Quit

'*--------------------------------------------------------------*'
'*      List all files older than the days given.               *'
'*--------------------------------------------------------------*'

Sub List_Files(strFolder, intDays)
    Dim datMax
    Dim objFS
    Dim objFile
    Dim objFiles
    Dim objFolder

    datMax = Now() - intDays

    Set objFS = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFS.GetFolder(strFolder)
    Set objFiles = objFolder.Files

    For Each objFile in objFolder.Files
        If CDate(objFile.DateLastAccessed) < CDate(datMax) Then
              WScript.Echo objFile.Name
        End If
    Next

    Set objFile   = Nothing
    Set objFiles  = Nothing
    Set objFolder = Nothing
    Set objFS     = Nothing
End Sub

'*--------------------------------------------------------------*'
'*    Get the arguments passed to this script.                  *'
'*--------------------------------------------------------------*'

Sub Get_Args
    Dim objArg

    mintDays   = 30
    mstrFolder = "."

    For Each objArg in WScript.Arguments
        Select Case True
            Case IsNumeric(objArg) = True
                 mintDays = objArg
            Case Else
                 mstrFolder = objArg
        End Select
    Next
End Sub

Open in new window

Test your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016
Commented:
Actually, double checking, I think you want the MINLAD swithc rather than the MAXLAD.

/MAXLAD:n :: MAXimum Last Access Date - exclude files unused since n.
/MINLAD:n :: MINimum Last Access Date - exclude files used since n.

So something like:

robocopy "c:\fromdir" "d:\tofolder" /MINLAD:30 /S

You might want to try it with the /L to see what files it thinks it will copy first.

~bp

Author

Commented:
I feel like an idiot...I had the same files in the source and destination so robocopy was skipping them.  I added /IS and it works perfectly.

Thanks for the quick responses.
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
Welcome.

~bp

Explore More ContentExplore courses, solutions, and other research materials related to this topic.