2012-11-14 44 views
0

我有這個片段的批處理文件在它:批處理文件|符號:插入空格無緣無故

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 
) 

我不知道爲什麼,但前行不起作用。輸出是這樣的:。

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

有它的「標誌後,似乎是一個多餘的空間,我以前用的是管道和命令之間的空間,但我在試圖解決這個錯誤,把他們趕走它沒有區別我已經確定文件是Windows格式的Windows風格的字符,我不知道該怎麼做。這是我正在使用的標準批處理腳本。

此代碼讓我如果它是我開發的自動化安裝腳本中的筆記本電腦或臺式機,則主機名具有特定的協議,並且它將根據此步驟通過一組安裝進行導航

任何人都知道什麼是錯的? 謝謝!

爲了響應[name here],這裏是在它之前執行的代碼(以及在它之後執行的代碼);然而,我修改了這些詞,所以它不是什麼明顯的。我在一個喜歡不分享內部文檔的組織工作,但不介意我是否將其混淆。失敗的部分在第一個systeminfo所在的行上。

::@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 
) 
+1

我試了兩個管道之前和之後的空間,它工作正常。即使在報價後有2個空格字符。 – Laf

+1

我猜想那裏有一個看不見的角色。嘗試重新輸入相關線路。 –

+0

我重新輸入它並沒有修復它。 (還沒有閱讀發佈的其他答案)。但是,感謝您的意見:) – Mgamerz

回答

1

您發佈的代碼段沒有任何問題。它工作得很好,孤立。

我假設您在顯示的代碼中看到「額外」空格,因爲ECHO爲ON。這些空格是解析器處理線條的工件。您不應該擔心它們(儘管在ECHO打開時查看輸出是診斷問題的寶貴工具)。

對於許多批處理結構,ECHO ON輸出被修改。例如,下面的完全有效的代碼

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

如下如果ECHO爲ON

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

可以看到,空間已被添加,重定向被轉移到結束時顯示,和1 (代表stdout)被插入重定向前面。這完全正常 - 命令執行得很好。 (當然,它什麼都不做,因爲IF計算結果爲FALSE)如果IF條件變爲TRUE,則匹配後的額外空間將不會包含在輸出中。

您不應該擔心ECHO ON輸出中出現多餘的空格。但是你應該在你的源代碼中尋找額外的空間。如果出現在原始源代碼中,則會在輸出中包含額外的尾部空格。

我懷疑語法錯誤問題在於代碼中的其他地方。也許你可以在發生錯誤的地方提供更多的代碼上下文。

響應更新更新回答問題

錯誤沒有發生,你認爲!語法錯誤出現在下面的複雜IF語句中。

您試圖在帶括號的代碼塊中使用:finishTestOS標籤。如果括號內的標籤之後沒有緊跟有效的非標籤行,則您將始終得到語法錯誤。 (實際上規則可能有點複雜,但這並不重要)

只需在標籤下面放置REM語句即可消除語法錯誤。

:finishTestOS 
rem 

但 - 你仍然會遇到重大問題 - 代碼不會按照你想要的方式工作。你絕不應該試圖在加括號的代碼塊中使用GOTO標籤。有關詳細信息,請參閱(Windows batch) Goto within if block behaves very strangely的接受答案。

您遇到的另一個問題是您嘗試在括號內的代碼塊中訪問%ERRORLEVEL%。該值在解析時被展開,並且整個塊被立即解析,因此該值將是您在進入IF塊之前存在的ERRORLEVEL。有很多方法可以解決。

  • 延遲擴展:if !errorlevel!==0 ...
  • if not errorlevel 1 ...(讀取IF語句的文檔)
  • 使用&&用於在用於在故障條件執行成功和/或||條件執行。在你的情況下,systeminfo|findstr ... && set OS=...

我懷疑你可能有其他問題,但我沒有時間來診斷一切。

+0

我更新了原始帖子,提供了更多信息。 – Mgamerz

+0

也關閉@echo失敗。我曾試圖找出它死亡的地方。 – Mgamerz

+1

@Mgamerz - 我已經想出了你的直接問題。看到我更新的答案。 – dbenham