I'm trying to handle an input argument when I do call script.bat argument
If argument is missing, the receiving script.bat will pop a prompt to ask user to assign value. But right now even if I do script.bat number, the prompt shows up anyway...not sure why.
Code:
:Loop
IF "%1"=="" GOTO Prompt
set VAR=%1
SHIFT
GOTO Loop
:Prompt
set /p VAR="Check which value? "
GOTO Continue
:Continue
You need to skip the :Prompt if the argument was supplied correctly:
:Loop
IF "%1"=="" GOTO Prompt
SET VAR=%1
GOTO Continue
SHIFT
GOTO Loop
:Prompt
set /p VAR="Check which value? "
GOTO Continue
:Continue
Actually, you can now remove the SHIFT and GOTO Loop; as #dbenham posted in the comment below, it's unreachable code. This also eliminates the :Loop itself, as it is never used. This reduces it to:
IF "%1"=="" GOTO Prompt
SET VAR=%1
GOTO Continue
:Prompt
set /p VAR="Check which value? "
GOTO Continue
:Continue
If %1 is set, you have to go to your continue label.
The accepted Ken White solution will work, but it can be greatly simplified.
set "VAR=%~1"
if not defined VAR set /p "VAR=Check which value? "
The user could simply press <Enter> without entering anything. You could introduce a GOTO loop to enforce entry of a value.
set "VAR=%~1"
:loop
if not defined VAR set /p "VAR=Check which value? "&goto :loop
Related
so I'm just wondering if it's possible to to make a batch file read a line of text but split every letter up into it's own variable, ex:
#echo off
:start
set /p text=input:
set out=
set out2=
set /p text=input:
:loop
set out=%out% %text:~0,1%
set out2=%out2:~1% %text:~1%
set text=%text:~1%
if defined text goto loop
echo %out% -%out2%
pause
goto start
What I've written here doesn't work (I was just fiddling around trying to find the answer)
But what I was trying to do was to make "out" and "out2" into 2 separate values. Where "out" would be the first letter typed, and "out2" would be the second letter and so on. (planning to have about 16 out's that can read the first 16 letters of whatever the user inputs and make it into separate varibles)
ex: typing "ab" in the same line would result in "out" being "a" and "out2" being b
Another thing I couldn't figure out either was how to stop "out" from reading everything after the first letter. If anyone could help me with this issue, please explain what you've done to fix it. Thanks in advance
Here is a little trick you can use with CMD.exe and the /U option. The FOR /F command is necessary to capture the output to assign to a variable. I then build a pseudo array with the cnt variable. The SET out command is just used to display all the variables in the pseudo array.
#echo off
setlocal
SET /P "text=INPUT:"
set "cnt=0"
for /F "delims=" %%G IN ('cmd /u /c "echo %text%"^|find /V ""') do (
set /A cnt+=1
CALL SET "out%%cnt%%=%%G"
)
FOR /L %%G IN (1,1,%cnt%) DO CALL echo %%out%%G%%
endlocal
pause
And here is just a quick run of the code.
C:\Users\Squashman\Desktop>so.bat
INPUT:foobar
out1=f
out2=o
out3=o
out4=b
out5=a
out6=r
Press any key to continue . . .
Here is the answer you provided but I fixed your logic errors.
#echo off
setlocal ENABLEDELAYEDEXPANSION
title Test
color a
mode 150
:start
cls
echo test
echo.
set /p text=Input:
set "texttmp=%text%"
set cnt=0
:Reader1
set /a cnt+=1
echo Val-%cnt% = !texttmp:~0,1!
set c[%cnt%]=!texttmp:~0,1!
set "texttmp=%texttmp:~1%"
if "%texttmp%" NEQ "" goto Reader1
I found an easy solution which is way easier to understand (for me at least)
#echo off
setlocal ENABLEDELAYEDEXPANSION
title Test
color a
mode 150
:start
cls
echo test
echo.
set /p text=Input:
set pos=1
:Reader1
echo Val-%pos% = !text:~%pos%,1!
set c[%pos%]=!text:~%pos%,1!
set /a pos=%pos%+1
if "!text:~%pos%,1!" NEQ "" goto Reader1
pause
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
So I am trying to save and load an environment variable of which string value has a space in a batch file. The code works correctly if a variable doesn't have a space, but after closing the program and loading a previous save from file the variable with a space does not display correctly.
Here is the gist of what I'm trying to do:
#echo off
setlocal enabledelayedexpansion
:menu
cls
echo What do you want to do?
echo 1. Set Variables
echo 2. Save and Exit
echo 3. Load
echo 4. Display
set /p input=
if %input%==1 goto set
if %input%==2 goto save
if %input%==3 goto load
if %input%==4 goto display
:set
set "a=apples"
set "b=big bananas"
goto display
:display
echo %a%
echo %b%
pause
goto menu
:save
(echo a=%a%)>>fruit.sav
(echo b=%b%)>>fruit.sav
echo saved!
pause
exit
:load
for /f %%a in (fruit.sav) do set %%a
goto display
In the example given, if the file is saved and then loaded, the variable reference %b% ends up echoing back as just big.
Are there any suggestions on why this isn't working correctly?
In your for loop you have to add "tokens=*", like for /f "tokens=*" %%a in (fruit.sav) do set %%a in order to read the whole line. You could also set "delims=". (As you can see, normally /F reads until line end or space)
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.
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: