Note that there are some explanatory texts on larger screens.

plurals
  1. POProblems reading simple numeric values from files in Windows batch files
    primarykey
    data
    text
    <p>My colleague and I have been pulling our hair out all day over this.</p> <p>We have a simple Windows batch file. We want it to read from a text file whose file path we are generating programmatically, take the single numeric value in this file, and compare it to a local variable. But we're getting completely inexplicable behavior.</p> <p>The file contains a single scalar number, such as the number 2. Here's the code:</p> <pre><code>ThisAppFlagFileName=foo.txt if not exist "%HOMEPATH%\ourcompanyname\%ThisAppFlagFileName%" ( ECHO do something here ) else ( SET /P InstalledVersion=&lt;"%HOMEPATH%\ourcompanyname\%ThisAppFlagFileName%" ECHO We think the file contains: %InstalledVersion% IF %InstalledVersion% GEQ %ThisVersionInstallDataNum% ( ECHO Version %ThisVersion% of the %ThisAppVisibleName% has already been installed for this user; exiting. GOTO TheEnd ) ) :TheEnd Echo END </code></pre> <p>We keep getting an error reading <code>2 was unexpected at this time.</code> So we inserted some trace message and, just in case the <code>else</code> was problematic, stuck to two different <code>if</code> statements:</p> <pre><code>ThisAppFlagFileName=foo.txt if not exist "%HOMEPATH%\ourcompanyname\%ThisAppFlagFileName%" ( ECHO do something here ) ECHO Trace Message 1 before IF if exist "%HOMEPATH%\ourcompanyname\%ThisAppFlagFileName%" ( ECHO Trace Message 2 after IF before CD SET /P InstalledVersion=&lt;%HOMEPATH%\ourcompanyname\%ThisAppFlagFileName%" ECHO We think the file contains: %InstalledVersion% IF %InstalledVersion2% GEQ %ThisVersionInstallDataNum% ( ECHO Version %ThisVersion% of the %ThisAppVisibleName% has already been installed for this user; exiting. GOTO TheEnd ) ) :TheEnd Echo END </code></pre> <p>And we see <em>only</em> the first trace message (before the <code>if</code> statement), and <em>not</em> the second trace message. So our conclusion is that somehow the <em>content</em> of the file is being interpolated into the line <code>if exist "%HOMEPATH%\ourcompanyname\%ThisAppFlagFileName%"</code>, but of course we don't understand why the first <code>if not exist</code> works but the second doesn't.</p> <p>Can anyone spot the mistake, please? Environment is Windows 7 <code>cmd.exe</code> window, but we are hoping to deploy to both Windows 7 and Windows XP.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload