Solved Test if a variable is a string (Batch)

September 9, 2011 at 10:47:56
Specs: Windows 7 Home edition
Hi! again. I've decided to post a separate question re. my code.

Anyone knows how can I test the content of a file, if it's a string or a number?

The code is

FOR /F "eol= tokens=* delims= usebackq" %%A in (%file%) do (
if "%%A" NEQ "0" (
echo "this is a string"
)else (
echo "this is a number"
)

I want this part if "%%A" NEQ "0" to test if it's a string or a number.

Also, is there any way that I can include the value of %%A in an email body?

Thanks so much again.


See More: Test if a variable is a string (Batch)

Report •


✔ Best Answer
September 14, 2011 at 19:42:30
Sure, but if you find the word or phrase (string) then there is no need to test further for it.

I set up a little demo test to locate the line(s) with Magick in them in a trial file. The trial file is named Trial.txt and the content is:

Magick: image widths or heights differ `flash_home_half.png' @ error/c
Magic: image widths or heights differ `flash_home_half.png' @ error/c
Magick: image widths or heights differ `flash_home_half.png' @ error/c

Note that the second line does not contain Magick only Magic so should not be found. Because I didn't know what you wanted to do after the string was found I simply wrote the lines to a temp file and displayed them removing Magick.

@echo off>%temp%\file.txt
cls
setlocal enabledelayedexpansion
set file=trial.txt

    findstr "Magick" %file%>>%temp%\file.txt

    for /f "tokens=*" %%1 in (%temp%\file.txt) do (
        set line=%%1
        set line=!line:Magick=!
        echo !line!
        )

Output is:

: image widths or heights differ `flash_home_half.png' @ error/c
: image widths or heights differ `flash_home_half.png' @ error/c

You could review the Find and Findstr commands. Hope this helps.


Please come back & tell us if your problem is resolved.



#1
September 9, 2011 at 20:19:27
Perhaps you might want to show what the content of %file% can be? I get the impression from your post that %file% contains just one character or digit and that you want to determine whether that character or digit is numeric or alpha?

Sorry I'm so dumb.


Please come back & tell us if your problem is resolved.


Report •

#2
September 12, 2011 at 06:10:44
Hi! Wahine. Actually, there are several possible output on %file% since it's an error log file. Anyways, one of the output i could get in %file% is

Magick: image widths or heights differ `flash_home_half.png' @ error/compare.c/CompareImageCommand/957.

So i guest, it is safe to say that it will be a combination of alphanumeric characters with some punctuations. Can you tell? Thanks!


Report •

#3
September 12, 2011 at 16:06:03
Well, you got me there. The way your script is written is a bit puzzling, using Tokens=* means that %%A will always contain the entire line therefore you are comparing 0 (zero) with the entire line, the return will always be True.

Try using Tokens=1* but note that if the first delimited field is anything other than just 0 (zero) the script will always give the same return regardless of whether or not the field is totally numeric.

So if the start of the file is:
0 Magick: .... the return will be False and "this is a number" will be echoed.
01 Magick: ... the return will be True and "this is a string" will be echoed.
0Magick: ..... the return will be True and "this is a string" will be echoed.

Hope this helps you.


Please come back & tell us if your problem is resolved.


Report •

Related Solutions

#4
September 13, 2011 at 20:49:23
Hi! Wahine. Thanks for your help. I think I can take it from here but just one more favor to ask. Can you tell if it's possible that I could find just a few words like "error" "magick" in the phrase then do a test if the words are there? I'm struggling about the right and working syntax actually.

Any help again would be very much appreciated.


Report •

#5
September 14, 2011 at 19:42:30
✔ Best Answer
Sure, but if you find the word or phrase (string) then there is no need to test further for it.

I set up a little demo test to locate the line(s) with Magick in them in a trial file. The trial file is named Trial.txt and the content is:

Magick: image widths or heights differ `flash_home_half.png' @ error/c
Magic: image widths or heights differ `flash_home_half.png' @ error/c
Magick: image widths or heights differ `flash_home_half.png' @ error/c

Note that the second line does not contain Magick only Magic so should not be found. Because I didn't know what you wanted to do after the string was found I simply wrote the lines to a temp file and displayed them removing Magick.

@echo off>%temp%\file.txt
cls
setlocal enabledelayedexpansion
set file=trial.txt

    findstr "Magick" %file%>>%temp%\file.txt

    for /f "tokens=*" %%1 in (%temp%\file.txt) do (
        set line=%%1
        set line=!line:Magick=!
        echo !line!
        )

Output is:

: image widths or heights differ `flash_home_half.png' @ error/c
: image widths or heights differ `flash_home_half.png' @ error/c

You could review the Find and Findstr commands. Hope this helps.


Please come back & tell us if your problem is resolved.


Report •

#6
September 20, 2011 at 09:38:48
thanks a lot!

Report •


Ask Question