I have a batch script which :
check the files in a directory and check if it exists in another
directory and it should not exists there
count each file with a specific format, there should be just one of each
if both of the above statement is true then generate a success file.
Below is my code which is working fine:
SET /A file1=file2=Val=0
SET /A FileE=1
set /a flagname=1
for %%A in (*ABC*.txt) do set /a file1+=1
for %%A in (*XYZ*.txt) do set /a file2+=1
for %%i in ("*") do if exist "Processed\%%~nxi" SET /A FileE=0
SET /A Val=%file1%*%file2%*%FileE%
if %Val% EQU 1 (
echo SUCESS>Sucess.txt
SET Flag=Sucess
echo %Flag%) else (
if %file1% EQU 0 ( echo Missing ABC.txt files >> Error.txt)
if %file1% GTR 1 ( echo More than 1 ABC.txt files >> Error.txt)
if %file2% EQU 0 ( echo Missing XYZ.txt files >> Error.txt)
if %file2% GTR 1 ( echo More than 1 XYZ.txt files >> Error.txt)
(for %%i in ("*") do if exist "Processed\%%~nxi" echo(File Exists in
Processed
Folder %%~i)>>Error.txt
SET Flag=FAILURE
echo %Flag%)
My problem is how to transform above code to iterate over a list of number of files like 100 ? Below is the code which I tried :
#echo off
setlocal enable delayed expansion
Set Filen[0]=ABC*.txt
Set Filen[1]=XYZ*.txt
SET /A Val=1
SET /A File1=0
FOR /l %%G in (0,1,1) Do (
echo !Filen[%%G]! hi
set File1=0
echo %file1% Count
for %%A in (!Filen[%%G]! ) do (
set File1=!File1!+1
echo %%A %file1%)
)
Put your search words in a string and iterate over it:
Set "Search=ABC DEF XYZ"
For %%A in (%Search%) do (
Or in a file and read one by one
For /f "usebackq" %%A in ("Search.txt") Do (
With this file Search.txt
ABC
DEF
XYZ
In the environment
> tree a:\ /F
Auflistung der Ordnerpfade für Volume RamDisk
A:\
└───Test
│ ABC_123.txt
│ DEF_456.txt
│ Search.txt
│
└───Processed
The following batch
:: Q:\Test\2018\07\01\SO_51120147.cmd
#Echo off & Setlocal EnableDelayedExpansion
Set "BaseDir=A:\Test"
Set Err=^>^> "Error.txt" Call Echo=[%%date%% %%time%%]
PushD "%BaseDir%" || (%Err% can't locate %BaseDir% & Pause &Goto :Eof)
%Err% Job %~f0 started by %USERNAME%
Set " Flag=Sucess"
:: Uncomment for string variant
:: Set "Search=ABC DEF XYZ"
:: For %%A in (%Search%) do (
:: use the file variant
For /f "usebackq" %%A in ("Search.txt") Do (
Set Cnt=0
For %%B in (%%A*.txt) Do Set /A Cnt=1
if !Cnt! NEQ 1 (
%Err% Count of %%A*.txt file=[!Cnt!]
SET Flag=FAILURE
)
)
For %%A in (*) do if exist "Processed\%%~nxA" (
%Err% File %%A does exist in Processed Folder
Set Flag=FAILURE
)
%Err% Job %~f0 terminated with %Flag%
Yields this output:
> type A:\Test\Error.txt
[2018-07-01 13:10:34,43] Job Q:\Test\2018\07\01\SO_51120147.cmd started by LotPings
[2018-07-01 13:10:34,45] Count of XYZ*.txt file=[0]
[2018-07-01 13:10:34,47] Job Q:\Test\2018\07\01\SO_51120147.cmd terminated with FAILURE
EDIT: Explanation of the somehow tricky line:
Set Err=^>^> "Error.txt" Call Echo=[%%date%% %%time%%]
To not have to pre/append every line which should go to the error log with
the redirection and a (fixed) file name I put these together with the
echo command and a [date time] stamp into a variable.
To avoid immediate interpretation when setting the variable, the '>' have to
be esaped with a caret, and to delay the expansion of the %-signs these have
to be doubled. (otherwise each log entry had the same date time)
To force expansion of date time when echoing the (pseudo) call is neccessary.
Inside my working directory I have files named after the year, month, day and hour of retrieval, like this:
19980101.00.nc 19980101.03.nc 19980101.06.nc 19980101.09.nc
19980101.12.nc 19980101.15.nc 19980101.18.nc 19980101.21.nc
19980102.00.nc 19980102.03.nc 19980102.06.nc 19980102.09.nc
19980102.12.nc 19980102.15.nc 19980102.18.nc 19980102.21.nc
19980103.00.nc 19980103.03.nc 19980103.06.nc 19980103.09.nc
19980103.12.nc 19980103.15.nc 19980103.18.nc 19980103.21.nc
And using the windows command line I would like to be able to operate with only this files:
19980101.15.nc |
19980101.18.nc |
19980101.21.nc _|_ # these are day 01 files
19980102.00.nc |
19980102.03.nc |
19980102.06.nc |
19980102.09.nc |
19980102.12.nc _|_ # these are day 02 files
So far I've used the command: dir 19980101.*.nc in order to list 19980101 files but I don't know how to omit the non desired files (I only need 19980101.15.nc, 19980101.18.nc and 19980101.21.nc files form day 01). Moreover, I haven't figured out how to combine in a single list file's names of day two (02 files) and files of day one (01 files).
Any thoughts?
The question is very unclear, but here is nevertheless a commented batch file which prompts the user for a file date like 19980102, determines the previous day for input date and runs DIR with the 8 files to print.
#echo off
setlocal EnableExtensions EnableDelayedExpansion
rem Prompt user for date string until a number string with exactly 8 digits is entered.
:EnterFileDate
set "FileDate="
set /P "FileDate=Enter date in format YYYYMMDD: "
rem Has the user nothing entered at all?
if not defined FileDate goto EnterFileDate
rem Has the user entered a string not consisting of only digits?
for /F "delims=0123456789" %%I in ("!FileDate!") do goto EnterFileDate
rem Has the user entered a too long string?
if not "%FileDate:~8%" == "" goto EnterFileDate
rem Has the user entered a too short string?
if "%FileDate:~7%" == "" goto EnterFileDate
rem Has the user entered a string not starting with 19 or 20 for century?
if not "%FileDate:~0,2%" == "19" if not "%FileDate:~0,2%" == "20" goto EnterFileDate
rem Don't care of possible other invalid date strings, but
rem check if the entered date string matches any existing file.
if not exist "%FileDate%.??.nc" (
echo/
echo There is no file "%FileDate%.??.nc".
echo/
goto EnterFileDate
)
rem Discard local environment and pass entered file date
rem to previous environment restored by command ENDLOCAL.
endlocal & set "FileDate=%FileDate%"
set "PrevYear=%FileDate:~0,4%"
set "PrevMonth=%FileDate:~4,2%"
rem Get day in month subtracted by 1 with avoiding to get 01 to 09
rem interpreted as octal number by concatenating this two digits
rem of day in month first with 1 for integer number 101 to 132.
set /A PrevDay=1%FileDate:~6,2% - 101
rem Is day in month greater or equal 1, month must not be changed.
if %PrevDay% GEQ 1 goto BuildPrevDate
set /A PrevMonth=1%PrevMonth% - 101
rem Is month less than 1, year must be changed too.
if %PrevMonth% LSS 1 (
set /A PrevYear-=1
set "PrevMonth=12"
set "PrevDay=31"
goto BuildPrevDate
)
rem Determine last day in new month and year.
if %PrevMonth% == 1 set "PrevDay=31" & goto BuildPrevDate
if %PrevMonth% == 3 set "PrevDay=31" & goto BuildPrevDate
if %PrevMonth% == 4 set "PrevDay=30" & goto BuildPrevDate
if %PrevMonth% == 5 set "PrevDay=31" & goto BuildPrevDate
if %PrevMonth% == 6 set "PrevDay=30" & goto BuildPrevDate
if %PrevMonth% == 7 set "PrevDay=31" & goto BuildPrevDate
if %PrevMonth% == 8 set "PrevDay=31" & goto BuildPrevDate
if %PrevMonth% == 9 set "PrevDay=30" & goto BuildPrevDate
if %PrevMonth% == 10 set "PrevDay=31" & goto BuildPrevDate
if %PrevMonth% == 11 set "PrevDay=30" & goto BuildPrevDate
rem Leap year finding for the years 1901 to 2099.
set /A LeapYear=PrevYear %% 4
if %LeapYear% == 0 (
set "PrevDay=29"
) else (
set "PrevDay=28"
)
set "LeapYear="
:BuildPrevDate
if %PrevDay% LSS 10 set "PrevDay=0%PrevDay%"
if %PrevMonth% LSS 10 set "PrevMonth=0%PrevMonth%"
set "PrevDate=%PrevYear%%PrevMonth%%PrevDay%"
set "PrevDay="
set "PrevMonth="
set "PrevYear="
echo/
dir %PrevDate%.15.nc %PrevDate%.18.nc %PrevDate%.21.nc %FileDate%.00.nc %FileDate%.03.nc %FileDate%.06.nc %FileDate%.09.nc %FileDate%.12.nc
set "FileDate="
set "PrevDate="
echo/
pause
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
dir /?
echo /?
endlocal /?
for /?
goto /?
if /?
pause /?
rem /?
set /?
setlocal /?
Thanks in advanced for your help! I have a batch file question for you guys. So we keep up with one of our client's backups. They're located over 200 miles away from us. We bring their backups over via remote desktop. We've found that using the copy function via the command prompt is MUCH faster than any other method of copy/paste. The person who is in charge of bringing them over often forgets to do so. I've only just started using batches, but I created a small batch program that will ask the user which file to bring over so he doesn't have to "copy n:\backups\blah \tsclient\h\backups\blah" which can be quite error prone. The following is the batch file:
#echo off
title Copy Zipbacks
:loopagain
set /p date=Enter the date that needs to be copied over (yyyymmdd format):
copy h:\zipbackups\daily%date%.zipx \\tsclient\h\benton_off_site_backup\zipbackups
set /p again=Copy Another Daily Zip file? (Y/N):
IF "%again%"=="Y" GOTO loopAgain
IF "%again%"=="N" GOTO goAway
:goAway
exit
This is good if there are only a handful of backups to be brought over. My question is this; Is there a way to bring over a range of backups? The file is setup as follows:
dailyYYYYMMDD.zipx
i.e. daily20140917.zipx
I have no problem asking for the date range, but getting the .bat to loop through the folder and getting only those that meet the criteria is where I'm running into the issue. Any thoughts?
You may use a for to enumerate files:
#echo off
title Copy Zipbacks
:loopagain
set /p start_date=Enter start date that needs to be copied over (yyyymmdd format):
set /p end_date=Enter end date (yyyymmdd format) or nothing to match only start date:
if not defined end_date set end_date=%start_date%
for %%f in (h:\zipbackups\daily*.zipx) do if "%%~nf" geq "daily%start_date%" if "%%~nf" leq "daily%end_date%" copy %%f \\tsclient\h\benton_off_site_backup\zipbackups
set /p again=Copy Another Daily Zip file? (Y/N):
IF "%again%"=="Y" GOTO loopAgain
IF "%again%"=="N" GOTO goAway
:goAway
exit
I came across a trick with xcopy that lets you generate a sequence of valid dates. The following script will generate the sequence and then copy each of the files.
#echo off
::set /p date=Enter the start date that needs to be copied over (yyyymmdd format):
::set /p end=Enter the end date that needs to be copied over (yyyymmdd format):
set /a date=20010218
set /a end=20010302
set /a y=%date:~0,4%
set /a m=%date:~4,2%
set /a d=%date:~6,2%
echo INPUT = %y% %m% %d%
:getnextvaliddate
set /a d+=1
if %d% gtr 31 (
set d=1
set /a m+=1
if %m% gtr 12 (
set m=1
set /a y+=1
)
)
echo %y% %m% %d%
xcopy /d:%m%-%d%-%y% /h /l "%~f0" "%~f0\" >nul 2>&1 || goto getnextvaliddate
call :prettydate
if %y% equ %end:~0,4% (
if %m% equ %end:~4,2% (
if %d% equ %end:~6,2% (
goto :EOF
)
)
)
goto :getnextvaliddate
:prettydate
if %d% lss 10 (
set dd=0%d%
) else (
set dd=%d%
)
if %m% lss 10 (
set mm=0%m%
) else (
set mm=%m%
)
echo copy h:\zipbackups\daily%y%%mm%%dd%.zipx \\tsclient\h\benton_off_site_backup\zipbackups
goto :EOF
Output on my machine:
INPUT = 2001 2 26
2001 2 27
copy h:\zipbackups\daily20010227.zipx \\tsclient\h\benton_off_site_backup\zipbackups
2001 2 28
copy h:\zipbackups\daily20010228.zipx \\tsclient\h\benton_off_site_backup\zipbackups
2001 2 29
2001 2 30
2001 2 31
2001 3 1
copy h:\zipbackups\daily20010301.zipx \\tsclient\h\benton_off_site_backup\zipbackups
2001 3 2
copy h:\zipbackups\daily20010302.zipx \\tsclient\h\benton_off_site_backup\zipbackups
I am struggling to write a batch script which can read a CSV file such as below
Name:, City:, Country:
Mark, London, UK
Ben, Paris, France
Tom, Athens, Greece
There will be a heading row in the CSV file. It should output to a text file as below:
Name:Mark
City:London
Country:UK
Name:Ben
City:Paris
Country:France
Name:Tom
City:Athens
Country:Greece
The field separator (:) in the above output is expected to be provided in the header row itself. So all that I need to do is concatenate the field heading and its value.
The number of columns in this CSV file is not fixed, so the script should not limit to 3 tokens. Kindly help!
#ECHO OFF
IF "%~1"=="" GOTO :EOF
SET "filename=%~1"
SET fcount=0
SET linenum=0
FOR /F "usebackq tokens=1-10 delims=," %%a IN ("%filename%") DO ^
CALL :process "%%a" "%%b" "%%c" "%%d" "%%e" "%%f" "%%g" "%%h" "%%i" "%%j"
GOTO :EOF
:trim
SET "tmp=%~1"
:trimlead
IF NOT "%tmp:~0,1%"==" " GOTO :EOF
SET "tmp=%tmp:~1%"
GOTO trimlead
:process
SET /A linenum+=1
IF "%linenum%"=="1" GOTO picknames
SET ind=0
:display
IF "%fcount%"=="%ind%" (ECHO.&GOTO :EOF)
SET /A ind+=1
CALL :trim %1
SETLOCAL ENABLEDELAYEDEXPANSION
ECHO !f%ind%!!tmp!
ENDLOCAL
SHIFT
GOTO display
:picknames
IF %1=="" GOTO :EOF
CALL :trim %1
SET /a fcount+=1
SET "f%fcount%=%tmp%"
SHIFT
GOTO picknames
This batch scipt:
accepts one parameter, the name of the file to process;
does not verify the presence of : at the end of a header token, and when the values are displayed they are placed immediately after the corresponding header tokens;
trims all the leading spaces (but not the trailing ones);
considers the first row to be the header row, which also defines the number of tokens to process in subsequent rows;
supports up to 10 tokens, and the two areas highlighted in bold italics are responsible for that (so when you need to change the maximum number, modify both areas: if you increase the number, you must expand the "%%a" "%%b" "%%c" … list, and, likewise, if you decrease the number, then shrink the list).
I know this is an old question, but this type of question is my favorite one so here it is my answer:
#echo off
setlocal EnableDelayedExpansion
rem Create heading array:
set /P headingRow=< %1
set i=0
for %%h in (%headingRow%) do (
set /A i+=1
set heading[!i!]=%%~h
)
rem Process the file:
call :ProcessFile < %1
exit /B
:ProcessFile
set /P line=
:nextLine
set line=:EOF
set /P line=
if "!line!" == ":EOF" goto :EOF
set i=0
for %%e in (%line%) do (
set /A i+=1
for %%i in (!i!) do echo !heading[%%i]!%%~e
)
goto nextLine
exit /B
This program have not any limit in the number of fields. This version requires to enclose in quotes the elements that may have spaces or other Batch delimiters, but this restriction may be easily fixed.
Python makes this so easy it should be regulated by the government.
from csv import DictReader
with open('file', 'rb') as file:
reader = DictReader(file)
for line in reader:
for field in reader.fieldnames:
print '{0}{1}'.format(field.strip(), line[field].strip())
print '\n'
Edit: I guess you need something native to the Windows command shell. Oh well.
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