I'm trying to make a username change option for the settings in one of my batch file programs, but it keeps displaying a message like "set was unexpected at this time" which is weird, because my code seems right. It happens after entering a value for "cuser". None of the values I'm entering for input are null, since I've pre-declared the values at the beginning of the program to make it so I didn't have any null-value errors.
:uch
cls
echo.
echo Are you sure you want to change your username?
echo.
echo [Y/N]
echo.
set /p input=
if %input% EQU n goto set
if %input% NEQ y goto uch
:ucy
cls
echo.
echo Enter your current username
echo.
set /p cuser=
if %cuser% NEQ %username1% (
echo.
echo Incorrect username. Please try again.
echo Press any button to continue.
echo.
pause>null
goto :ucy
)
if %cuser% EQU %username1% (
echo Please enter new username.
echo.
set /p nuser=
echo Please enter again.
echo.
set /p nuser2=
if %nuser2% EQU %nuser% set username1=%nuser%
if %nuser2% EQU %nuser% goto ga1
if %nuser2% NEQ %nuser% (
echo Usernames do not match. Please try again.
echo Press any button to continue.
echo.
pause>null
goto ucy
)
goto ucy
You are trying to set value of variable inside if block without delayed expansion and the if is parsed with wrong syntax.And you have one unclosed bracket...
:uch
cls
echo.
echo Are you sure you want to change your username?
echo.
echo [Y/N]
echo.
set /p input=
if %input% EQU n goto set
if %input% NEQ y goto uch
:ucy
cls
echo.
echo Enter your current username
echo.
set /p cuser=
if %cuser% NEQ %username1% (
echo.
echo Incorrect username. Please try again.
echo Press any button to continue.
echo.
pause>null
goto :ucy
)
setlocal enableDelayedExpansion
if %cuser% EQU %username1% (
echo Please enter new username.
echo.
set /p nuser=
echo Please enter again.
echo.
set /p nuser2=
if !nuser2! EQU !nuser! set username1=!nuser!
if !nuser2! EQU !nuser! goto ga1
if !nuser2! NEQ !nuser! (
echo Usernames do not match. Please try again.
echo Press any button to continue.
echo.
pause>null
goto ucy
)
goto ucy
You are not using the proper syntax for the if command. When comparing strings, use
if "%var1%"=="%var2%" to compare. EQL NEQ etc. are for numeric comparison.
You DO NOT need to have multiple if statements (one for yes and one for no) because, you can assume that if they didn't say yes then they meant to say no. Here is an improved script for you, one that doesn't need delayed expansion in order to work. Hope this helps.
:uch
cls
echo.
echo Are you sure you want to change your username?
set /p input=[Y/N]
if not "%input%"=="y" (goto ga1)
::THEY WANT TO CHANGE THEIR USERNAME
cls
echo.
set /p cuser=Enter your current username ^>
if not "%cuser%"=="%username1%" (
echo.
echo Incorrect username. Please try again.
echo Press any button to continue.
pause>NUL
goto :uch
)
::THE USERNAME MATCHES
echo.
set /p nuser=Please enter new username. ^>
echo.
set /p nuser2=Please enter again. ^>
if "%nuser2%"=="%nuser%" (set username1=%nuser% && goto ga1)
echo Usernames do not match. Please try again.
echo Press any button to continue.
echo.
pause>NUL
goto uch
:ga1
:://DO SOME OTHER STUFF HERE AFTER THEY CHANGED THEIR NAME OR OPTED NOT TO.
Related
In Windows batch, I'm asking the user whether he wants to use the program's internal default or wants to set his own parameters, but no matter what the user sets as an answer, the program always jumps straight to the main routine using the internal defaults. This is my code:
#echo off
setlocal EnableDelayedExpansion
choice /C:yn /M "Use internal defaults? "
if errorlevel==1 goto yes
if errorlevel==2 goto no
rem use default
:yes
set "MYNUMBER=5"
goto run
rem let user define another number
:no
set /P MYNUMBER="Please set a number: "
goto run
rem main routine
:run
echo %MYNUMBER%
pause
What am I missing?
And since we're at it: how can I force the program to wait for the user to hit "Enter" after typing his choice? Right now, it starts directly after typing "y" or "n".
Thanks to the commentators, that helped a lot! Now its working like this:
#echo off
setlocal EnableDelayedExpansion
:ask
set /P USER_CHOICE="Use internal defaults? (Y/N) "
if /i "%USER_CHOICE%"=="y" (
goto yes
) else (
if /i "%USER_CHOICE%"=="n" (
goto no
) else (
echo "Wrong input! Please choose Y or N!"
goto ask
)
)
rem use default
:yes
set "MYNUMBER=5"
goto run
rem let user define another number
:no
set /P MYNUMBER="Please set a number: "
goto run
rem main routine
:run
echo %MYNUMBER%
pause
I am sort of new to coding, and tried looking around. My goal is to remotely obtain Domain Controller from a machine on the network. The command I am using via batch is below. I want to filter the results from nltest to only display the DC Name. I don't want connection status, and flags.
:Start
color 02
cls
#echo off
Echo.
Echo Domain Controller Finder
Echo.
Set /P Computer=Enter the Asset ID:
If "%Computer%"== "" goto BadName
nltest /sc_query:<Domain_Name> /server:%Computer%
pause
Goto End
:BadName
Cls
Echo.
Echo You have entered an incorrect name or left this field blank
Echo Please enter a valid Name or press Ctr-C to exit.
Echo.
Pause
Goto Start
:End
I'm guessing that the DC Name begins with \\, so you could try this:
#ECHO OFF
:Begin
SET/P "AssetID=Enter the Asset ID: "
IF "%AssetID%"=="" GOTO BadName
SET "DCName="
FOR /F "TOKENS=1* DELIMS=\" %%A IN (
'nlest /sc_query:<Domain_Name> /server:%AssetID%^|FIND "\\"'
) DO SET "DCName=\\%%B"
IF DEFINED DCName (ECHO %%DCName%% = %DCName%
PAUSE & GOTO End)
:BadName
Note. I changed :Start to :Begin because Start is a command and looks wrong, the decision is yours.
Is there a way to write a .bat file which sends all my inputs to a program running in background ?
Something like this ,
c:\start.bat
Opens a new prompt ,
But whatever I type in new prompt should go to the default program running in the background (Don't know where to specify the default program , I don't want to show the program name each time I pass the arguments). I want to use something like this line ,
<CUSTOM_PROMPT>"select data from tablex" , the string should go the "programX.bat"
instead of
<CUSTOM_PROMPT>programX.bat "select data from tablex"
I am afraid your question is not clear; there are many details that needs clarification. However, the Batch file below may give a starting point of discussion for both of us:
#echo off
setlocal
if "%~1" equ "goto" goto %2
cls
echo I am the user-interface program
echo Enter "exit" (with no quotes) to end
echo/
"%~F0" goto getInput | "%~F0" goto background > background.txt
echo/
echo The user-interface program ends
echo/
echo This is the input captured by background program:
echo/
type background.txt
goto :EOF
:getInput
set /P "input=<CUSTOM_PROMPT>: " > CON
echo %input%
if /I "%input%" neq "exit" goto getInput
goto :EOF
:background
set /P input=
if /I "%input%" equ "exit" goto :EOF
echo Input received at %time%:
echo %input%
echo/
goto background
I am making a login system for my OS. And there is a error i dont know how to fix it
#echo off
color 02
IF NOT EXIST Users md Users
:start
:menu
cls
echo ZEROS 0.2.1 LOGIN
echo.
echo [1]-Sign in
echo [2]-Register
echo [3]-User List
echo [4]-Exit
echo.
set /p input=Your choice:
if %input%==1 goto log
if %input%==2 goto reg
if %input%==3 goto ucheck
if %input%==4 exit
echo Invalid Choice
pause
goto menu
:reg
cls
color 02
echo ZEROS 0.2.1 REGISTER
echo.
set /p user="Enter new username: "
echo %user%>>"Users\userlist.txt"
cls
echo ZEROS 0.2.1 REGISTER
echo.
set /p pass="Enter new password: "
echo %pass% >> "Users\%user%.txt"
goto menu
:log
cls
echo ZEROS 0.2.1 LOGIN
echo.
set /p user="Username: "
cls
echo ZEROS 0.2.1 LOGIN
echo.
set /p pass="Password: "
#echo off
set /p password=<Users\%user%.txt
#echo off
if %pass% equ %password% call console.bat
pause
goto menu
:ucheck
IF NOT EXIST Users (
cls
echo There are no users.
pause
goto start
)
:users
if not exist Users\userlist.txt (
goto reg
)
cls
cd Users
echo ZEROS 0.2.1 USERS
echo.
type userlist.txt
pause
goto start
When i choose 3rd option one time, it works well, but if i choose 3rd option 2 or more times, it shows that there are no users. How to fix it?
It's because Users\Users doesn't exist. At some point you cd Users, but never cd ... Logic problem.
I suggest instead of cd, use pushd Users, then popd when you want to return. You should also setlocal at the top of your script. See this page for an explanation of why this is important.
And don't be afraid to add double line breaks between sections and indents in your parenthetical code blocks to improve the readability of your code. This script is about as readable as assembly. :P
I am running a batch file that prompts the user to "enter name". If the user entered nothing and pressed enter, then I want to show the same window over and over again that says:
Enter Name:
The below code is not working:
#echo off
:myDosFunc
set /p name=Enter Name:
IF "%name%"=="" (
call:myDosFunc
) ELSE (
echo %path%
)
pause
GOTO:EOF
call:myDosFunc
I am new to batch, please help.
This is similar to the w0051977 answer, but I belive it more closely matches your original intent.
#echo off
set "name="
:myDosFunc
set /p "name=Enter Name: "
if not defined name goto :myDosFunc
echo %path%
pause
exit /b
If you only want the prompt to appear once, no matter how many times the user presses Enter without entering anything, then you can use:
#echo off
set "name="
<nul set /p "=Enter Name: "
:myDosFunc
set /p "name="
if not defined name goto :myDosFunc
echo %path%
pause
exit /b
I think this is what you are after:
#ECHO OFF
:start
SET /P uname=Please enter your name:
IF "%uname%"=="" GOTO Error
ECHO Hello %uname%, Welcome to DOS inputs!
GOTO End
:Error
goto start
:End
Use the GOTO keyword. Go here for more information: http://www.codeproject.com/Tips/123810/Get-user-input-from-DOS-prompt
The problem is that you are trying to show the PATH without enclosing it with double quotes, and PATH can contain conflictive operators like ) then the batfile closes by an error.
PS: I've made other minor corrections in the code.
#echo off
:myDosFunc
Set "NAME="
set /p "name=Enter Name:"
IF NOT DEFINED NAME (
call :myDosFunc
) ELSE (
echo "%path%"
)
pause
GOTO:EOF
A simple solution would be to add cls before you prompt for the input
:myDosFunc
cls
set /p name=Enter Name: