Date Comparison for Batch file using XCOPY

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
CoreSys
Posts: 2
Joined: 13 Jun 2013 07:55

Date Comparison for Batch file using XCOPY

#1 Post by CoreSys » 13 Jun 2013 08:08

I have created a batch file the runs xcopy comparing files from the server to the local machine by date. Then once those files are copied the batch file next runs a local executable the moves those files to a particular device. This works great.

I want to add the .bat file to the startup for all users, again not a problem. However, if there are no new files I do not want to run the copy command or the executable file. So what I would like to do is compare the file dates and if the dates are not 'newer - older' then just end the batch, if the date of the server file is newer then continue with the copy to the local drive.

Here is what I have so far and I'm sure I'm completely wrong.

If Exist "\\<servername>\Marketing Live\Receipt\*.*" C:\AXIDir /d
GoTo RCPTRUN
Echo "No new files to copy"
GoTo End


:RCPTRUN
xcopy /y /d "\\<servername>\Marketing Live\Receipt\*.*" C:\AXIDir

My thinking was an IF/THEN statement but I know that xcopy does not work in that fashion.

Thanks for your help.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Date Comparison for Batch file using XCOPY

#2 Post by foxidrive » 13 Jun 2013 09:02

This checks if the server is accessible
It then uses the xcopy command to check for any copyable files using the /L switch.
That will print the path\filenames and the FIND command detects any "\" characters.
The && is a conditional operator that will launch on a zero errorlevel and will only run the following xcopy command when there are files to copy.


Code: Select all

If Exist "\\<servername>\Marketing Live\Receipt\" (
  xcopy /y /d /L "\\<servername>\Marketing Live\Receipt\*.*" C:\AXIDir\ |find "\" >nul && (
    xcopy /y /d "\\<servername>\Marketing Live\Receipt\*.*" C:\AXIDir\
    REM launch your executable here
   )
)

CoreSys
Posts: 2
Joined: 13 Jun 2013 07:55

Re: Date Comparison for Batch file using XCOPY

#3 Post by CoreSys » 13 Jun 2013 09:45

Thank you. That worked.

Post Reply