我有使用批处理文件在Windows中映射驱动器的代码,我使用像nik和密码这样的参数。然后,如果用户和密码匹配程序将输出“成功”,否则程序将输出“一些错误”。输出成功的参数和我从FOR DO和FIND的净使用得到的一些错误
@MC ND
这里
:MENU
Title MyTitle
@ECHO OFF
@ECHO OFF
Echo --------------- MENU -----------------------
Echo 1. Press 1 for start or reload program.
Echo 2. Press 0 for exit.
Echo Choose :
SET /p tekan=
IF %tekan%==0 GOTO EXIT
IF %tekan%==1 GOTO FORM_QMR
:FORM_QMR
Echo ---------- Fill this Form (%userdomain%) ----------
SET /p nik="Insert Your ID :"
SET /p passwd="Insert Your Password :"
GOTO CREDENTIAL_ACTION
IF %userdomain% == DOMAIN GOTO CREDENTIAL_ACTION_FOR_USER_WITH_DOMAIN
IF NOT %userdomain% == DOMAIN GOTO CREDENTIAL_ACTION_FOR_USER_WITHOUT_DOMAIN
:CREDENTIAL_ACTION_FOR_USER_WITH_DOMAIN
FOR /F %%H IN ('NET USE "\\myserver\Path01\Path02" /User:domain\%nik% %passwd% 2^>^&1 ^| Find /I "System error 1326 has occurred."') DO SET "ACTSTATUS=BAD"
IF %ACTSTATUS%==BAD
(
Echo "Some error found, please contact administrator."
GOTO FORM_QMR
)
IF NOT %ACTSTATUS%==BAD
(
Echo "Success."
)
:CREDENTIAL_ACTION_FOR_USER_WITHOUT_DOMAIN
FOR /F %%H IN ('NET USE "\\192.168.1.1\Path01\Path02" /User:domain\%nik% %passwd% 2^>^&1 ^| Find /I "System error 1326 has occurred."') DO SET "ACTSTATUS=BAD"
IF %ACTSTATUS%==BAD
(
Echo "Some error found, please contact administrator."
GOTO FORM_QMR
)
IF NOT %ACTSTATUS%==BAD(
Echo "Success."
)
:EXIT
exit
我的问题在插入密码后,程序总是发送输出“命令的语法不正确”
任何人都可以帮忙..
答案 0 :(得分:2)
在以下代码段中纠正了一些错误(必要时进行了注释):
:FORM_QMR
Echo ---------- Fill this Form (%userdomain%) ----------
SET /p nik="Insert Your ID :"
SET /p passwd="Insert Your Password :"
rem CREDENTIAL_ACTION label not found
rem GOTO CREDENTIAL_ACTION
SET "ACTSTATUS=BAD"
IF %userdomain% == DOMAIN (
GOTO CREDENTIAL_ACTION_FOR_USER_WITH_DOMAIN
) else (
GOTO CREDENTIAL_ACTION_FOR_USER_WITHOUT_DOMAIN
)
:CREDENTIAL_ACTION_FOR_USER_WITH_DOMAIN
rem note double quoted password and "delims=" option
FOR /F "delims=" %%H IN ('
NET USE "\\myserver\Path01\Path02" /User:domain\%nik% "%passwd%" 2^>NUL ^| Find /I "The command completed successfully"
') DO SET "ACTSTATUS=GOOD"
goto :common
:CREDENTIAL_ACTION_FOR_USER_WITHOUT_DOMAIN
rem note double quoted password and "delims=" option
FOR /F "delims=" %%H IN ('
NET USE "\\192.168.1.1\Path01\Path02" /User:domain\%nik% "%passwd%" 2^>NUL ^| Find /I "The command completed successfully"
') DO SET "ACTSTATUS=GOOD"
:common
rem note parentheses and double quotes
IF "%ACTSTATUS%"=="BAD" (
Echo "Some error found, please contact administrator."
GOTO FORM_QMR
) else (
Echo "Success."
)
使用GOTO
和ERRORLEVEL
choice.exe
设置net.exe
的{{1}}费用较少的另一种方法:
@ECHO OFF
SETLOCAL
:MENU
Title MyTitle
Echo --------------- MENU -----------------------
Echo Press 1 for start or reload program.
Echo Press 0 for exit.
Echo Choose:
CHOICE /C 10 /M "Press 1 for start or reload program, 0 for exit."
IF errorlevel 2 GOTO EXIT
:FORM_QMR
Echo ---------- Fill this Form (%userdomain%) ----------
SET /p nik="Insert Your ID :"
SET /p passwd="Insert Your Password :"
IF %userdomain% == DOMAIN (
set "_shar=\\myserver\Path01\Path02"
set "_user=/User:domain\%nik%"
) else (
set "_shar=\\192.168.1.1\Path01\Path02"
set "_user=/User:domain\%nik%"
)
>NUL 2>&1 NET USE "%_shar%" %_user% "%passwd%"&&SET "ACTSTATUS=OK"||SET "ACTSTATUS=BAD"
IF "%ACTSTATUS%"=="BAD" (
Echo "Some error found, please contact administrator."
GOTO FORM_QMR
) else (
Echo "Success."
)
:EXIT
说明:
CHOICE.exe
Accept user input to a batch file,另见choice /?
。&&
和||
:Redirection。答案 1 :(得分:1)
(
必须与其关联的if
位于同一物理行。
同样的评论适用于do
且else
必须为) else (
否,如果,buts或maybes(除了更有经验的用户......)