0

I'm looking for a way to accomplish this task. I've tried robocopy and writing a batch file, but robocopy doesn't seem to be able to do it (since /MINAGE can't do time--only dates), and writing a batch file to do all this is a little beyond me since it gets somewhat complicated and turns into spaghetti code.

1) Someone uploads a file to a source folder on a server (S-drive).

2) A few workstations check the S-drive periodically (once every 5 minutes).

3) The workstation has one file in a destination folder. If the workstation sees that the S-drive has a file that is newer than the file in destination folder on the workstation, the workstation copies only the newest file from the S-drive to the destination folder. Filenames don't matter here--only the timestamp does.

On the other hand, if the workstation's destination folder has a file with the same or newer timestamp than on the S-drive (even if it's a different file name), it shouldn't copy the file.

4) The workstation runs a separate batch file to rename and process the new file.

1
  • Write it in PowerShell instead of batch. :) Jan 29, 2015 at 19:12

2 Answers 2

1

To simplify your question (as I understand it), you want to:

  1. Look at the source directory and get the latest file
  2. Look at the destination directory and get latest file
  3. compare the time stamps of these two files
  4. If the source directory file (step 1) is newer than the destination directory file (step 2), copy it to the destination directory.
  5. If copied, rename the new file in the destination directory.

Steps 1 and 2 can be performed with a for loop:

set SOURCE_SERVER=\\server\source
set DEST_SERVER=C:\Destination Folder

set SOURCE_LATEST=
set DEST_LATEST=

REM Latest timestamp in the source directory
for /f "tokens=*" %%A in ('dir "%SOURCE_SERVER%\*.*" /b /o:-d') do (
    if not defined SOURCE_LATEST set SOURCE_LATEST=%%~fA)

REM latest timestamp in the destination directory
for /f "tokens=*" %%A in ('dir "%DEST_SERVER%\*.*" /b /o:-d') do (
    if not defined DEST_LATEST set DEST_LATEST=%%~fA)

More help on that here: How do I write a Windows batch script to copy the newest file from a directory?

Step 3: Now you know the latest file in each location. Time to compare their timestamps to see which file is newer. We can use wmic to retrieve a timestamp for each file that can be compared with relational operators: e.g. 20150129113038 (which equals Jan 29, 2015 at 11:30:38) is greater than 20150129112533 (Jan 29, 2015 at 11:25:33).

set DEST_FILE_DATE=
set SOURCE_FILE_DATE=

for /f "tokens=1 skip=1 delims=." %%A in ('wmic datafile where name^="%SOURCE_LATEST:\=\\%" get "Last Modified"') do (
    if not defined SOURCE_FILE_DATE set SOURCE_FILE_DATE=%%A)

for /f "tokens=1 skip=1 delims=." %%A in ('wmic datafile where name^="%DEST_LATEST:\=\\%" get "Last Modified"') do (
    if not defined DEST_FILE_DATE set DEST_FILE_DATE=%%A)

Additional ideas around this last comparison can be found here: How do I compare timestamps of files in a DOS batch script?

Steps 4 and 5: Finally, determine if the latest file is the one on the Source server and copy + rename as needed.

if "%SOURCE_FILE_DATE%" gtr "%DEST_FILE_DATE%" (
    REM copy the source file to the destination and rename it as desired.
    copy "%SOURCE_LATEST%" "%DEST_SERVER%\new file name.ext"
) else (
    REM Wait 5 minutes and try again
    timeout /T 300
    goto :START
)
4
  • Wow, I appreciate all the effort you put into this. Yes, you understood the question perfectly. One wrinkle in the potential solution though--I can't be copying the files around for comparisons. The server and workstation are are in different networks/locations, the files are somewhat large, and bandwidth is limited. Would there be another approach to avoid copying until it's determined that the file at the source location is the newest file?
    – Force Flow
    Jan 29, 2015 at 21:22
  • You can use a checksum to compare files.
    – surfasb
    Jan 29, 2015 at 21:29
  • @surfasb, checksums will tell if you the files are different, but not which file is newer.
    – user387876
    Jan 29, 2015 at 22:18
  • @ForceFlow Updated with timestamp comparison as requested, rather than requiring file copying.
    – user387876
    Jan 30, 2015 at 0:46
0

How about the following batch file

xcopy "\\Source Server\Source Path" "c:\Destination Path" /C /D /Y

of course the " marks are only needed if there are spaces in the names.

It uses UTC time for comparison. (Coordinated Universal Time)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .