batch-filefor-loopif-statementfindstrnet-use

batch file net use with for output for variable if


I have code for mapping drive in windows with batch file, i use parameter like nik and password. Then if the user and password match the program will give output "Success" and else it the program will give output "some error". The parameters for output Success and some error i got from net use with FOR DO and FIND

@MC ND

here

: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

my problem after insert password, the program always send output "the syntax of the command is incorrect"

can anyone helpme..


Solution

  • There are some mistakes corrected in the following code snippet (commented if necessary):

    :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."
    )
    

    Another approach with less GOTOs utilises ERRORLEVEL set by choice.exe and net.exe:

    @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
    

    Explanation: