Link to home
Start Free TrialLog in
Avatar of Justin Collins
Justin CollinsFlag for United States of America

asked on

Command Line to check for file name version/number and then update

Should be a pretty easy one...  Looking for a command line that will look at a filename(ex. 1001.txt) and if it's <1001 then it will copy the three updated files, and then update the file name/version.  Also, I want to keep this a batch file.  
Avatar of Bill Prew
Bill Prew

Don't understand what you are after yet.  Where will the 1001.txt file be located, and are there multiple files to be checked?  And where are the "three updated files", don't understand what that means.  And what updates to the name / version need to be done.

~bp
Here's the basic BAT code to get just the name of the file, and check it against 1001, and take action.  But I'm not clear what action is to be taken, or where the name of the file comes from?

for %%A in ("1001.txt") do (
  if %%~nA LSS 1001 (
    echo Less than 1001
  ) else (
    echo Not less than 1001
  )
)

Open in new window

~bp
Are the version number files always 4 digits, i.e. is the one before 1000.txt 999.txt ?

It would be better if you had a file which contained the version number, e.g.

version.txt:
1001

But if not possible then we can perhaps look for ????.txt in the relevant dir and look for the most recent file matching that pattern?  Something like this maybe:

@Echo off
set NewVersion=1001
set VersionDir=c:\somedir\with\versionfile\in
set LookFor=????.txt
Set Sourcedir=C:\sourcedir
set DestDir=c:\destdir

set OldVersion=0
for /f "delims=" %%A in ('dir /b /a-d /o-d"%VersionDir%\%lookfor%"') do (
  set OldVersion=%%~nA
  goto :next
)
:next
if %OldVersion%==0 (
  echo There is no old version no. found
  echo Do whatever it is you want to here
  exit /b
) ELSE (
  echo Found version number file %OldVersion%
)

if %OldVersion% LSS %NewVersion% (
  echo This version needs updating
  copy "%sourcedir%\file1.txt" "%destdir%"
  copy "%sourcedir%\file2.txt" "%destdir%"
  copy "%sourcedir%\file3.txt" "%destdir%"
  del "%VersionDir%\%OldVersion%.txt"
  echo %date% %time% > "%VersionDir%\%NewVersion%.txt"
) ELSE
  echo This has the latest version already
)
Sorry Bill, typing there for a while clearly..

Steve
Avatar of Justin Collins

ASKER

@Bill-I just made up that filename.  It could be anything that's easiest.
Source Directory: \\server\package\version
Destination Directory C:\Program Files\Package\Version
Version file location: C:\Program Files\Package\Version\version.txt(or whatever is best)

@dragon-it-Putting the version info into the text file would be fine.

So, is the idea that you are placing some files (three maybe?) in the "\\server\package\version" and then want to run a BAT script that will determine if they are a newer "version" than the files in "C:\Program Files\Package\Version", and if so copy them over and update the versions "file" in the destination folder to the source folder version number?

If so then it seems like you would want a "version file" in both the source and destination folders to compare against each other, no?

Also, do you really need version numbers, or would it be easier just to use XCOPY /D and it will only copy files if they are newer on the source folder?

~bp
OK in which case:

@Echo off
set NewVersion=1001
set VersionFile="C:\Program Files\Package\Version\version.txt"
Set Sourcedir=\\server\package\version
set DestDir=C:\Program Files\Package\Version

set OldVersion=0
if exist %VersionFile% for /f "delims=" %%A in ('type %VersionFile%') do set OldVersion=%%A

if %OldVersion%==0 (
  echo There is no old version no. found
  echo Do whatever it is you want to here
  exit /b
) ELSE (
  echo Found version number %OldVersion%
)

if %OldVersion% LSS %NewVersion% (
  echo This version needs updating
  copy "%sourcedir%\file1.txt" "%destdir%"
  copy "%sourcedir%\file2.txt" "%destdir%"
  copy "%sourcedir%\file3.txt" "%destdir%"
  echo %NewVersion% >%VersionFile%
) ELSE
  echo This has the latest version already
)
Crossed again there with Bill.

xcopy /d might certainly be a better idea.... BUT if the files get updated on the local machine for any reason they could end up newer than your server files and therefore not get updated.

My little script above can be customised in the set lines for different paths / version file.
If no version file is found then "oldversion" is 0
If the version file is found then the first line of that file is compared with the "newversion" variable.

If it is less than this then the 3 x copy commands are ran and the version number file updated with the new version number.

Steve
Okay, if you do need or want to go with a version approach then here's my thought.  You can use a file in the folder name xxxx.ver where xxxx is the current version number of the files in that folder.  When the script runs it will look for the *.ver file from both source and destination folders, and compare them.  If a newer version is available then it will copy the files over.

@echo off
Set BaseDir=\\server\package\version
set DestDir=C:\Program Files\Package\Version

REM Get the version from the base folder (distribution)
set BaseVer=0
for %%A in ("%BaseDir%\*.ver") do set BaseVer=%%~nA

REM If no version file in base folder can not continue
if %BaseVer% EQU 0 (
  echo No version file in base directory.
  exit /b
)

REM Get the version from the dest folder (target)
set DestVer=0
for %%A in ("%DestDir%\*.ver") do set DestVer=%%~nA

