Solved

If file exists and is older

Posted on 2013-11-26
10
628 Views
Last Modified: 2013-11-26
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"
0
Comment
Question by:cwpower
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 56

Expert Comment

by:Bill Prew
ID: 39677762
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.

~bp
0
 

Author Comment

by:cwpower
ID: 39677782
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.
0
 
LVL 35

Accepted Solution

by:
Dan Craciun earned 167 total points
ID: 39677795
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.

HTH,
Dan
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39677807
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
0
 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 167 total points
ID: 39677832
Various ways, one of which I thought of involves checking the source and dest time stamps and file sizes and comparing:

Steve

@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
)
0
 
LVL 85

Assisted Solution

by:oBdA
oBdA earned 166 total points
ID: 39677840
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
setlocal
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

0
 

Author Comment

by:cwpower
ID: 39677961
Thanks to everyone for the help! Multiple solutions are viable and work as intended.
0
 
LVL 85

Expert Comment

by:oBdA
ID: 39677999
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"
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39678020
oBdA, your solution was the only one that checked to see if the file was actually copied from the server. Good job.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39678804
No problem, always lots of ways to solve things.

Steve
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question