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.
Answer
To simplify your question (as I understand it), you want to:
- Look at the source directory and get the latest file
- Look at the destination directory and get latest file
- compare the time stamps of these two files
- If the source directory file (step 1) is newer than the destination directory file (step 2), copy it to the destination directory.
- 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
)
No comments:
Post a Comment