KAE
KAE

Reputation: 855

Batch file to copy all new files except the most recent

I would like to write a batch file containing DOS commands (unfortunately perl or another language is not an option) to do the following task.
In one directory (c:\MyData\Directory1) there are the following files:
File2.txt
File2.dat
FileA.bin
FileQ.bin
FileC.bin
File8.bin
File2.bin
These files all have different creation dates. The most recently created *.bin file is File2.bin in this example, but it could be any randomly named *.bin file.

In another directory (c:\MyData\Directory2) there are the following files:
File2.txt
File2.dat
FileA.bin
FileQ.bin

Here is what I want to do:
Copy all files with the extension *.bin in Directory1 that do not already exist in Directory2 except for the most recently created *.bin file in Directory1. So the only files that should be copied into Directory2 are:
FileC.bin - Copy because it's a bin file that's not yet in Directory2
File8.bin - Copy because it's a bin file that's not yet in Directory2

The following files should not be copied into Directory2:
File2.txt - Wrong extension so don't copy it
File2.dat - Wrong extension so don't copy it
FileA.bin - Already exists in Directory2 so don't copy it
FileQ.bin - Already exists in Directory2 so don't copy it
File2.bin - Most recent *.bin file so don't copy it

Thanks for any help!

Upvotes: 1

Views: 2206

Answers (3)

Chris
Chris

Reputation: 1483

I don't have Robocopy on my machine, otherwise I would do a /? and tell you. But as I recall it has many more capabilities (especially wrt timestamps). It's a windows tool. http://en.wikipedia.org/wiki/Robocopy

Upvotes: 0

KAE
KAE

Reputation: 855

@echo off
@rem     Sorry for excessive commenting - I am a batch file newbie
@rem     Batch file will not work if there are spaces in names of directory or copied files
@rem     Next line allows for/do loop to work correctly
setlocal enabledelayedexpansion

@rem     Make temporary file that lists files from newest to oldest
DIR /o-d /b c:\temp\Directory1\*.bin > FileList.txt

@rem     Counter will be used to avoid copying newest file which is listed first
set /A Counter=1

@rem     Read in names of all files with chosen extension in the first directory
@rem     Names will be stored in the variable %%a
for /F "delims=" %%a in (C:\temp\FileList.txt) do (

@rem     Increment the counter
    set /A Counter+=1
@rem     Only copy files that are not the most recent one, so Counter>1
@rem     Requires the exclamation points because this is a string not number comparison
    if !Counter! gtr 1 (
@rem     If the file does not already exist in Directory2, copy it
            if not exist C:\temp\Directory2\%%a (
                    echo Copying C:\temp\Directory1\%%a to C:\temp\Directory2\%%a
                    copy C:\temp\Directory1\%%a C:\temp\Directory2\%%a
            )
    )
)
@rem     Remove the temporary file
del FileList.txt

Upvotes: 3

Philip Kelley
Philip Kelley

Reputation: 40319

You can use DIR *.bin /o-d /b > Files.txt to get a list of bin files ordered most recent to last. Do this on both folders (to separate output files), and then set up a FOR loop, maybe two nested FOR loops, to go through the two files, pick out the ones to copy (with special handling for the first one in the date-ordered list), and copy them from within the loop. Silly tricks would be done with setting the Attribute setting and then using XCOPY /M to copy them all at the same time, but that seems overly fussy.

I've always found FOR loops to be cantankerous beasts, and if you can find a non-batch-file way, or some form of third-party plug in to help, you'd be ahead of the game.

Upvotes: 0

Related Questions