Batch file renaming using FILEVER

April 4, 2012 at 09:58:34
Specs: Windows XP
I posted this question once in a slightly different format and got 1 reply that didn't work. I'm reposting this in hopes that someone can help me de-bug this.

I can copy/paste the code into a CMD prompt and it exucutes no problem. If I use the same code in a Batch file it hangs up on the very first command and then gives me a File not Found error. However the reason it can't even find the file is because it already renamed the file with a space at the end of the name, before the .exe. The first command of the Batch file isn't even calling to rename the file (yet), but it keeps getting re-named.????

General overview, I want to use FILEVER to compare version of 2 different files, 1 local 1 on a server. If the one on the serve is newer, then rename the local file with the version number and then copy the one from the server to the local machine and launch the program.

Any advice is welcome.

:BEGIN
FOR /F "tokens=5" %%D IN ('FILEVER "C:\Program Files\GE\Start Stop\Start Stop Dryer.exe"') DO SET _ver1=%%D


FOR /F "tokens=5" %%E IN ('FILEVER "\\3.67.38.12\share\AP1_Library\Executables\Start Stop\Start Stop Dryer.exe"') DO SET _ver2=%%E

IF %_ver2% GTR %_ver1% GOTO :UPDATE
IF %_ver2% LEQ %_ver1% GOTO :LAUNCH

:UPDATE
REN "C:\Program Files\GE\Start Stop\Start Stop Dryer.exe" "Start Stop Dryer %%_ver1%%.exe
COPY "\\3.67.38.12\share\AP1_Library\Executables\Start Stop\Start Stop Dryer.exe" "C:\Program Files\GE\Start Stop"
GOTO :LAUNCH

:LAUNCH
START "Start Stop Dryer" /NORMAL "C:\Program Files\GE\Start Stop\Start Stop Dryer.exe"
GOTO :END

:END


See More: Batch file renaming using FILEVER

Report •

#1
April 4, 2012 at 10:09:06
Any advice is welcome.
Post the output of FILEVER. It isn't in Win7, and I'm too lazy busy to log into a server box just to find out what the command looks like.

How To Ask Questions The Smart Way


Report •

