Solved BATCH file to validate variable

November 1, 2012 at 04:09:12
Specs: DOS
set dr_narr=TRANSFER12345678901234612

:CHAR1
For %%a in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 1 2 3 4 5 6 7 8 9 0 .) do (if %%a EQU %dr_narr:~0,1% GOTO CHAR2)
CALL Journal_creator_validation_failed.bat

:CHAR2
For %%a in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 1 2 3 4 5 6 7 8 9 0 .) do (if %%a EQU %dr_narr:~1,1% GOTO PASSED)
CALL Journal_creator_validation_failed.bat

The above works, bt if I add a space tothe variable it fails, I have tried adding [] to the list A-Z 0-9 but still won't validate a space? Any way around this?

Thanks.

PS (the reason for this is to endure that the variable only contains capital letters and numbers and a decimal place).


See More: BATCH file to validate variable

Report •

#1
November 1, 2012 at 06:41:00
Use " for the ones containing spaces and when using the for command in brackets use ' at each end in addition to the variables if they contain any space(s),

Report •

#2
November 1, 2012 at 06:51:54
I understand that variables containing spaces need '. I am trying to validate that the variable only contains capital letters A-Z, 0-9 a full stop or a space. As you can see from the FOR command all A-Z 0-9 etc are included, what do I include to allow the space to pass validation.

Thanks


Report •

#3
November 1, 2012 at 13:00:02
✔ Best Answer
You'll have to put quotes around each element in the validation set, but then you can add the space. Like:
for %%a in ("A","B"," ","0") do if %%a equ "%dr_narr:~0,1%" goto CHAR)

Note that you do NOT use quotes around %%a because they're already included in it.

Here's another method that seemed to work:
::==== begin script
@echo off & setlocal enabledelayedexpansion
set alpha=abcdefghijklmnopqrstuvwxyz 0123456789
set test=testing valid 09
for /L %%a in (0,1,100) do (
set t=!test:~%%a,1!
:: end of test-string reached, it is "good"
if "!t!" equ "" goto :okproceed
call :xx
if !t! equ fail echo failed at char# %%a&goto :eof
)
:: string longer than arbitrary "100"
goto :toolong

:xx
if "%alpha%" equ "!alpha:%t!%=*!" echo fail: %t%&set t=fail
goto :eof

:okproceed
::note, the entire string has been validated
echo ok, proceeding with batch
::======= end sample snippet batch

note that you do have to use enabledelayedexpansion and a called sub, but it is shorter than the other way.

ps: edited to fix my transcription mistakes.


Report •

Related Solutions

#4
November 2, 2012 at 02:23:46
nbrane.... wow thanks alot, works with ".....


Report •

#5
November 2, 2012 at 19:29:31
(sorry, i did mangle the crap out of this line in transition:)
if "%alpha%" equ "!alpha:%t%=*% echo fail: %t%&set t=fail

should be:
if "%alpha%" equ "!alpha:%t%=*!" echo fail: %t%&set t=fail


Report •

Ask Question