| 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. 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 PMCode: [Select]for %%a in (%s%) do if not "%%~xa"=="" (echo dot exist) else echo dot not exists 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
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. |
|