#2
April 4, 2012 at 10:26:33
First FILEVER:
D:\>FOR /F "tokens=5" %D IN ('FILEVER "C:\Progra
m Files\GE\Start Stop\Start Stop Dryer.exe"') DO SET _ver1=%D

D:\>SET _ver1=1.0.0.0

Second FILEVER:
D:\>FOR /F "tokens=5" %E IN ('FILEVER "\\3.67.38
.12\share\AP1_Library\Executables\Start Stop\Start Stop Dryer.exe"') DO SET _ver
2=%E

D:\>SET _ver2=1.0.1.2


OR...
manually typing it in CMD prompt gives me these full results, just the FILEVER by itself....
C:\>filever "c:\program files\ge\start stop\start stop dryer.exe
--a-- W32i APP ENU 1.0.0.0 shp 1,083,904 03-07-2012 start stop dryer.exe


Report •

#3
April 4, 2012 at 10:50:16
See, these are the things that make me want to read your OP.

If I use the same code in a Batch file it hangs up on the very first command and then gives me a File not Found error.
What's giving the error? Filever or CMD? Are you running the commands in the same environment as the script? If I Filever's not in Win7, it might not be in the other workstation versions of Windows. If not, you might try a different language. (VBScript.)

The first command of the Batch file isn't even calling to rename the file (yet), but it keeps getting re-named.????
If neither IF test passes, execution falls though into your :UPDATE code. Remember, labels do not denote functions, and they do not stop execution. They're just targets of GOTO and CALL.

Note that CMD will compare file versions as text, so "1.9.0" will be greater than "1.11.0".

How To Ask Questions The Smart Way


Report •

Related Solutions

#4
April 4, 2012 at 12:50:53
If I use the same code in a Batch file it hangs up on the very first command and then gives me a File not Found error.
What's giving the error? Filever or CMD? Are you running the commands in the same environment as the script? If I Filever's not in Win7, it might not be in the other workstation versions of Windows. If not, you might try a different language. (VBScript.)

Ok, if I copy the command string from my batch.bat file and remove the extra % signs and past it into a <Start Menu> <Run> <CMD> (MS-DOS Command Prompt) and hit "enter" the FOR command executes with no errors and sets my variable that I can then call by simply typing %_ver1% or %_ver2%. Regardless of which FOR I use, the variable is set and can be called in the CMD prompt.
If I run these same commands in the Batch.bat file, it pauses for about 60-120 seconds and then all of a sudden I get "File not found" scrolling across my screen over and over. However in explorer I can see that the file has been renamed to "Start Stop Dryer .exe". There is simply a space before the .exe.
If I click inside the CMD window and stop the scroll the batch file actually continues, but of course starts to error out because the file has already been renamed. Even if I insert a PAUSE after the first FOR command, it renames the file, which should be impossible because it hasn't even gotten to the REN command in my script.


The first command of the Batch file isn't even calling to rename the file (yet), but it keeps getting re-named.????
If neither IF test passes, execution falls though into your :UPDATE code. Remember, labels do not denote functions, and they do not stop execution. They're just targets of GOTO and CALL.

Like I said, I can insert a PAUSE in the sccript right after the first FOR command and my file is still being renamed without a REN command being called yet.

Note that CMD will compare file versions as text, so "1.9.0" will be greater than "1.11.0".
I was not aware of this, so far using several versions of this particular executable I have not had this problem. It has handled the comparison correctly. If this might be a problem in the future, then this code may not work.

As for using VBS scripts... the s/w developer I am working with would rather use a simple batch file to acheive this. He's writing the programs in LabView and could easily incorprate a VBS script but doesn't want to. Why not? He hasn't given me a good answer yet either. :-)

Thanks so much for your help Razor. I hope this helps to clarify... I really really appreciate your advice, I've been stuck on this stupid script for a few days now. It's really frustrating that I can manually use a CMD prompt and execute all of these commands, but not in a Batch file. I'm definitely missing something.


Report •

#5
April 4, 2012 at 13:46:22
This isn't the entirety of your script, is it? If it is, this means you're leaving echo on. Which line throws the error?
Are you sure you're running the correct batch.bat?
EDIT: Are you running the manual command in the same directory as batch.bat?
Is it really called batch.bat, and not FOR.bat, FILEVER.bat, SET.bat, IF.bat, COPY.bat, or START.bat? I'm asking because the exact situation you're describing should be impossible.
I still don't see why %%_ver1%% and %%_ver2%% aren't %_ver1% and %_ver2%.

It has handled the comparison correctly. If this might be a problem in the future, then this code may not work.
The comparison should work as long as the length of the string never changes. If that's not a concern, you're good.

How To Ask Questions The Smart Way


Report •

#6
April 5, 2012 at 07:38:40
I found the problem. As soon as you mentioned running it manually from the same starting directory as the batch file I figured out the fix.

I added the line...
CD C:\
..... at the beginning of my script, as well as @echo off and now it works perfectly every single time via the batch file.

Incidentally, if you are running windows 7 and want to run FILEVER on it, simply copy a version of FILEVER.exe to “C:\Windows\System32” from an XP machine to Windows 7 machine and FILEVER will run on Win7 no problem. I have also found that some of our laptops running XP are missing FILEVER as well. I took the liberty of adding it to our server and just adding a COPY command to my script to ensure it’s always on the machine that tries to run my script.

Having said all that…. THANK YOU THANK YOU! If you hadn't mentioned the “starting directory” I may have never found it and we may have gone on back and forth all day. :-)

Since someone may want a "Best Answer" on this one later, I will mark this post as best answer. Here's the working script in its entirety with a few changes to account for different IP’s in the lab versus my desktop PC….

I also removed the double %% in the REN command due to it's usage in this instance. You only need one % when calling the set _ver1. However in the FOR line double %% is required for the batch file to work. As is the case in every MS-DOS batch file I have ever written.


:BEGIN
@ECHO off
CD C:\
COPY /Y "\\192.168.1.12\share\AP1_Library\Installation Files\Windows XP\filever.exe" "C:\Windows\System32"
FOR /F "tokens=5" %%D IN ('FILEVER "C:\Program Files\GE\Start Stop\Start Stop Dryer.exe"') DO SET _ver1=%%D
FOR /F "tokens=5" %%E IN ('FILEVER "\\192.168.1.12\share\AP1_Library\Executables\Start Stop\Start Stop Dryer.exe"') DO SET _ver2=%%E
IF %_ver2% GTR %_ver1% GOTO :UPDATE
IF %_ver2% LEQ %_ver1% GOTO :LAUNCH
:UPDATE
REN "C:\Program Files\GE\Start Stop\Start Stop Dryer.exe" "Start Stop Dryer %_ver1%.exe
COPY "\\192.168.1.12\share\AP1_Library\Executables\Start Stop\Start Stop Dryer.exe" "C:\Program Files\GE\Start Stop"
GOTO :LAUNCH
:LAUNCH
START "Start Stop Dryer" /NORMAL "C:\Program Files\GE\Start Stop\Start Stop Dryer.exe"
GOTO :END
:END
@ECHO on

THANKS AGAIN Razor!! Really appreciate you helping me and steering me in the right direction. :-)


Report •

Ask Question