Mgamerz
Mgamerz

Reputation: 2890

Batch file | symbol: inserting a space for no apparent reason

I have a batch file with this snippet in it:

systeminfo|findstr /B "Host Name:"|findstr /C:"COMPNAME-SET"
if %ERRORLEVEL% EQU 0 (
echo This computer is either a test machine or has not had it's name configured yet. Continuing will run the script in test mode.
pause
)

I'm not sure why, but the top line does not work. The output is this:

Z:\>systeminfo | findstr /B "Host Name:"  | findstr /C:"IGSWIDWB-SET"
The syntax of the command is incorrect.
Z:\>

There is an extra space it seems after the " mark. I used to use a space between the pipes and the commands, but I removed them in an attempt to fix this error. It made no difference. I have made sure the file is in Windows format with Windows-style characters, and I don't know what to do now. This is standard batch scripting that I am using.

This code lets me figure out if it's a laptop or a desktop in an automated setup script I have developed. The hostname has a specific protocol and it will navigate through a set of installations based on this step.

Anyone know what's wrong? Thanks!

In response to [name here] here is the code that executes before it (and some after it); I have modified the words however so it is not obvious what it is. I work at an organization that prefers I don't share internal documents, but don't mind if I obfuscate it. The part that fails is on the line where the first systeminfo is.

::@echo off
setlocal
set BATDIR=%~dp0%
set SCRIPTVER=3.2
cls
set SUBDEPARTMENT=false

if not [%1]==[] (
    if "%1" EQU "/help" (
        echo Calling For Help.
        CALL :HELP
        goto :EOF
       )

    if "%1" EQU "/SUBDEPARTMENT" (
        set SUBDEPARTMENT=true
    echo Set SUBDEPARTMENT options to true.
    ::echo SUBDEPARTMENT Switch does not work on this version of the script.  ::CD VERSION ONLY :: Continuing without.
    echo.
    goto CONTINUE
    )
)

:Continue
color 0B
   echo XXXX AutoInstaller Script for Windows 7/XP (x86/amd64)
   echo Version %SCRIPTVER% - Build Date: 11/14/2012
   echo Estimated Install Time: 80(Core) to 180(SUBDEPARTMENT) minutes
   echo Computer will reboot to phase 2 once completed.
   echo A log file for this script can be found at %USERPROFILE%\AutoInstallerLog.txt
   echo ---------------------------------------------------------------
echo Preparing System...
echo Auto Install Log, generated on: > %USERPROFILE%\AutoInstallerLog.txt
echo AutoInstaller Script Version: %SCRIPTVER% >> %USERPROFILE%\AutoInstallerLog.txt
DATE /T >> %USERPROFILE%\AutoInstallerLog.txt
echo This log file reflects what steps completed in the ASI script. It does not mean they completed correctly so please make sure that all software is installed correctly!
echo Make sure the computer has been renamed to match the type [e.g. COMPUTER_PROTOCOL]. Make sure it  has also been rebooted it so that it takes effect.
echo SystemInfo Output: >> %USERPROFILE%\AutoInstallerLog.txt
systeminfo >> %USERPROFILE%\AutoInstallerLog.txt

::Setting script parameters
echo.
echo Configuring script parameters.

::Check if it's a test machine
echo systeminfo^|findstr /B "Host Name:"^|findstr /C:"TESTCOMP_NAME"
systeminfo|findstr /B "Host Name:"|findstr /C:"TESTCOMP_NAME"
if %ERRORLEVEL% EQU 0 (
echo This computer is either a test machine or has not had it's name configured yet. Continuing will run the script in test mode.
pause
set TestComp=1
::TestOS
   systeminfo|findstr /C:"XP Professional"
   if %ERRORLEVEL% EQU 0 (
      set OS=XP
      goto :finishTestOS
     )

    systeminfo | findstr /C:"7 Enterprise"
    if %ERRORLEVEL% EQU 0 (
      set OS=7
      goto :finishTestOS
     )
     :finishTestOS

set STYLE=workstation
goto :AdminCheck
)
::Debug Statement:
echo NEXT PArT!!! LINE 67
::::OS Version
:getOS
   systeminfo|findstr /C:"XP Professional"
   if %ERRORLEVEL% EQU 0 (
      set OS=XP
      goto :finishOS
     )

    systeminfo|findstr /C:"7 Enterprise"
    if %ERRORLEVEL% EQU 0 (
      set OS=7
      goto :finishOS
     )


    :finishOS
    echo This system is running Windows %OS%.

::Get Form Factor
:getStyle
    systeminfo | findstr /B "Host Name:" | findstr /C:"WORKSTATION_PROTOCOL"
    if %ERRORLEVEL% EQU 0 (
      set STYLE=laptop
     ) else (
      set STYLE=workstation
     )
     :finishStyle
    echo This system is a %STYLE%.

if not defined STYLE (
    echo System style not configured correctly. The name of the computer likely does not follow protocol, it must have PROTOCOLS in it.
    goto :ERROREND
)

if not defined OS (
    echo OS not detected properly. The script may need to be updated to detect new versions of Windows.
    goto :ERROREND
)

Upvotes: 0

Views: 905

Answers (1)

dbenham
dbenham

Reputation: 130849

There is nothing wrong with the code snippet that you posted. It works just fine in isolation.

I am assuming you are seeing the "extra" spaces in the code that is displayed because ECHO is ON. Those spaces are an artifact of how the parser processes a line. You shouldn't worry about them (although looking at the output when ECHO is ON is an invaluable tool for diagnosing problems).

The ECHO ON output is modified for many batch constructs. For example, the following perfectly valid code

if "a"=="b" >test.txt echo matched

is displayed as follows if ECHO is ON

if "a" == "b" echo matched 1>test.txt

You can see that spaces have been added, the redirection was moved to the end, and a 1 (representing stdout) was inserted in front of the redirection. This is all perfectly normal - The command executes just fine. (of course it does nothing because the IF evaluates to FALSE) If the IF condition were changed to TRUE, then the extra space after matched would NOT be included in the output.

You shouldn't worry about extra spaces appearing in your ECHO ON output. But you should be on the lookout for extra spaces in your source code. An extra trailing space would be included in output if it appears in your original source code.

I suspect the syntax error problem lies elsewhere in your code. Perhaps you can provide more code context where the error is occurring.

Updated answer in response to updates to question

The error is not occurring where you think! The syntax error is in the complex IF statement that follows.

You attempt to use a :finishTestOS label within a parenthesized block of code. You will always get a syntax error if the label within the parentheses is not immediately followed by a valid non-label line. (Actually the rule may be a bit more complicated, but that is not important here)

You could eliminate the syntax error by simply putting a REM statement below the label.

:finishTestOS
rem

BUT - you would still have major problems - The code would not work the way you want. You should never attempt to GOTO a label within a parenthesized block of code. See the accepted answer to (Windows batch) Goto within if block behaves very strangely for more info.

Another problem you have is you attempt to access %ERRORLEVEL% within the parenthesized block of code. The value is expanded at parse time, and the entire block is parsed at once, so the value will be the ERRORLEVEL that existed before you entered the IF block. There are a number of ways that can be solved.

  • delayed expansion: if !errorlevel!==0 ...
  • if not errorlevel 1 ... (read the documentation for the IF statement)
  • use && for conditional execution upon success and/or || for conditional execution upon failure. In your case systeminfo|findstr ... && set OS=...

I suspect you may have additional problems, but I don't have time to diagnose everything.

Upvotes: 1

Related Questions