我的批处理文件有什么错误

时间:2014-03-14 05:23:19

标签: batch-file

尝试创建批处理文件以使这些任务运行得更快。

有人可以纠正我的错误或建议更好的方式来编写这个脚本

基本上每次我运行它都说“请求超时”

@echo off
color 0a
Title 

:Beginning 
set /p UserInput = What Would You Like To Start?
echo.

if %UserInput% == 1 goto :Windows Update
if not %UserInput% == "" goto :Exit
else goto :Exit

if %UserInput% == 2 goto :Group Policy Update
if not %UserInput% == "" goto :Exit
else goto :Exit

if %UserInput% == 5 goto :Favorites
if not %UserInput% == "" goto :Exit
else goto :Exit

if %UserInput% == 3 goto :Tools
if not %UserInput% == "" goto :Exit
else goto :Exit

 if %UserInput% == 4 goto :Printer
 if not %UserInput% == "" goto :Exit
 else goto :Exit

:Windows Update
start C:\Windows\system32\wuapp.exe
pause 
exit

:Group Policy Update 
start gpupdate.exe
pause
exit 

:Favorites 
move %userprofile%\favorites\*.* G:\
pause 
exit

:Tools
start \\NoneOFyourBusiness
pause 
exit

:Printer
start iexplore.exe http://www.Google.com
pause
exit

:Exit
set /p beginning == Return To The Start?
echo.
echo Y=Yes or N=No
if %beginning% == "Y" goto :Beginning 
if not %beginning% == "N" goto :Exit 2

:Exit 2
pause 
exit**'

3 个答案:

答案 0 :(得分:2)

set /p UserInput = What Would You Like To Start?

将输入字符串应用于名为UserInput的变量 Space

批次对set = 两侧的空格敏感(但不是set /a

if not %UserInput% == "" goto :Exit

由于您无法控制使用的输入,因此可能包含批次敏感的空格和其他字符。

使用

if not "%UserInput%"=="" goto :Exit

请注意,两个字符串必须完全相等。 IF /i ...会使测试不区分大小写。

另请注意,简单地将 Enter 回复到set /p将使变量保持不变,它不会"设置"它是一个空字符串。如果您需要,请使用

set "var="
set /p var=

另外,要检测是否已设置var,请使用

if defined var


if "%UserInput%"=="1" goto :Windows Update

这是一个失败失败的场景。执行的真实命令将是

if "%UserInput%"=="1" goto Windows

空格分隔符后面的剩余文字是文档。

恕我直言,goto使用冒号不是一个好主意。它有效,但与CALL语句有所不同。 CALL :something将执行名为something内部子例程(即此批处理文件中的子例程),而不带冒号的CALL something将调用外部可执行文件。通过类比,GOTO也不应该与冒号一起使用。唯一的例外是记录的特殊条件GOTO :EOF,其中冒号 是必需的。 GOTO :EOF表示'转到物理批处理文件的末尾。The label EOF need not (indeed **should** not) be included in the batch. CMD`知道它必须去哪里...

所以 - 解决这些问题,它应该会更好。不确定您的错误回复是什么,但我建议您预计会因为等待输入而感到厌倦。

答案 1 :(得分:0)

 **     @echo off
   color 0a
   Title 
  :Beginning 
   set UserInput=Error
   set /p UserInput=What Would You Like To Start?
   echo.
   if "%UserInput%"=="WU" goto Windows Update
   if not "%UserInput%"=="" goto Exit
   if "%UserInput%"=="GPU" goto Group Policy Update
   if not "%UserInput%"=="" goto Exit
   if "%UserInput%"=="Fav" goto Favorites
   if not "%UserInput%"=="" goto Exit
   if "%UserInput%"=="T" goto Tools
   if not "%UserInput%"=="" goto Exit
   if "%UserInput%"=="P" goto Printer
   if not "%UserInput%"=="" goto Exit
  :Windows Update
   start C:\Windows\system32\wuapp.exe
   goto exit
  :Group Policy Update 
   start gpupdate.exe
   goto exit 
   :Favorites 
   move %userprofile%\favorites\*.* G:\
   goto exit
  :Tools
   start \\T
   goto exit
  :Printer
   start iexplore.exe http://Google.com/
  :Exit
   set Beginning==Error
   set /p Beginning==Return To The Start?
   echo.
   echo y=Yes or n=No
   if "%Beginning%"=="y" call :Beginning 
   if not "%Beginning%"=="n" goto Exit 2
  :Exit 2
   pause 
   exit**

答案 2 :(得分:0)

试试这个......

color 0a
Title Testing

REM Since exit is a keyword - changed to leave

:Begining 
echo choose an option
choice /c 12345

rem set /p UserInput = What Would You Like To Start?
echo.

if %errorlevel% == 1 goto WindowsUpdate

if %errorlevel% == 2 goto GroupPolicyUpdate

if %errorlevel% EQU 5 goto Favorites

if %errorlevel% EQU 3 goto Tools

if %errorlevel% EQU 4 goto Printer

:WindowsUpdate
start C:\Windows\system32\wuapp.exe
pause 
goto leave

:GroupPolicyUpdate 
start gpupdate.exe
pause
goto leave 

:Favorites 
move %userprofile%\favorites\*.* G:\
pause 
goto leave

:Tools
start \\NoneOFyourBusiness
pause 
goto leave

:Printer
start iexplore.exe http://www.Google.com
pause
goto leave

:leave
echo leave the script?
choice /c YN
if %errorlevel% NEQ 1 goto Begining
if %errorlevel% EQU 1 exit