返回列表 发帖

[文本处理] 批处理if else嵌套脚本报错

运行一直报错:The syntax of the command is incorrect.
哪位可以帮忙看看,谢谢!

------------------------------------------------------------------------------------
@echo off
@hostname>>"c:\temp\hostname.txt"
set /p host=<c:\temp\hostname.txt
if "%host:~7,5% lss 10031 (echo %user% is Com_PRD_PPC01)
else if "%host:~7,5% lss 10061 (echo %user% is Com_PRD_PPC02)
else if "%host:~7,5% lss 10091 (echo %user% is Com_PRD_PPC03)
else if "%host:~7,5% lss 10121 (echo %user% is Com_PRD_PPC04)
else if "%host:~7,5% lss 10151 (echo %user% is Com_PRD_PPC05)
else if "%host:~7,5% lss 10181 (echo %user% is Com_PRD_PPC06)
else if "%host:~7,5% lss 10211 (echo %user% is Com_PRD_PPC07)
else if "%host:~7,5% lss 10241 (echo %user% is Com_PRD_PPC08)
else if "%host:~7,5% lss 10271 (echo %user% is Com_PRD_PPC09)
else if "%host:~7,5% lss 10301 (echo %user% is Com_PRD_PPC10)
else if "%host:~7,5% lss 10331 (echo %user% is Com_PRD_PPC11)
else if "%host:~7,5% lss 10361 (echo %user% is Com_PRD_PPC12)
else if "%host:~7,5% lss 10391 (echo %user% is Com_PRD_PPC13)
else if "%host:~7,5% lss 10421 (echo %user% is Com_PRD_PPC14)
else if "%host:~7,5% lss 10451 (echo %user% is Com_PRD_PPC15)
else if "%host:~7,5% lss 10481 (echo %user% is Com_PRD_PPC16)
else if "%host:~7,5% lss 10511 (echo %user% is Com_PRD_PPC17)
else if "%host:~7,5% lss 10541 (echo %user% is Com_PRD_PPC18)
else if "%host:~7,5% lss 10571 (echo %user% is Com_PRD_PPC19)
else if "%host:~7,5% lss 10601 (echo %user% is Com_PRD_PPC20)
else if "%host:~7,5% lss 10631 (echo %user% is Com_PRD_PPC21)
else if "%host:~7,5% lss 10661 (echo %user% is Com_PRD_PPC22)
else if "%host:~7,5% lss 10691 (echo %user% is Com_PRD_PPC23)
else if "%host:~7,5% lss 10721 (echo %user% is Com_PRD_PPC24)
else if "%host:~7,5% lss 10751 (echo %user% is Com_PRD_PPC25)
else if "%host:~7,5% lss 10781 (echo %user% is Com_PRD_PPC26)
else (goto eof)
copy c:\Script\Time\shortcut.lnk "C:\Users\%user%\Desktop\shortcut.lnk"
pause&exitCOPY

回复 1# conan52


   
ELSE 子句必须出现在同一行上的 IF 之后。例如:

    IF EXIST filename. (
        del filename.
    ) ELSE (
        echo filename. missing.
    )

TOP

回复 2# wujunkai


    是要if回车,另起一行吗?好像还是不行啊。

TOP

本帖最后由 went 于 2020-4-9 15:30 编辑
@echo off
for /f "delims=" %%i in ('hostname') do set "host=%%i"
echo %host%
if "%host:~7,5%" lss "10031" (echo %user% is Com_PRD_PPC01 & goto :done)
if "%host:~7,5%" lss "10061" (echo %user% is Com_PRD_PPC02 & goto :done)
if "%host:~7,5%" lss "10091" (echo %user% is Com_PRD_PPC03 & goto :done)
if "%host:~7,5%" lss "10121" (echo %user% is Com_PRD_PPC04 & goto :done)
if "%host:~7,5%" lss "10151" (echo %user% is Com_PRD_PPC05 & goto :done)
if "%host:~7,5%" lss "10181" (echo %user% is Com_PRD_PPC06 & goto :done)
if "%host:~7,5%" lss "10211" (echo %user% is Com_PRD_PPC07 & goto :done)
if "%host:~7,5%" lss "10241" (echo %user% is Com_PRD_PPC08 & goto :done)
if "%host:~7,5%" lss "10271" (echo %user% is Com_PRD_PPC09 & goto :done)
if "%host:~7,5%" lss "10301" (echo %user% is Com_PRD_PPC10 & goto :done)
if "%host:~7,5%" lss "10331" (echo %user% is Com_PRD_PPC11 & goto :done)
if "%host:~7,5%" lss "10361" (echo %user% is Com_PRD_PPC12 & goto :done)
if "%host:~7,5%" lss "10391" (echo %user% is Com_PRD_PPC13 & goto :done)
if "%host:~7,5%" lss "10421" (echo %user% is Com_PRD_PPC14 & goto :done)
if "%host:~7,5%" lss "10451" (echo %user% is Com_PRD_PPC15 & goto :done)
if "%host:~7,5%" lss "10481" (echo %user% is Com_PRD_PPC16 & goto :done)
if "%host:~7,5%" lss "10511" (echo %user% is Com_PRD_PPC17 & goto :done)
if "%host:~7,5%" lss "10541" (echo %user% is Com_PRD_PPC18 & goto :done)
if "%host:~7,5%" lss "10571" (echo %user% is Com_PRD_PPC19 & goto :done)
if "%host:~7,5%" lss "10601" (echo %user% is Com_PRD_PPC20 & goto :done)
if "%host:~7,5%" lss "10631" (echo %user% is Com_PRD_PPC21 & goto :done)
if "%host:~7,5%" lss "10661" (echo %user% is Com_PRD_PPC22 & goto :done)
if "%host:~7,5%" lss "10691" (echo %user% is Com_PRD_PPC23 & goto :done)
if "%host:~7,5%" lss "10721" (echo %user% is Com_PRD_PPC24 & goto :done)
if "%host:~7,5%" lss "10751" (echo %user% is Com_PRD_PPC25 & goto :done)
if "%host:~7,5%" lss "10781" (echo %user% is Com_PRD_PPC26 & goto :done)
pause&exit
:done
copy "c:\Script\Time\shortcut.lnk" "C:\Users\%user%\Desktop\shortcut.lnk"
pause&exitCOPY

TOP

回复 4# went


    还是报错。

TOP

回复 5# conan52


    报什么错

TOP

一样的错误

TOP

回复 6# went


    The syntax of the command is incorrect.

TOP

回复 8# conan52


    hostname显示什么,再试试4楼

TOP

回复 9# went


    一样,错误信息,之前能显示出正确的本机主机名。

TOP

回复 10# conan52


    你测试的主机名是什么

TOP

if "%host:~7,5%" lss "10031" (echo %user% is Com_PRD_PPC01&&goto done)COPY
把4楼的修改 参照这一句。我的电脑 echo %user% 没有这个。
目的,学习批处理

TOP

返回列表