1.

Solve : check if a "." is in a variable?

Answer»

I know a similar question was ask but I did not understand the code nor did it work when I tried to modified it.

I would like my batch file to check if a period is in a variable. If "yes" then it does this otherwise it does something else. THis is all I have so far. This does NOT work. I'm using windows XP.

SET /p str=Enter a string:
for . IN %str% do echo got it
pause

Any comment or suggestion are always welcome.

thanks
C:\>found
88800004
88800004
88800004
88800004
88800004
88800004
8880000.4
[7]8880000.4
0
Found period
88800004
88800004
88800004
88800004
88800004
88800004
1
no period
BYE
C:\>type found.bat
@ECHO OFF

type period.txt
type period.txt | find /N "."
echo %ERRORLEVEL%

if %ERRORLEVEL%==1 GOTO NOPERIOD

if %ERRORLEVEL%==0 GOTO Found

:Found
echo Found period
type noperiod.txt
type noperiod.txt | find /N "."
echo %ERRORLEVEL%
if %ERRORLEVEL%==1 GOTO NOPERIOD

if %ERRORLEVEL%==0 GOTO Found

Goto END

:NOPERIOD
echo no period

:END
echo BYE

C:\>




http://www.dostips.com/DtTipsStringManipulation.php#Snippets_LeftString


<<"Mid String - Extract a Substring by Position
Description: Similar to the Mid function in VB a batch script can return a specified number of characters from any position inside a string by specifying a substring for an expansion given a position and length using :~ while expanding a variable CONTENT. The example here shows how to extract the parts of a date. ">> see below ***

Rem run period.bat
C:\>period
20:18:18.78
20:18:18.78
extract "." from string. Find "." and replace with nothing "."=
20:18:1878

C:\>type period.bat
@echo OFF
echo %TIME%
set str=%TIME%
echo.%str%
echo extract "." from string Find "." and replace with nothing "."=%.
set str=%str:.=%
echo.%str%


Script
echo.Date : %date%
echo.Weekday: %date:~0,3%
echo.Month : %date:~4,2%
echo.Day : %date:~7,2%
echo.Year : %date:~10,4%

***Script Output: Script Ouput
Date : Sat 03/11/2006
Weekday: Sat
Month : 03
Day : 11
Year : 2006



Code: [Select]@echo off
set/p s=enter string:

echo -=EXAMPLE WITH FOR LOOP=-
for %%a in (%s%) do if not "%%~xa"=="" (echo dot exist) else echo dot not exists

echo -=EXAMPLE WITH FIND=-
echo %s%|find "." >nul 2>&1 && (echo dot exist)||echo dot not exist
Thank you RENO. It works perfectly...Quote from: Reno on March 06, 2009, 10:14:20 PM

for %%a in (%s%) do if not "%%~xa"=="" (echo dot exist) else echo dot not exists

I like that lateral thinking, Reno, where you consider the string as a "filename" and see if it has an "extension".
Dias de verano wrote:

" . . .the string as a "filename" and see if it has an "extension"

Don't all file names on XP have an ".ext" .txt , .wpd . doc , .pps . . .?Quote from: billrich on March 07, 2009, 12:49:18 PM
Don't all file names on XP have an ".ext" .txt , .wpd . doc , .pps . . .?

An extension is not mandatory in MS-DOS or any VERSION of Windows. Extensions are used by Windows to implement file "types", but it is perfectly possible to have filenames without an extension. The Hosts file is a WELL known example. Others include ntldr and cmldr.
Quote from: Reno on March 06, 2009, 10:14:20 PM
Code: [Select]for %%a in (%s%) do if not "%%~xa"=="" (echo dot exist) else echo dot not exists

The code works great but I do not understand the code for the for loop one. Can you or anyone explain please.

Where in the code does it say that you check for the "." Also, what does "%%~xa"=="" mean?

thanksQuote from: locbtran on March 07, 2009, 06:15:59 PM
Quote from: Reno on March 06, 2009, 10:14:20 PM
Code: [Select]for %%a in (%s%) do if not "%%~xa"=="" (echo dot exist) else echo dot not exists

The code works great but I do not understand the code for the for loop one. Can you or anyone explain please.

