批处理文件 net use with for output for variable if
batch file net use with for output for variable if
我在 windows 中有使用批处理文件映射驱动器的代码,我使用像 nik 和密码这样的参数。然后,如果用户和密码匹配,程序将输出 "Success",否则程序将输出 "some error"。输出成功的参数和我从网络使用 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
输入密码后我的问题,程序总是发送输出"the syntax of the command is incorrect"
谁能帮帮我..
(
必须与其关联的 if
在同一物理线路上。
同样的评论适用于 do
和else
必须是) else (
没有如果,但是或者也许(除了更有经验的用户......)
以下代码片段中有一些错误已更正(必要时进行注释):
: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
s 较少的方法利用由 choice.exe
和 net.exe
:
设置的 ERRORLEVEL
@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.
我在 windows 中有使用批处理文件映射驱动器的代码,我使用像 nik 和密码这样的参数。然后,如果用户和密码匹配,程序将输出 "Success",否则程序将输出 "some error"。输出成功的参数和我从网络使用 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
输入密码后我的问题,程序总是发送输出"the syntax of the command is incorrect"
谁能帮帮我..
(
必须与其关联的 if
在同一物理线路上。
同样的评论适用于 do
和else
必须是) else (
没有如果,但是或者也许(除了更有经验的用户......)
以下代码片段中有一些错误已更正(必要时进行注释):
: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
s 较少的方法利用由 choice.exe
和 net.exe
:
ERRORLEVEL
@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.