Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I am trying to write a batch script that can find the files created/modified/newer since yesterday and/or any specific day from today in a particular folder. Then those files need to be copied to different location.

I tried to use the forfiles command but my XP doesn't have that command.

Any help would be awesome!

share|improve this question
1  
The utility forfiles is not included with Windows, it is from the Windows Server 2000 Resource Kit. You can also download the executable itself from the Petri site. –  jscott Nov 3 '11 at 19:16
    
If you're willing to install cygwin, you can use find /cygdrive/c/your/folder -ctime 1 | xargs mv -t /cygdrive/c/new/folder to perform all that (files changed within the last 24 hours; there are other time options if you need to be more precise). –  Kevin Nov 3 '11 at 20:01
    
hey kevin - thanks for this! even though i havent worked with cygwin and dont know what it is, i will give it a shot. –  newbie Nov 3 '11 at 20:28
    
thank you scott! –  newbie Nov 4 '11 at 14:26
1  
If installing something as extensive as cygwin is a possibility, you should look into using powershell as well. –  RobW Nov 4 '11 at 15:56
show 2 more comments

2 Answers 2

You can use Horst Schaeffer's excellent wasfile.exe tool:

for /r c:\particular_folder %i in (*.*) do @wasfile.exe %i created after Today-1 > nul && @copy %i f:\different_location

You can change created and modified per your need:

WasFile, ver. 2.2 (c) 2006-2007, Horst Schaeffer
compares ..
    the time&date of two files (or directories),
    the date only, time ignored
    the date of a file with TODAY-n (days)
    the time&date of a file with NOW-n (minutes)
Examples:
    WasFile this.zip created before that.zip
    WasFile this.zip modified after today-8
Syntax:
    WasFile File1 [Stamp] [not] before|after|sametime File2 [Stamp] [Option]
    WasFile File1 [Stamp] [not] before|after|sametime today-n [Option]
    WasFile File1 [Stamp] [not] before|after|sametime now-n
Stamp is either:
    created, modified (default) or accessed
    (by default second stamp = first stamp)
Options to compare date only, ignore time:
    /DateLocal or /DateUTC
    (if TODAY is used, default is /DateLocal)
Result by errorlevel:
    0: true, 1: false, 255: error (message to STDERR)

by the way when you put (.), does it mean look for any format/kind of the file? can i just put *.csv instead to search csv file only?

I think you mean (*.*)? Yes, this tells you what set you are interested in. *.csv would isolate just the csv files.

also why are you using @wasfile.exe, nul, @ copy and switch %i (two places)

You might benefit from reading some web sites for background:
http://www.netikka.net/tsneti/http/tsnetihttpprog.php#batch
http://www.netikka.net/tsneti/info/tscmd.php

Google batch tutorial for more sites.

In a batch file, @command means don't echo the command line to the screen. Otherwise, you will see both what you asked for as well as what the result was. The && means 'process what follows if command is successful' and determines this by errorlevel. If I wanted 'was not successful' I would have used ||.

In this case, you read the code as: Is the file newer than my target date, and if yes (&&) copy it to some location. Don't show me any output from my command ">nul"
Ok?

share|improve this answer
    
thank you very much rob! i will give it a shot. by the way when you put (.), does it mean look for any format/kind of the file? can i just put *.csv instead to search csv file only? also why are you using @wasfile.exe, nul, @ copy and switch %i (two places)? –  newbie Nov 4 '11 at 16:45
    
I'll append my answer to my post above –  RobW Nov 4 '11 at 19:33
add comment

This is easily achievable using VBScript, with its GetFile() and DateDiff() functions. VBScript is native to XP.

Option Explicit
On Error Resume Next
Err.Clear

processFolder "c:\temp"

WScript.Quit


Function processFolder( strPath )

    On Error Resume Next

    Dim objFSO
    Dim objFolder
    Dim intRc
    Dim colFiles
    Dim objFile

    intRc = 0

    Set objFSO = CreateObject( "Scripting.FileSystemObject" )
    If Err.Number <> 0 Then
        intRc = -1
        Err.Clear
    Else

        Set objFolder = objFSO.GetFolder( strPath )
        If Err.Number <> 0 Then
            intRc = -2
            Err.Clear
        Else

            Set colFiles = objFolder.Files
            If Err.Number <> 0 Then
                intRc = -3
                Err.Clear
            Else

                For Each objFile in colFiles
                    checkFile strPath, objFile.Name, (24 * 60 * 60)
                Next

                Set colFiles = Nothing
            End If

            Set objFolder = Nothing

        End If

        Set objFSO = Nothing
    End If

processFolder = intRc

End Function



Function checkFile( ByVal strPath, strName, intThreshold )

    On Error Resume Next

    Dim objFSO
    Dim objFile
    Dim intRc
    Dim strFileName
    Dim intModificationPeriodSecs
    Dim objDateLastModified

    intRc = 0
    strFileName = strPath & "\" & strName

    Set objFSO = CreateObject( "Scripting.FileSystemObject" )
    If Err.Number <> 0 Then
        intRc = -1
        Err.Clear
    Else

        Set objFile = objFSO.GetFile( strFileName )
        If Err.Number <> 0 Then
            intRc = -2
            Err.Clear
        Else

            'intRc = objFile.Size
            objDateLastModified = objFile.DateLastModified
            intModificationPeriodSecs = DateDiff( "s", objDateLastModified, Now )

            If intModificationPeriodSecs > intThreshold Then
                WScript.Echo "File [" & strFileName & "] last modified [" & intModificationPeriodSecs & "] seconds ago."
            End If

            Set objFile = Nothing

        End If

        Set objFSO = Nothing
    End If

checkFile = intRc

End Function
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.