Removing last character from a variable in a Batch file - windows

I'm trying to assign a value to a variable called "doNotLog" based on the last character of some other variable called "choice" through a Batch file.
The structure of the variable choice is:
1) Either an integer with 1 or more digits
2) Or an integer with 1 or more digits plus character "n" at the last
The objectives are:
1) To set the value of "doNotLog" to true if the last character of "choice" is n
2) To finally remove n from "choice"
The Batch file I'm using to achieve this is:
#echo off
echo enter choice
set/p choice=
set doNotLog=false
setlocal ENABLEDELAYEDEXPANSION
if %choice:~-1,1%==n (
set doNotLog=true
set choice=!choice:n=!
)
endlocal
echo After changes:
echo choice= %choice%
echo donotLog= %doNotLog%
#pause
It produces the following output:
enter choice
54n
After changes:
choice= 54n
donotLog= false
Press any key to continue . . .
However, I was expecting the following output:
enter choice
54n
After changes:
choice= 54
donotLog= true
Press any key to continue . . .
How do I achieve my desired output

Variable expansion is described under the help information for the Set command. Open a Command Prompt window and enter set /? to read it.
Here's some helper examples for you:
C:\Users\Mohd>Set "Variable=String"
C:\Users\Mohd>Echo(%Variable%
String
C:\Users\Mohd>Echo(%Variable:~0,-1%
Strin
C:\Users\Mohd>Echo(%Variable:~0,1%
tring
C:\Users\Mohd>Echo(%Variable:~0,-2%
Stri
C:\Users\Mohd>Echo(%Variable:~-1,1%
g
C:\Users\Mohd>Echo(%Variable:~1,-1%
trin
C:\Users\Mohd>Echo(%Variable:~1,1%
t
C:\Users\Mohd>Echo(%Variable:~1,2%
tr
C:\Users\Mohd>Echo(%Variable:~1,-2%
tri
C:\Users\Mohd>Echo(%Variable:~2,1%
r
C:\Users\Mohd>Echo(%Variable:~2,-1%
rin
C:\Users\Mohd>Echo(%Variable:~2,-2%
ri
C:\Users\Mohd>Echo(%Variable:~-2,2%
ng
C:\Users\Mohd>Echo(%Variable:~-2,1%
n
C:\Users\Mohd>Echo(%Variable:~2,-1%
rin

The issue is that you've tried to modify doNotLog and choice inside a setlocal, so you've set them locally, but the global ones remain unchanged.
Get the value of them inside setlocal to achieve the expected result:
#echo off
echo enter choice
set/p choice=
set doNotLog=false
setlocal ENABLEDELAYEDEXPANSION
if %choice:~-1,1%==n (
set doNotLog=true
set choice=!choice:n=!
)
echo After changes:
echo choice= %choice%
echo donotLog= %doNotLog%
#pause
endlocal
If you want to disable the delayed expansion anyway, you can put the rest of the code in another setlocal, that disables it:
#echo off
echo enter choice
set/p choice=
set doNotLog=false
setlocal ENABLEDELAYEDEXPANSION
if %choice:~-1,1%==n (
set doNotLog=true
set choice=!choice:n=!
)
setlocal DISABLEDELAYEDEXPANSION
echo After changes:
echo choice= %choice%
echo donotLog= %doNotLog%
#pause
:: We have to end two locals now...
endlocal
endlocal
...however, if you do this multiple times, your code will quickly become unmaintainable (oh, wait, aren't all batch files unmaintainable?) and less performant.
Alternatively, you can move variables out of local by using the magic of single-line or parenthesis-grouped commands, and the classic %variable% syntax:
endlocal & set "doNotLog=%doNotLog%" & set "choice=%choice%"
...or the equivalent, but more readable...
(
endlocal
set "doNotLog=%doNotLog%"
set "choice=%choice%"
)
The above solutions might look silly, but they do work...
Both of the above will set global variables, because they're after endlocal, but read the local ones, as they're substituted before the evaluation of the line (or the grouped structure) starts. (That's why these hacks work with the %var% syntax, but not with the !var!)
So, your code can even be changed to:
#echo off
echo enter choice
set/p choice=
set doNotLog=false
setlocal ENABLEDELAYEDEXPANSION
if %choice:~-1,1%==n (
set doNotLog=true
set choice=!choice:n=!
)
endlocal & (
set "doNotLog=%doNotLog%"
set "choice=%choice%"
)
echo After changes:
echo choice= %choice%
echo donotLog= %doNotLog%
#pause
...which may be the best solution among these.

So, what about without using SetLocal EnableDelayedExpansion
#echo off
set "doNotLog=" && set "choice="
for /f ^skip^=^4 %%a in ('echo/ prompt $h^| cmd
')do echo/ && set/p "choice=-%%a Enter choice: "
echo/%choice:~-1%|find /i "n">nul && (
call set "choice=%choice:~0,-1%" && set "doNotLog=true")
if "%doNotLog%" == "" set "doNotLog=false"
echo/ choice = %choice% && echo/ doNotLog = %doNotLog%
echo/ Press any key to continue... & timeout -1 >nulF
outputs for input 54n and 54 ::
G:\SO_en-EN\Q59738810>Q59738810.cmd
Enter choice: 54n
choice = 54
doNotLog = true
Press any key to continue...
---------------------------------------
G:\SO_en-EN\Q59738810>Q59738810.cmd
Enter choice: 54
choice = 54
doNotLog = false
Press any key to continue...

Related

Windows Batch SET with Variable Substring Length?

Take this simple example:
#ECHO OFF
SET /P phrase="Enter Word : "
SET /a rnum=%random% %%10 +1
ECHO %phrase%
ECHO %rnum%
SET rchar=%phrase:~0,%rnum%%
ECHO %rchar%
Pause
I just want to be able to pass that rnum variable to pick that as the character chosen from the left of that user entered word to that random character.
I can't seem to figure out how to pass that as a variable.
I tried with enabledelayedexpansion with no luck:
#ECHO OFF
SET /P Phrase="Enter Word : "
SET /a rnum=%random% %%10 +1
ECHO %phrase%
ECHO %rnum%
setlocal enabledelayedexpansion
SET rchar=!phrase:~0,%rnum%!
endlocal
ECHO %rchar%
Pause
So how do I pass rnum as a variable in this instance? Thanks for any assistance.
Here's a simple modification of your example delayed expansion code, which shows one method of maintaining your variable value beyond endlocal:
#ECHO OFF
SET /P "phrase=Enter Word : "
SET /A rnum = %RANDOM% %% 10 + 1
ECHO %phrase%
ECHO %rnum%
SETLOCAL ENABLEDELAYEDEXPANSION
FOR %%G IN ("!phrase:~0,%rnum%!") DO ENDLOCAL & SET "rchar=%%~G"
ECHO rchar=%rchar%
PAUSE
The above example should be fine, as long as the end user does not begin to input strings with problematic characters. If you wanted to make it a little more robust for such scenarios then perhaps this will help:
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
:AskString
Rem Get interactive string input
Set "String="
Set /P String="Enter Word : "
If Not Defined String GoTo AskString
Set String
Rem Generate a random integer 1..10
Set /A "Integer = (%RANDOM% %% 10) + 1"
Set Integer
Rem Create a substring variable using %String% and %Integer%
Echo %%SubString%% = %%String:~0,%Integer%%%
SetLocal EnableDelayedExpansion
For /F Delims^=^ EOL^=^ UseBackQ %%G In ('"!String:~0,%Integer%!"') Do (
EndLocal
Set "SubString=%%~G"
)
Set SubString
Pause
Please note that the above code uses Set Variable to display the variable name along side its value. If your variable contains certain poison characters just using Echo %Variable% may not work, and you would probably be better off keeping delayed expansion enabled at that time.
As Compo already comments, the position of your endlocal is the problem.
You could just move the endlocal after the echo
#ECHO OFF
setlocal enabledelayedexpansion
SET /P Phrase="Enter Word : "
SET /a rnum=%random% %%10 +1
ECHO !phrase!
ECHO !rnum!
SET "rchar=!phrase:~0,%rnum%!"
ECHO !rchar!
endlocal

Reference element by index in a list using Batch Script

Trying to obtain an element in a list by its index, using batch script. Here is the code:
#Echo off
setlocal EnableDelayedExpansion
set acc[0]=default
set acc[1]=Account_2
set acc[2]=Account_3
set acc[3]=Account_4
set acc[4]=Account_5
if exist interator.txt (
set /p i=<interator.txt
echo "read: !i!"
echo "!acc[%i%]!"
REM start cmd /c setx AWS_PROFILE !acc[%i%]!
REM start cmd /k python script.py
set /A i=i+1
(echo !i!)>interator.txt
echo "write: !i!"
) else (
(echo 0)>interator.txt
)
Output Received:
"read: 0"
""
"write: 1"
As setx requires the CMD session to be closed, for affect to take place. I am trying a different approach to automate some regular stuff.
Expected Output:
"read: 0"
"default"
"write: 1"
#Echo off
setlocal EnableDelayedExpansion
set "acc[0]=default
set "acc[1]=Account_2"
set "acc[2]=Account_3"
set "acc[3]=Account_4"
set "acc[4]=Account_5"
if exist q65771965.txt (
set /p i=<q65771965.txt
echo "read: !i!"
FOR %%a IN (acc[!i!]) DO (
ECHO "!%%a!"
echo start cmd /c setx AWS_PROFILE "!%%a!"
echo start cmd /k python script.py
)
set /A i=i+1
(echo !i!)
echo "write: !i!"
) else (
(echo 0)
)
GOTO :EOF
OK - small changes to allow this to work on my test environment:
Changed name of file from interator.txt to q65771965.txt (suits my environment)
Removed updating of data file so the modifications are shown on-screen.
Replaced REM start with ECHO start to show the start commands on-screen.
Subtle syntax-oriented change : Use set "var1=data" for setting values - this avoids problems caused by trailing spaces.
Significant change : insert a for loop to transfer indirect values to a metavariable (%%a) and use these.
Possibly-required : I don't use setx much, but I've some memory of the argument's needing to be "quoted"
The problem is, you used echo "%acc[!i!]%" within a codeblock. You need another layer of parsing, like call echo "%%acc[!i!]%%"
As an alternative, restructure your code, so the critical part isn't in a code block:
#Echo off
setlocal EnableDelayedExpansion
set acc[0]=default
set acc[1]=Account_2
set acc[2]=Account_3
set acc[3]=Account_4
set acc[4]=Account_5
if not exist interator.txt (
(echo 0)>interator.txt
goto :eof
)
set /p i=<interator.txt
echo "read: !i!"
echo "%acc[!i!]%"
set /A i=i+1
(echo !i!)>interator.txt
echo "write: !i!"
(this code is functionally identically to yours, just structured in another way)
(btw: it should probably iterator, not interator - but that's only spelling)

Why I can't get output from this batch script? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
This program stops automatically! When I enter correct name and password (i.e it is not executing from 8th line). Please check if I made any mistakes.
#echo off
:s
set t=0
set /p a= enter your name
set /p b= enter your password
if %a%==andrew(set /a t=%t%+1)
if %b%==123 (set /a t=%t%+2 )
if %t%==3 ( echo welcome andrew
echo ---------------------------------------------------------------------------
:r
set /p c=want to change your settings -yes -no
if %c%==no (echo ok
goto e
)
if %c%==yes (set /p p= what you want to change -name(1) -password(2)
if %p%==1 (set /p a=enter name to change)
if %p%==2 (set /p a=enter password to change)
)
)
if %t%==2 echo %a% is not authorised
if %t%==1 echo enter correct password
if %t%==0 echo Both name and id are wrong enter again
goto r
goto s
:e
pause
Set #echo on and you may see something like this when the script is executed:
--> set t=0
--> set /p a= enter your name
enter your nameandrew
--> set /p b= enter your password
enter your password123
--> if andrew == andrew(set /a t=0+1)
--> if 123 == 123 (set /a t=0+2 )
(echo was unexpected at this time.
--> if ==no (echo ok
The script stopped with error:
(echo was unexpected at this time.
Line 7 showed OK:
if 123 == 123 (set /a t=0+2 )
though, next command output seen is:
if ==no (echo ok
which means %c% is undefined as you can see a value missing left of the == that caused the error.
This could be happening as to the opening parentheses in line 8:
if %t%==3 ( echo welcome andrew
The parser will see the opening parentheses and will keep reading the lines until it reaches a closing parentheses. It will substitute the percentage enclosed variables before execution. This makes %c% undefined as %c% is not defined before the opening parentheses on line 8 began the read.
So it seems the parser read:
if %t%==3 ( echo welcome andrew
echo ---------------------------------------------------------------------------
:r
set /p c=want to change your settings -yes -no
if %c%==no (echo ok
goto e
)
And does variable substitution of known values of t=3 and c=:
if 3==3 ( echo welcome andrew
echo ---------------------------------------------------------------------------
:r
set /p c=want to change your settings -yes -no
if ==no (echo ok
goto e
)
The error of if ==no (echo ok is shown.
Your question in the title:
why i cant get output from this batch script?
It is the opening parentheses in line 8. So one should ask thy self... What is that opening parentheses doing there in line 8 when it will include a label :r and a set /p that sets a variable c that can cause an undefined variable?
If the opening parentheses is meant to be there, view set /? that may show an example like:
set VAR=before
if "%VAR%" == "before" (
set VAR=after
if "%VAR%" == "after" #echo If you see this, it worked
)
The example code and the details in set /? informs you how set variables work.
The label between the parentheses would still be a concern as it can cause error in a read block of code.
There is a couple of things. Do not set single character variables or label names. rather use set vara than using set a but making things recognisable like using variables that means something, makes things less confusing.
when matching variables with a value using == double quote each side. i.e if "%var" == "123"
For selection, use choice instead of set /p
Also, when using set /a the better way to add is set /a vart+=1 instead of set /a %vart%+1
This should do more or less what you want.
#echo off
:start
set vart=0
set /p "name=Enter your name: "
set /p "pass=Enter your password: "
if /i "%name%" == "andrew" set /a vart+=1
if "%pass%" == "123" set /a vart+=2
if %vart% equ 3 echo welcome andrew
echo ---------------------------------------------------------------------------
:settings
choice /c YN /m "Change your settings?"
if %errorlevel% equ 0 goto opt0
goto opt%errorlevel%
:opt0
echo You Pressed CTRL+C and selected "N"
exit /b 1
:opt2
pause
goto :eof
:opt1
choice /c UP /m "Change your User or Password?"
if %errorlevel% equ 0 goto opt0
goto chng%errorlevel%
:chng2
set /p "name=Enter name to change: "
goto :eof
:chng1
set /p "pass=Enter password to change: "
if %vart% equ 2 echo %a% is not authorised
if %vart% equ 1 echo enter correct password
if %vart% equ 0 echo Both name and id are wrong enter again
goto settings
goto start
Please read the syntax of the commands, such as if and set before posting.
Addressing some problems in your code:
First,
if %a%==andrew(set /a t=%t%+1)
is intercepted by cmd.exe as:
if %a% is andrew(set, run a command called /a t=%t%+1. Also, please double quote all set /p ...= in your script, even though it can't handle poison characters.
Second, you have unescaped closing parenthesis ) in your if, which can cause interception errors.

"Bullet Proof" Validation in Batch

This is a fairly general question on user input validation, with particular focus on special characters. In this "simplest" case one character validation is sought.
The code is modified from a 5 year old thread but it is apparent that most of the special characters require extra treatment to what is proposed below:
Setlocal EnableDelayedExpansion
:Prompter
REM Eliminate poison chars on <CR>
SET "validateText="
SET /P "validateText=Type f to foo or b to bah. (f/b)"
call :Validate
if /i %validateText%==f (echo foo pause>nul goto :eof
) else (
if /i %validateText%==b (echo bah pause>nul goto :eof
) else (goto Prompter))
:Validate
SET Input=%validateText:~0,1%
For %%G In (^= ^| ^& ^< ^> ^^ + ^( ^) \ / . # # $ { } [ ] ' : ` ^%% ^") Do (
REM Test Script fails for = | & < > ^ ) ; " and space
REM Loops through the set, but the if clauses are never evaluated
If %Input%==%%G (#echo isequal pause>nul exit /b) else (echo isnotequal pause>nul exit /b))
REM quotes on the test clause also fail
rem If "%validateText%==%%G" (echo equal pause >nul goto :eof) else (echo not equal pause >nul goto :eof))
REM The following are what is intended. All fail
rem If "%validateText%==%%G" (set Input=!Input:%%G=! goto BangDO) else (set Input=%validateText% goto :eof))
rem If %validateText%==%%G (set "Input=%Input:%%G=%" goto BangDO) else (set "Input=%validateText%" goto :eof))
:BangDO
#echo %Input%
pause
Set Input=!Input:%=!
REM Remove !
SetLocal DisableDelayedExpansion
Set "%Input:!=X"
Setlocal EnableDelayedExpansion
Set "validateText=%Input%"
#echo %validateText%
pause
The question is, that given there haven't been updates to the Command processor (at least since the quoted post) why it doesn't work. Either improve on this attempt at validation or provide own code.
The answer is almost staring you in the face :-) You enable delayed expansion, but then you fail to use it!
One beautiful feature of delayed expansion is you never have to worry about poison characters in the content.
One other thing, set /p will preserve any existing variable value if the user simply presses <Enter> without entering a value. That may or may not be what you want. I usually explicitly clear the variable value before using set /p
setlocal EnableDelayedExpansion
REM set /p does not clear existing value if user enters empty string
REM Remove REM below if you want to ignore any existing str value
REM set "str="
:prompter
set /p "str=Type f to foo or b to bah. (f/b) "
if /i !str!==f (
echo foo
) else if /i !str!==b (
echo bah
) else goto prompter
pause >nul
In your case only one replacement is necessary to be secure.
SET "validateText="
SET /P "validateText=Type f to foo or b to bah. (f/b)"
set "validateText=%validateText:"=""%"
if /i "%validateText%"=="f" (
echo foo pause>nul goto :eof
) else if /i "%validateText%"=="b" (
echo bah pause>nul goto :eof
) else (
goto Prompter
)
This works as all special characters are safe inside quotes.
And you only need to be sure that a quote inside your text can't cause problems, this can be achieved by doubling all quotes.

Batch File input validation - Make sure user entered an integer

I'm experimenting with a Windows batch file to perform a simple operation which requires the user to enter a non-negative integer. I'm using simple batch-file techniques to get user input:
#ECHO OFF
SET /P UserInput=Please Enter a Number:
The user can enter any text they want here, so I would like to add some routine to make sure what the user entered was a valid number. That is... they entered at least one character, and every character is a number from 0 to 9. I'd like something I can feed the UserInput into. At the end of the routine would be like an if/then that would run different statements based on whether or not it was actually a valid number.
I've experimented with loops and substrings and such, but my knowledge and understanding is still slim... so any help would be appreciated.
I could build an executable, and I know there are nicer ways to do things than batch files, but at least for this task I'm trying to keep it simple by using a batch file.
You're probably not doing this in a DOS batch file. Or at least, support for set /p is unheard of for me in DOS :-)
You could use substrings. In fact I have written a parser for a specific regular language that way once, but it's cumbersome. The easiest way would probably be to assign the contents of %userinput% to another variable, using set /a. If the result comes out as 0 you need to check whether the input itself was 0, otherwise you can conclude it was a non-number:
#echo off
setlocal enableextensions enabledelayedexpansion
set /p UserInput=Enter a number:
set /a Test=UserInput
if !Test! EQU 0 (
if !UserInput! EQU 0 (
echo Number
) else (
echo Not a number
)
) else (
echo Number
)
However, this works only for numbers in the range of Int32. If you just care for any number (possibly floating-point as well) then you need to resort to the loop-based approach of dissecting it.
NOTE: Updated to solve the space issues. However, there is still a problem lurking: Entering 123/5 yields "number", since set /a can evaluate this ...
Thanks all. I was trying to make it harder for myself looking at loops and string manipulation. I used your tips on math evaluation and comparison. Here's what I finally came up with as my concept script:
:Top
#ECHO OFF
ECHO.
ECHO ---------------------------------------
SET /P UserInput=Please Enter a Number:
ECHO.
ECHO UserInput = %UserInput%
ECHO.
SET /A Evaluated=UserInput
ECHO Math-Evaluated UserInput = %Evaluated%
if %Evaluated% EQU %UserInput% (
ECHO Integer
IF %UserInput% GTR 0 ( ECHO Positive )
IF %UserInput% LSS 0 ( ECHO Negative )
IF %UserInput% EQU 0 ( ECHO Zero )
REM - Other Comparison operators for numbers
REM - LEQ - Less Than or Equal To
REM - GEQ - Greater Than or Equal To
REM - NEQ - Not Equal To
) ELSE (
REM - Non-numbers and decimal numbers get kicked out here
ECHO Non-Integer
)
GOTO Top
This method catches all numbers and can detect whether it's positive, negative, or zero. Any decimal or string will be detected as non-integers. The only edge case I've found is a string with spaces. For example, the text "Number 1" will cause the script to crash/close when the user input is evaluated as math. But in my situation, this is fine. I don't want my script to go on with invalid input.
You can also use a quite simple trick:
echo %userinput%|findstr /r /c:"^[0-9][0-9]*$" >nul
if errorlevel 1 (echo not a number) else (echo number)
This uses findstr's regular expression matching capabilities. They aren't very impressive but useful at times.
This is the same idea as that of Johannes..
SET /A sets a numeric value. If the input is not a number, it changes it to 0.
That's what you can exploit here to do your check.
#ECHO OFF
SET /P UserInput=Please Enter a Number:
IF %UserInput% EQU 0 GOTO E_INVALIDINPUT
SET /A UserInputVal="%UserInput%"*1
IF %UserInputVal% GTR 0 ECHO UserInput "%UserInputVal%" is a number
IF %UserInputVal% EQU 0 ECHO UserInput "%UserInputVal%" is not a number
GOTO EOF
:E_INVALIDINPUT
ECHO Invalid user input
:EOF
As an alternative, you could always create a little javascript file and call it from your batchfile. With parseInt() you could force the input to be an integer, or you could roll your own function to test the input.
Writing the javascript is just as fast as the batchfile, but it's much more powerful. No IDE or compiler required; notepad will do. Runs on every windows box, just like your batchfiles. So why not make use of it?
You can even mix batchfiles and javascript. Example:
contents of sleep.js:
var SleepSecs=WScript.Arguments.Item(0);
WScript.Sleep(SleepSecs*1000)
contents of sleep.cmd:
cscript /nologo sleep.js %1
You can now call this from a batchfile to make your script sleep for 10 seconds. Something like that is difficult to do with just a plain batchfile.
sleep 10
As pointed out by ghostdog74, the answers posted by Joey Mar 26 '09 (score 10) and Wouter van Nifterick Mar 26 '09 (score 5) don't work.
The answer posted by Joey Mar 25 '10 (score 2) does work, except that redirection symbols and '&' cause syntax errors.
I think the best and simplest solution is the one posted by Sager Oct 8 '14 (score 0). Unfortunately, it has a typo: ‘"%a"’ should be ‘"%a%"’.
Here's a batch file based on Sager's answer. Redirection symbols and '&' in the input don't cause problems. The only problems I could find were caused by strings containing double quotes.
#echo off & setlocal enableextensions & echo.
set /p input=Enter a string:
SET "x=" & for /f "delims=0123456789" %%i in ("%input%") do set x=%%i
if defined x (echo Non-numeral: "%x:~0,1%") else (echo No non-numerals)
In addition to the remark about the error that occures when spaces are part of the users input. You can use errorlevel errorlevel=9165. It can be used for the spaces in a string or for the error handling of 'no' input.
Kind Regards,
Egbert
You might also like this one - it's short and easy. This one use the multiplication trick to set TestVal. Comparing TestVal against UserInput allows all numeric values to get through including zeroes, only non-numerics will trigger the else statement. You could aslo set ErrorLevel or other variables to indicate a failed entry
#ECHO OFF
SET TestVal=0
SET /P UserInput=Please Enter a Number:
SET /A TestVal="%UserInput%"*1
If %TestVal%==%UserInput% (
ECHO You entered the number %TestVal%
) else ECHO UserInput "%UserInput%" is not a number
GOTO EOF
:EOF
I know this is years old, but just to share my solution.
set /p inp=Int Only :
:: Check for multiple zeros eg : 00000 ::
set ch2=%inp%-0
if %inp% EQU 0 goto :pass
if [%inp%]==[] echo Missing value && goto :eof
if %inp:~0,1%==- echo No negative integers! && goto :eof
set /a chk=%inp%-10>nul
if %chk%==-10 echo Integers only! && goto :eof
:pass
echo You shall pass
:eof
Tested and working on Windows 8.
you can reinvent the wheel and grow a few white hairs doing string validation in batch, or you can use vbscript
strInput = WScript.Arguments.Item(0)
If IsNumeric(strInput) Then
WScript.Echo "1"
Else
WScript.Echo "0"
End If
save it as checkdigit.vbs and in your batch
#echo off
for /F %%A in ('cscript //nologo checkdigit.vbs 100') do (
echo %%A
rem use if to check whether its 1 or 0 and carry on from here
)
You can validate any variable if its number:
SET "var="&for /f "delims=0123456789" %i in ("%a") do set var=%i
if defined var (echo."NIC">nul) else (echo."number")
If you want some sort of a loop and default set up for that particular question, then here's my method for doing this.
Notes on the code within.
#echo off
setlocal EnableDelayedExpansion
set "ans1_Def=2"
:Q1
set /p "ans1=Opt 1 of 1 [Value 1-5 / Default !ans1_Def!]: "
:: If not defined section. This will use the default once the ENTER key has been
:: pressed and then go to :Q2.
if not defined ans1 (
echo/ & echo ENTER hit and the default used. Default is still: !ans1_Def! & echo/
set "ans1=!ans1_Def!" && goto :Q2 )
:: This section will check the validity of the answer. The "^[1-5]$" will work
:: for only numbers between one and five in this example but this can be changed
:: to pretty much suit the majority of cases. This section will also undefine
:: the ans1 variable again so that hitting the ENTER key at the question
:: will work.
echo %ans1%|findstr /r /c:"^[1-5]$" >nul
if errorlevel 1 (
echo/ & echo At errorlevel 1. Wrong format used. Default is still: !ans1_Def! & echo/
set "ans1=" && goto Q1
) else ( echo Correct format has been used. %ans1% is the one. && goto :Q2 )
:Q2
echo/
echo -----------------------------
echo/
echo Now at the next question
echo !ans1!
echo/
pause
exit
Try this:
set /p numeric=enter a number
(
(if errorlevel %numeric% break ) 2>nul
)&&(
echo %numeric% is numeric
)||(
echo %numeric% is NOT numeric
)
Just try this
#echo off
SET constNum=100
:LOOP
Set /p input=Please input a number less than %constNum% :
if "%input%" == "" echo Blank is not allowed & goto LOOP
SET "notNumChar="
for /f "delims=0123456789" %%i in ("%input%") do set notNumChar=%%i
if defined notNumChar (
echo %input% is a string
goto LOOP
) else (
REM Remove leading 0 if it has. eg: 08→8
FOR /F "tokens=* delims=0" %%A IN ("%input%") DO SET inputNum=%%A
)
REM Compare
if defined inputNum (
echo %inputNum%
if %inputNum% equ %constNum% & goto LOOP
if %inputNum% gtr %constNum% & goto LOOP
if %inputNum% lss %constNum% & goto CONTINUE
)
:CONTINUE
:: Your code here
:ASK
SET /P number= Choose a number [1 or 2]:
IF %number% EQU 1 GOTO ONE
IF %number% NEQ 1 (
IF %number% EQU 2 GOTO TWO
IF %number% NEQ 2 (
CLS
ECHO You need to choose a NUMBER: 1 OR 2.
ECHO.
GOTO ASK
)
)
It works fine to me. If he chooses numbers less or greater, strings, floating number etc, he wil receive a message ("You need to choose a NUMBER: 1 OR 2.") and the INPUT will be asked again.
#echo off
setlocal enableextensions enabledelayedexpansion
set /p UserInput=Enter a number:
set /a Test=UserInput
if !Test! EQU 0 (
if !UserInput! EQU 0 (
echo Number
) else (
echo Not a number
)
) else (
echo Number
)
yeaph everthing is great
but you forget about one little thing
0 also is a digit
;(
This is more of a user friendly way.
if %userinput%==0 (
cls
goto (put place here)
)
if %userinput%==1 (
cls
goto (put place here)
)
if %userinput%==2 (
cls
goto (put place here)
)
if %userinput%==3 (
cls
goto (put place here)
)
if %userinput%==4 (
cls
goto (put place here)
)
if %userinput%==5 (
cls
goto (put place here)
)if %userinput%==6 (
cls
goto (put place here)
)if %userinput%==7 (
cls
goto (put place here)
)
if %userinput%==8 (
cls
goto (put place here)
)
if %userinput%==9 (
cls
goto (put place here)
)
This can be used for any type of user input.
for me this is working for all non-zero values ..should i be cautious of some rare cases?
set /a var = %1
if %var% neq 0 echo "it is number"
pause

Resources