REM If a new version is found, copy the new files over
if %BaseVer% GTR %DestVer% (
  echo Updating from version [%DestVer%] to [%BaseVer%]
  del /Q "%DestDir%\*.ver"
  copy /Y "%BaseDir%\*.*" "%DestDir%" > NUL
)

Open in new window

~bp
It keeps erroring out.  I think it's here:
if exist %VersionFile% for /f "delims=" %%A in ('type %VersionFile%') do set OldVersion=%%A

it keeps saying %%A not expected at this time.
Try changing that line to this:

if exist %VersionFile% (for /f "delims=" %%A in ('type %VersionFile%') do set OldVersion=%%A)

~bp
the XCOPY idea sounds super simple and easy, but I'm just worried about if/when the filenames that I'm updating change, which they are going to shortly.  Moving from Test to Live environment.
Sorry Bill-that didn't work either...  
Okay, likely not that line then.  Can you post up your actual script you are running, as well as the output generated.  

~bp
also, the script doesn't make it all the way until the pause.  It flashes and then exits.  
Yes, likely due to an error.  I'm assuming this is Steves (dragon-it) you are working with, not my suggestion?  And still need to see your actual code to debug a bit.

~bp
It's working.  Was missing a ( after the last ELSE.  :)
ASKER CERTIFIED SOLUTION
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Was just about to say that.... had retyped that bit of code from my tested version here to change the echoed message, doh!

If you want there being no old version to also trigger a copy then remove the pause and exit /b lines then it will continue to trigger a new version being copied.

thanks

Steve
Doesn't Steve's approach imply you have to edit the BAT file to put in the new version number every time you want to roll out a new versions.  This seems a little harder to manage than the approach I had proposed.  Thoughts?

~bp
Bills is certainly neater... mine grown from initial thoughts before we had the facts (tut tut, should have done it properly :-)

Mine specifially copies three files.... but could easily change to *.* or whatever if needed.

Can of course change mind to pickup version no. from file too but that's just less neat copy of Bill's then and thats someone else who does that...

Steve

And FWIW, I don't think either approach does a good job of dealing with file names that can change.  I'd want to understand a little better what would be desired in that case.  So let's say today the base folder contains:

file1.txt
fiel2.txt
file3.txt

and the dest folder contains:

file1.txt
fiel2.txt
file3.txt

and we are "in sync".  What if the next versions files looked like:

fiel2.txt
file3.txt
file4.txt

should file1.txt be deleted from the dest folder a part of the sync?

~bp
Yes some more info. please.  Frankly a single robocopy command might be all that you need.

Steve
/MIR

~bp
This was exactly what I was looking for.  I am planning on running this as a login script.  I wanted to be able to add other commands in as well.  It's not hard to put in a del command to delete the old files.  Also, I want to copy only the specific file because there are going to be other files in there that only get copied for certain workstations.  So a *.* wouldn't work.  I am also copying shortcuts to the desktop and Start folder.
Ok, good luck with it and spread them points out!
Seems like you may have done more work here than you needed to, but I think you probably have enough to keep you moving.  I'm thinking that you would never go BACKWARDS with a version number, so checking for a value and doing a numeric compare seems like extra work.

You could just keep the VER file in the dest folder and when you want to roll out a new version bump the name of it in the source folder.  Then the script becomes as simple as something like:

@Echo off
set Version=1001
Set Sourcedir=\\server\package\version
set DestDir=C:\Program Files\Package\Version

if not exist "%DestFolder%\%Version%.ver" (
  del /Q "%DestFolder%\*.ver"
  REM do update logic here, including copying files 
)

Open in new window

~bp
i suppose only reason might be if v1000 needs different files to v1005 say
==> i suppose only reason might be if v1000 needs different files to v1005 say

I don't think that conflicts with what I'm saying.  What I've heard so far is that if the version number changes we're going to take some action to "upgrade" to the new version, and the script will have the logic to do that.

Nothing that we discussed so far handles the case of different action being needed to go from say v1000 to v1001 versus v1000 to v1002.

~bp
v.true!
Looking at the question and example filename (1001.txt), wouldn't it suffice to do the following:
for %%a in (1001.txt) do (
  if %%~na lss 1001 (
    :
    : Code goes here to copy your files
    :
  )
)

Open in new window

As you can see, I'm just drawing on the fact the filename is numerical.

Or am I over-simplifying it somewhat?
@Paul - If you know the filename is 1001.txt you might aswell do:

@echo off
exit /b

as you know that 1001 is not < 1001

http:#37063498 from Bill
and with wrapper to look for the file http:#37063555
dragon-it

I didn't actually mean '1001.txt' in it's literal sense so allow me to define what I meant by the following:
for %%a in ("%filename%") do (
  if %%~na lss 1001 (
    :
    : Code goes here to copy your files
    :
  )
)

Open in new window

As you can see, the value in %filename% is unknown...
The linear approach would be as follows:
for %%a in ("%filename%") do set fn=%%~na

if %fn% lss 1001 (
  :
  : Code goes here to copy your files
  :
)

Open in new window

But I'm probably mile off track anyway... Was just a thought!
Paul,

As Steve mentioned, that's essentially what I suggested at the begining in http:#37063498.

~bp
billprew

apologies... yes, i see your reply. very similar...

i responded because this question popped up as an alert. I see that's nolonger the case though...

Thanks "puter_geek".  There were quite a few other suggestions there of simpler method but if that is what you are after...
Steve