If file exists and is older

I'm trying to create a batch file or vbs script that will check to see if there is a new version of a map file on our server, if there is a newer version then I would like it to copy the file off the server to a local c drive, then rename the old file on the local drive to mapfile.old. I also need the batch file or script to ensure I only have one version of the mapfile.old. So at any given time there should only be two files on the local c drive, mapfile.map and mapfile.old. I Know that I can use xcopy to copy the file only if the source is new than the destination but I'm out of thoughts on how to make the rename and deletion only occur if a new file was copied.  The xcopy I am using look like this: xcopy \\x\ManifoldUserUpdates\FisherUS\Maps\nolinkcwpkey.m
ap "c:\fisherus\program files\fisherus\maps"
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewCommented:
So, you only want to copy the .MAP file to the .OLD file when a new .MAP file exists on the server, and will be copied down via XCOPY?

How large is the .MAP file, one approach would be to just always copy the existing .MAP to the .OLD (overwriting), but that is a bit less efficient.

cwpowerAuthor Commented:
Correct the local .map should only be renamed to .old if a new file is copied down. I always need the newest file to have its original name as my end users have shortcut to it on their desktop. The file is currently copied down via xcopy but if there is a more efficient way to do so that would be ok. The file is about 1 GB in size and this batch will run on around 20 computers.
Dan CraciunIT ConsultantCommented:
Can't test it now, but this should work:

$localCopy = Get-ChildItem "\path\to\file.map"
$localBackup = Get-ChildItem "\path\to\file.old"
$networkCopy = Get-ChildItem "\\path\to\file.map"

$localCopyTime = $localCopy.LastWriteRime
$networkCopyTime = $networkCopy.LastWriteTime

if ($networkCopyTime -gt $localCopyTime) {
  Copy-Item $localCopy -Destination $localBackup
  Copy-Item $networkCopy -Destination $localCopy

Replace the paths with your paths.
It simply compares last modified date from the file on the server with the local copy.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Dan CraciunIT ConsultantCommented:
Yes, it's better to simply delete and rename the backup, so you can use instead of

Copy-Item $localCopy -Destination $localBackup

Remove-Item $localBackup
Rename-Item $localCopy $localBackup
Steve KnightIT ConsultancyCommented:
Various ways, one of which I thought of involves checking the source and dest time stamps and file sizes and comparing:


@echo off
set source=\\x\ManifoldUserUpdates\FisherUS\Maps
set dest=c:\fisherus\program files\fisherus\maps
set filename=nolinkcwpkey

for %%A in (%source%\%filename%.map) do set sourcefile=%%~tA#%%~zA
for %%A in (%dest%\%filename%.map) do set destfile=%%~tA#%%~zA

echo Source file is %sourcefile%
echo Dest file is %destfile%

if "%sourcefile%"=="%destfile%" (
  echo Nothing to change
) ELSE (
  echo copying new version
  IF EXIST "%dest%\%filename%.old" del "%dest%\%filename%.old"
  IF EXIST "%dest%\%filename%.map" rename "%dest%\%filename%.map" "%filename%.old"
  xcopy "%source%\%filename%.map" "%dest%" /y
Plain batch; for security reasons, it only deletes the original after the file has been successfully copied. If it fails, it will leave with errorlevel 1:
@echo off
set SourceFile=\\Server\share\mapfile.map
set TargetFile=D:\temp\mapfile.map
for %%a in ("%SourceFile%") do (set SourceDate=%%~ta)
for %%a in ("%TargetFile%") do (
	set TargetDate=%%~ta
	set TargetFolder=%%~dpa
	set TargetName=%%~na
	set TargetExt=%%~xa
if "%SourceDate%"=="%TargetDate%" (
	echo Both files have the same time stamp of %SourceDate%; leaving.
	exit /b 1
REM When we're here, the file dates differ. Backup the old file and copy the new one from the serverlist
echo The files differ:
echo Source date: %SourceDate%
echo Target date: %TargetDate%
echo Copying '%SourceFile%', this might take a while ...
copy "%SourceFile%" "%TargetFolder%%TargetName%.new"
if errorlevel 1 (
	echo Could not copy the source file '%SourceFile%'; leaving.
	if exist "%TargetFolder%\%TargetName%.new" del "%TargetFolder%\%TargetName%.new"
	exit /b 1
REM When we're here, we now have three file versions: .ext, .new, .old
if exist "%TargetFolder%%TargetName%.old" del "%TargetFolder%%TargetName%.old"
if exist "%TargetFile%" "%TargetName%.old" ren "%TargetFile%" "%TargetName%.old"
ren "%TargetFolder%%TargetName%.new" "%TargetName%%TargetExt%"
echo Done.

Open in new window

cwpowerAuthor Commented:
Thanks to everyone for the help! Multiple solutions are viable and work as intended.
For the sake of completeness, I just saw an error in line 29 (you probably found it, but if somebody else finds it useful) in http:a39677840; the first "%TargetName%.old" should of course not be there and needs to be removed:
if exist "%TargetFile%" "%TargetName%.old" ren "%TargetFile%" "%TargetName%.old"
Dan CraciunIT ConsultantCommented:
oBdA, your solution was the only one that checked to see if the file was actually copied from the server. Good job.
Steve KnightIT ConsultancyCommented:
No problem, always lots of ways to solve things.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.