Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 批处理文件|符号:无明显原因插入空格_Windows_Batch File_Pipe - Fatal编程技术网

Windows 批处理文件|符号:无明显原因插入空格

Windows 批处理文件|符号:无明显原因插入空格,windows,batch-file,pipe,Windows,Batch File,Pipe,我有一个批处理文件,其中包含以下代码段: 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 ) 我不知道为

我有一个批处理文件,其中包含以下代码段:

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]的响应,这里是在[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
)

您发布的代码片段没有问题。它在隔离状态下运行良好

我假设您在显示的代码中看到“额外”空格,因为ECHO处于打开状态。这些空格是解析器处理行的方式的产物。您不必担心这些问题(尽管在ECHO打开时查看输出是诊断问题的宝贵工具)

针对许多批处理构造修改了输出上的ECHO。例如,下面的代码是完全有效的

if "a"=="b" >test.txt echo matched
如果ECHO打开,则显示如下

if "a" == "b" echo matched 1>test.txt
您可以看到已经添加了空格,重定向被移动到末尾,并且在重定向前面插入了1(表示stdout)。这是完全正常的-命令执行得很好。(当然它什么也不做,因为IF的计算结果为FALSE)如果IF条件更改为TRUE,那么匹配后的额外空间将不会包含在输出中

您不必担心输出时回声中会出现额外的空格。但是您应该在源代码中寻找额外的空间。如果在原始源代码中出现,输出中将包含额外的尾随空间

我怀疑语法错误问题在代码的其他地方。也许您可以在发生错误的地方提供更多的代码上下文

根据问题更新更新了答案

错误不会发生在你认为的地方!语法错误出现在后面的复杂IF语句中

您试图在括号内的代码块中使用
:finishTestOS
标签。如果括号内的标签后面没有紧跟有效的非标签行,则始终会出现语法错误。(事实上,规则可能有点复杂,但这在这里并不重要)

只需在标签下方放置REM语句,就可以消除语法错误

:finishTestOS
rem
但是,您仍然会遇到一些重大问题,代码无法按您希望的方式工作。千万不要试图转到括号内代码块中的标签。有关更多信息,请参阅的已接受答案

您遇到的另一个问题是试图访问括号内代码块中的
%ERRORLEVEL%
。该值在解析时展开,并且立即解析整个块,因此该值将是在您输入IF块之前存在的ERRORLEVEL。有很多方法可以解决

  • 延迟扩展:
    if!错误等级==0…
  • 如果不是错误级别1…
    (请阅读if语句的文档)
  • 成功时使用
    &&
    进行有条件执行,失败时使用
    |
    进行有条件执行。在您的案例中,
    systeminfo | findstr…&&设置操作系统=…

我怀疑您可能还有其他问题,但我没有时间对所有问题进行诊断。

我在两个管道前后都使用了空格进行了尝试,效果很好。即使引号后面有两个空格字符,我猜里面有一个看不见的字符。尝试重新键入有问题的行。我重新键入,但它没有修复它。(尚未阅读发布的其他答案)。不过,感谢您的输入:)它也会在@echo关闭时失败。我在试图找出它死在哪里的时候对它发表了评论。@Mgamerz-我已经找出了你眼前的问题。看看我的最新答案,我知道了。我在TestComp部分之外使用了一个if定义的TestComp,以转移到实际检测之外。我使用了errorlevel而不是%errorlevel%。它有点笨重,但它能用。现在,我的问题是网络路径和命令提示,但一旦我完成了,我将打开另一个问题。谢谢