批处理文件newbiew - 错误检查帮助

时间:2011-08-23 10:54:53

标签: batch-file

我创建了以下脚本,以帮助IT人员进行USMT迁移。目的是列出按日期分类的可用迁移存储,然后使用一系列用户输入菜单选择它们。

然而问题是,如果用户输入正确的值,则没有问题,如果输入了错误的值,则脚本继续并且在目录结构变量中包含不正确的值。我试图使用,如果不存在,但我的代码只是导致脚本无限循环。如果有人可以帮助我减轻这些错误,我会很感激它我是一个基础设施人而不是程序员;)

:LOADSTATEW732
cls
dir "\\%migserver%\reminst\USMT XP to Windows 7\32Bit\%username%" 
Set /P loadstateday=Please enter day(01-31):
cls
dir "\\%migserver%\reminst\USMT XP to Windows 7\32Bit\%username%\%loadstateday%" 
Set /P loadstatemonth=Please enter month(01-12):
cls
dir "\\%migserver%\reminst\USMT XP to Windows 7\32Bit\%username%\%loadstateday%\%loadstatemonth%"
Set /P loadstateyear=Please enter year(Example 2011):
cls
ECHO You have selected: Day: %loadstateday% Month: %loadstatemonth% Year: %loadstateyear%
ECHO.
Set /P loadstateset=Are these settings correct? (Enter Yes/No):
IF %loadstateset% == Yes GOTO LOADSTATEGO
IF %loadstateset% == Y GOTO LOADSTATEGO
IF %loadstateset% == NO GOTO LOADSTATEW732
IF %loadstateset% == N GOTO LOADSTATEW732
:LOADSTATEGO
ECHO Exporting Migration Settings...
"%CD%\loadstate.exe" \\%migserver%\reminst\USMT XP to Windows 7\32Bit\%username%\%  Loadstateday%\%Loadstatemonth%\%Loadstateyear% /c /i:userfiles.xml /i:migapp.xml /v:12
ECHO.
pause
GOTO MENU:

整个脚本

@ECHO OFF
:START

:MIGSERVER
cls
ECHO.
ECHO ____________________________________________________________
ECHO Please set the deployment server
ECHO For example: UKHQITS017 FRPRAPS002 HUHKAPS001 etc
ECHO.
ECHO By default this script is configured to use the reminst 
ECHO share for USMT migrations.
ECHO ____________________________________________________________
ECHO.
Set /P MigServer=Please enter USMT Server:
cls
ECHO Server set to: %migserver% 
ECHO Please insure this is correct before continuing
pause
cls
GOTO MENU:

:MENU
ECHO.
ECHO ________________________________________________
ECHO User State Migration Tool v0.1
ECHO ________________________________________________
ECHO.
ECHO 1 Check Directory parameters
ECHO 2 Set USMT Server
ECHO 3 Delete Unused User Profiles (XP ONLY)
ECHO 4 Export migration settings from host system (Windows XP 32Bit)
ECHO 5 Export migration settigns to target system (Windows 7 32Bit)
ECHO 6 Exit
ECHO.
SET /P userChoice=Choose Option(1-6):

IF %userchoice% == 1 GOTO DIRPARAMS
IF %userchoice% == 2 GOTO MIGSERVER
IF %userchoice% == 3 GOTO PROFDEL
IF %userchoice% == 4 GOTO SCANSTATEXP32
IF %userchoice% == 5 GOTO LOADSTATEW732
IF %userchoice% == 6 GOTO QUITMENU


:DIRPARAMS
cls
ECHO.
ECHO  Current USMT x86 Folder: %CD%
ECHO  Current USMT Migration Store Server: %migserver%
ECHO.
ECHO   Please make sure of the following
ECHO   before continuing.
ECHO.
ECHO   1. You have set the correct migration server in the batch file.
ECHO   2. The above directorys exist
ECHO   3. The appropriate files are included within them
ECHO.
pause
cls
GOTO MENU:

:PROFDEL
cls
ECHO User Profile Deletion - Please select which profiles you wish to remove.
"%CD%\delprof2.exe" -c:127.0.0.1 -p -d:90
ECHO.
pause
GOTO MENU:

:SCANSTATEXP32
cls
ECHO Creating migration store.......
"%CD%\scanstate.exe" "\\%migserver%\reminst\USMT XP to Windows 7\32Bit\%username%\%date%" /o /c /i:userfiles.xml /i:migapp.xml /v:12
ECHO.
pause
GOTO MENU:

:LOADSTATEW732
cls
dir "\\%migserver%\reminst\USMT XP to Windows 7\32Bit\%username%" 
Set /P loadstateday=Please enter day(01-31):
cls
dir "\\%migserver%\reminst\USMT XP to Windows 7\32Bit\%username%\%loadstateday%" 
Set /P loadstatemonth=Please enter month(01-12):
cls
dir "\\%migserver%\reminst\USMT XP to Windows 7\32Bit\%username%\%loadstateday%\%loadstatemonth%"
Set /P loadstateyear=Please enter year(Example 2011):
cls
ECHO You have selected: Day: %loadstateday% Month: %loadstatemonth% Year: %loadstateyear%
ECHO.
Set /P loadstateset=Are these settings correct? (Enter Yes/No):
IF %loadstateset% == Yes GOTO LOADSTATEGO
IF %loadstateset% == Y GOTO LOADSTATEGO
IF %loadstateset% == NO GOTO LOADSTATEW732
IF %loadstateset% == N GOTO LOADSTATEW732

:LOADSTATEGO
ECHO Exporting Migration Settings...
"%CD%\loadstate.exe" \\%migserver%\reminst\USMT XP to Windows 7\32Bit\%username%\%Loadstateday%\%Loadstatemonth%\%Loadstateyear% /c /i:userfiles.xml /i:migapp.xml /v:12
ECHO.
pause
GOTO MENU:

:QUITMENU
ECHO USM TOOL v0.1 has ended.
pause

1 个答案:

答案 0 :(得分:1)

您可以使用IF EXIST命令。但是,IF EXIST的标准语法仅适用于文件。在此引用:http://support.microsoft.com/kb/65994 您可以按如下方式构建它:

:LOADSTATEW732
cls
dir "\\%migserver%\reminst\USMT XP to Windows 7\32Bit\%username%" 
Set /P loadstateday=Please enter day(01-31):
cls
IF EXIST "\\%migserver%\reminst\USMT XP to Windows 7\32Bit\%username%\%loadstateday%\NUL" (
 GOTO MONTH
) ELSE (
 ECHO Not a valid day integer.
 PAUSE
 GOTO LOADSTATEW732
)
:MONTH
cls
dir "\\%migserver%\reminst\USMT XP to Windows 7\32Bit\%username%\%loadstateday%" 
Set /P loadstatemonth=Please enter month(01-12):
cls
IF EXIST "\\%migserver%\reminst\USMT XP to Windows 7\32Bit\%username%\%loadstateday%\%loadstatemonth%\NUL" (
 GOTO YEAR
) ELSE (
 ECHO Not a valid month integer.
 PAUSE
 GOTO MONTH
)

等等。