1.

Solve : batch file question?

Answer»

I have a batch file question.

is it possible to COUNT the number of characters in a string? I am wanting to set a minimum number of characters in a string.

thank you,
wayne brost


Quote from: wbrost on April 12, 2008, 11:07:38 PM

I have a batch file question.

is it possible to count the number of characters in a string? I am wanting to set a minimum number of characters in a string.

thank you,
wayne brost




yes. depends on whether you need a pure batch solution or not.
in vbscript
Code: [Select]Set arg = WScript.Arguments
StrString = arg(0)
WScript.Echo Len(StrString)
save the code as script.vbs and on command LINE, type
Code: [Select]C:\test>cscript /nologo script.vbs "test"
4
you can use a for loop in batch to catch the result. Although you can do everything in vbscript.

you can also use *nix tools like wc for windows

example:
Code: [Select]C:\test>echo "test"|wc -m
8
the length is shown as "8" because wc treats "\r\n" as 2 chars.

for just batch solution, you can certainly google for it on the web.
If you have write access, you can echo the string to a text file, then get the file size, subtracting 2 bytes for the CR+LF at the end

All in one line!

Code: [Select]echo %string%>%temp%\sizeme.txt&for %%F in ("%temp%\sizeme.txt") do set /a stringlen=%%~zF-2&del "%temp%\sizeme.txt"thank you both. I now have the batch file working with the advice you gave. but, now I have ran into a new problem with the batch file.

:problem
have you ever tried to add numbers that STARTED with a leading 0?

example: if you type the following you get some odd results.

set test=01234
set /a add=%test%+50
echo %add%

it works but give you an incorrect answer. Have you seen this before?


:question
is it possable (with out lots of code) to figure out if a string contains numbers or letters?

thanks,
WayneQuote from: wbrost on April 15, 2008, 02:03:18 PM
have you ever tried to add numbers that started with a leading 0?

example: if you type the following you get some odd results.

set test=01234
set /a add=%test%+50
echo %add%

it works but give you an incorrect answer. Have you seen this before?

Yes.

The set /a command reads numeric values as decimal numbers, unless prefixed by 0x for hexadecimal numbers, and 0 for octal numbers. So 0x12 is the same as 18 is the same as 022. The octal notation can be confusing: 08 and 09 are not valid numbers because 8 and 9 are not valid octal digits.

Type set /a at the prompt for details of usage.

Quote
is it possible (with out lots of code) to figure out if a string contains numbers or letters?

I corrected your spelling of "possible". I do not know what your definition of "lots of code" is. I don't think 2 lines is lots of code.

You can pipe the string to FINDSTR using the /R switch and a regex (regular expression) and check the errorlevel. When an item is not found FINDSTR will return an errorlevel >0

Code: [Select]Echo 12G6 | FindStr /R "[0-9]"
If %ERRORLEVEL% EQU 0 echo The string contains one or more numeric characters

Echo 12G6 | FindStr /R "[^0-9]"
If %ERRORLEVEL% EQU 0 echo The string contains one or more non numeric characters







its easier in vbscript
Code: [Select]s="123"
If IsNumeric(s) Then
WScript.Echo "Numeric"
End If


Discussion

No Comment Found