Where in the code does it say that you check for the "." Also, what does "%%~xa"=="" mean?

thanks

I hope I am not pre-empting Reno by explaining his or her excellent ingenious [see below*] piece of scripting.

1. By typing FOR /? at the prompt you will see the help text which includes information about the "variable modifiers".

2. These consist of a tilde character ( ~ ) followed by one or more letters.

3. They allow the extraction of information from a string of text considered to be a filename.

4. Examples: If %%A [in a batch] is a filename, [or %A at the prompt] then %%~dA returns the drive letter and colon (e.g. "C:"), %%~pA returns the path, %%~nA returns the name, and %%~xA returns the extension if there is one, and an empty string if there is not. An extension is defined as a dot and at least one following character.*

5. The filename does not have to refer to any existing file.

6. Thus:

Code: [Select]C:\>for /f "delims==" %A in ("hello.kitty") do @echo Result: "%~xA"
Result: ".kitty"

C:\>for /f "delims==" %A in ("hello.kitty.today") do @echo Result: "%~xA"
Result: ".today"

C:\>for /f "delims==" %A in ("hello.k") do @echo Result: "%~xA"
Result: ".k"

C:\>for /f "delims==" %A in (".k") do @echo Result: "%~xA"
Result: ".k"
BUT...

Code: [Select]C:\>for /f "delims==" %A in ("hello.") do @echo Result: "%~xA"
Result: ""

C:\>for /f "delims==" %A in (".") do @echo Result: "%~xA"
Result: ""

C:\>for /f "delims==" %A in ("......") do @echo Result: "%~xA"
Result: ""
* We see by experiment that the method will not INFALLIBLY detect the presence of a dot in a string, since if the string contains a dot only in the last position, or if all characters are dots, then a false report is given.

So to answer your questions

Quote
Where in the code does it say that you check for the "."

It doesn't, as explained above, explicitly search for a dot. It attempts to exploit a feature of cmd.exe intended for another purpose. And will succeed in some, but not all cases. Thus I cannot recommend it.

Quote
Also, what does "%%~xa"=="" mean?

It is part of an IF expression...

Code: [Select]if not "%%~xa"==""

... which tests the following:

is the string formed by expanding "%%~xa" NOT equal to two quote marks? (i.e. is it NOT empty?)


If the user enters the string, the user already knows the string has a "." The source of the string could be the file names on the
computer. Do not list all the file names but instead provide a count of the file names. Also provide a count of the filenames without a "." Reno suggested the source of the string be filenames on the computer
dir /s * | find /c "."
30353

dir /s * | find /v /c "."
13003Quote from: billrich on March 08, 2009, 06:18:28 AM

C:\>dir /s | find "." | wc -l


wc is not a standard Windows command, it is a Unix utility that needs to be downloaded. You should have made that clear.

Also, it doesn't answer the OP's question.



dias, that's too much, its just an ordinary script, and worse its buggy code.
you have done a good job at explaining. if i had to do the explaining part, i am going to reply type for /? for more info

i also just realized it wont work when the string contain space, so here is the fix:
Code: [Select]for %%a in ("%s%-") do if not "%%~xa"=="" (echo dot exist) else echo dot not exist
still the above code can't handle wildcard characters such as "*, ?", but the find example can with some fix.
Code: [Select]echo "%s%"|find "." >nul 2>&1 && (echo dot exist)||echo dot not exist
Quote from: Reno on March 08, 2009, 06:38:42 AM
dias, that's too much, its just an ordinary script, and worse its buggy code.

Scripts, whether ordinary or special, should be as error free as we can make them. My turn to roll eyes



ahh....

sometimes I can bask in the glory of having Instr and InstrRev at my disposal...dir /s | find "." | wc -l
dir /s * | find /c "."
Number of strings with "." = 30362
dir /s * | find /v /c "."
Number of strings without "." = 13003


Dias de verano wrote:
"Wc is not a standard Windows command, it is a Unix utility that needs to be downloaded. You should have made that clear.
Also, it doesn't answer the OP's question."

The Original Poster ask us to find a "." in a string. All the file names on the computer are a string. ( When the user enters the string, the user already knows the string has a ".") We answered the question very well. find /c can find the count. wc -l is not needed.


Discussion

No Comment Found