批量生成的IF语句导致问题。单独运行时工作?

时间:2014-08-01 15:31:32

标签: batch-file if-statement

在工作中,我每天为新用户设置几台电脑。我正在使用一个批处理文件,提供3个选项: 1.添加新用户 2.更改PC名称 两个 从他们自己的批处理文件运行时它们都很好用,但是当将代码复制/粘贴到IF语句中时,只有IF语句'2'可以工作。我究竟做错了什么? 设置要求输入1,2或3。 在此先感谢!!

set /p choose=Enter your selection here:
if %choose%==1 (
set /p user=Enter Username, then press Enter to create new user with default password:
net user "%user%" LSCpass14 /add
)

if %choose%==2 (
set /P pcname=Please enter this computer's LSC Asset Tag number, eg. 1295:
REG ADD HKLM\SYSTEM\CurrentControlSet\services\Tcpip\Parameters /v "NV Hostname" /t REG_SZ /d "LSC-%pcname%" /f
set /p reboot=You will need to restart your computer for these changes to be applied.   Would you like to restart now? [y/n] )
if %reboot%==y start shutdown -r -t 00
)

if %choose%==3 (
set /p user=Enter Username, then press Enter to create new user with default password:
net user "%user%" LSCpass14 /add
set /P pcname=Please enter this computer's LSC Asset Tag number, eg. 1295 :
REG ADD HKLM\SYSTEM\CurrentControlSet\services\Tcpip\Parameters /v "NV Hostname" /t REG_SZ /d "LSC-%pcname%" /f
set /p reboot=You will need to restart your computer for these changes to be applied.   Would you like to restart now? (y\n) 
if %reboot%==y start shutdown -r -t 00
)

2 个答案:

答案 0 :(得分:1)

使用此代码将所有字符串括在需要的双引号中,因为字符串中包含特殊字符。

@echo off
setlocal EnableDelayedExpansion
set /p "choose=Enter your selection here: "
if "!choose!"=="1" (
   echo Enter user name, then press Enter to create new user with default password.
   set /p "user=User name: "
   %SystemRoot%\system32\net.exe user "!user!" LSCpass14 /add
) else if "!choose!"=="2" (
   set /P "pcname=Please enter this computer's LSC Asset Tag number, eg. 1295: "
   %SystemRoot%\system32\reg.exe ADD HKLM\SYSTEM\CurrentControlSet\services\Tcpip\Parameters /v "NV Hostname" /t REG_SZ /d "LSC-!pcname!" /f
   echo You will need to restart your computer for these changes to be applied.
   set /p "reboot=Would you like to restart now? [y/n] "
   if /i "!reboot!"=="y" start %SystemRoot%\system32\shutdown.exe -r -t 00
) else if "!choose!"=="3" (
   echo Enter user name, then press Enter to create new user with default password.
   set /p "user=User name: "
   %SystemRoot%\system32\net.exe user "!user!" LSCpass14 /add
   set /P "pcname=Please enter this computer's LSC Asset Tag number, eg. 1295 : "
   %SystemRoot%\system32\reg.exe ADD HKLM\SYSTEM\CurrentControlSet\services\Tcpip\Parameters /v "NV Hostname" /t REG_SZ /d "LSC-!pcname!" /f
   echo You will need to restart your computer for these changes to be applied.
   set /p "reboot=Would you like to restart now? [y/n] "
   if /i "!reboot!"=="y" start %SystemRoot%\system32\shutdown.exe -r -t 00
)
endlocal

在输入help cmdcmd /?后,在命令提示符窗口的最后一页输出中显示一个字符列表,该字符串需要用双引号括起来。括号和方括号也属于这组特殊字符。批处理代码演示了()具有特殊含义的原因,因此包含它们的字符串确实需要包含双引号。

输入set /?help set后,有必要按照帮助输出中的说明使用环境变量的延迟扩展。

答案 1 :(得分:1)

当cmd解析器读取一行或一行(括号内的代码)时,所有变量读取都将替换为变量中的值开始执行代码之前。如果块中代码的执行会更改变量的值,则无法从同一块内部看到此值,因为变量的读取操作不存在,因为它已替换为变量中的值。

在几行命令与&连接的行中可以看到同样的行为。该行被完全解析然后执行。如果第一个命令更改了变量的值,则后面的命令不能使用此更改的值,因为读取操作将替换。

要解决此问题,您需要启用延迟扩展,并在需要时将语法从%var%更改为!var!,向解析器指示读取操作需要延迟执行命令。

setlocal enabledelayedexpansion
....
if "%choose%"=="1" (
    set "user="
    set /p "user=Enter Username, then press Enter to create new user with default password:"
    if defined user net user "!user!" LSCpass14 /add
)
....

其余的块有同样的问题。