Page 1 of 3

How to handle a comma in a filename with WMIC?

Posted: 21 Jan 2014 08:16
by foxidrive
Has anyone solved the problem of using a comma in a filename and processing it with WMIC?

I get an error ", - Invalid alias verb."

Code: Select all

@echo off
set "file=a filename with, a comma.txt"
echo text>"%file%"
WMIC DATAFILE WHERE name="%file%" get lastmodified | find "."
pause

Re: How to handle a comma in a filename with WMIC?

Posted: 21 Jan 2014 10:58
by AiroNG
I may have a solution:
WMIC DATAFILE WHERE (name="%file%") get lastmodified | find "."

however, if i run your script with the parenthesis i get another error:
Keine Instanzen verfügbar.

it should translate to something like "No instances available".

Re: How to handle a comma in a filename with WMIC?

Posted: 21 Jan 2014 11:53
by penpen
In WinXP prof it is no matter of a comma, but a matter of full path with escaped '\', so try:

Code: Select all

@echo off
set "file=%~dp0a filename with, a comma.txt"
echo text>"%file%"
WMIC DATAFILE WHERE name="%file:\=\\%" get lastmodified | find "."
pause

penpen

Edit: My xp was not at actual patch level: Now this won't work anymore.

Re: How to handle a comma in a filename with WMIC?

Posted: 21 Jan 2014 14:01
by Sponge Belly
Hello All! :-)

I combined AiroNG’s parentheses and Penpen’s backslash-escape techniques and ended up with something like this:

Code: Select all

X:\> set "fn=%cd%\tom, dick & harry.txt"
X:\> type nul >"%fn%"
X:\> wmic datafile where (name="%fn:\=\\%") get lastmodified
LastModified
20140121191739.539644+000


I haven’t tested this against all poison characters, but it looks promising.

- SB

Re: How to handle a comma in a filename with WMIC?

Posted: 21 Jan 2014 15:11
by Ed Dyreen
AiroNG wrote:I may have a solution:
WMIC DATAFILE WHERE (name="%file%") get lastmodified | find "."

however, if i run your script with the parenthesis i get another error:
Keine Instanzen verfügbar.

it should translate to something like "No instances available".
does the file exist ?

Code: Select all

@echo off &setlocal disableDelayedExpansion
ver
set "f=c:\\___.txt"
echo.>"%f%"
WMIC DATAFILE WHERE ( name='%f%' AND Archive='TRUE' ) get lastmodified | find "."
set "f=c:\\_,_.txt"
echo.>"%f%"
WMIC DATAFILE WHERE ( name='%f%' AND Archive='TRUE' ) get lastmodified | find "."

pause
it works for me..

Code: Select all

Microsoft Windows XP [versie 5.1.2600]
20140121213757.484375+060
20140121213757.484375+060
Druk op een toets om door te gaan. . .
if it were standard SQL it would work always :roll:

Re: How to handle a comma in a filename with WMIC?

Posted: 21 Jan 2014 16:31
by Endoro
you may have an error:

Code: Select all

C:\Users\User\TEST>set "f=c:\\_,_.txt"

C:\Users\User\TEST>for /f %a in ('WMIC DATAFILE WHERE ( name^='%f%' AND Archive^='TRUE' ^) get lastmodified ^| find "."') do @echo %a
No Instance(s) Available.



you may have a comma:

Code: Select all

C:\Users\User\TEST>set "f=c:\\_,_.txt"

C:\Users\User\TEST>for /f %a in ('"WMIC DATAFILE WHERE name='%f%' get lastmodified | find ".""') do @echo %a
, - Invalid alias verb.



or not: :roll:

Code: Select all

C:\Users\User\TEST>set "f=c:\\_,_.txt"

C:\Users\User\TEST>for /f %a in ('^"WMIC DATAFILE WHERE name='%f%' get lastmodified ^| find "."^"') do @echo %a
c:\\_ _.txt - Invalid alias verb.


Who knows the quoting for the loop?

Re: How to handle a comma in a filename with WMIC?

Posted: 21 Jan 2014 17:23
by Sponge Belly
Hi Endoro! :-)

Great question. Turns out the equals sign after name must be caret-escaped:

Code: Select all

X:\> set "fn=%cd%\tom, dick & harry.txt"
X:\> type nul >"%fn%"
X:\> for /f "skip=1" %f in ('
More? wmic datafile where (name^="%fn:\=\\%"^) get lastmodified
More? ') do @echo(%f
20140121224248.567011+000


It took me quite a while to track this one down. It’s always the little things that trip you up. ;-)

- SB

Re: How to handle a comma in a filename with WMIC?

Posted: 21 Jan 2014 17:33
by aGerman
The key is to use double quotes instead of single quotes around the file path.

Regards
aGerman

Re: How to handle a comma in a filename with WMIC?

Posted: 21 Jan 2014 22:34
by foxidrive
I did google before posting and what I found was that when using parentheses, it fixes the comma, but then the closing bracket becomes a poison character in a filename.

I didn't find any solution that fixes the comma without introducing the closing bracket problem. I thought maybe there was some technique that google wasn't showing me.

Re: How to handle a comma in a filename with WMIC?

Posted: 22 Jan 2014 18:26
by penpen
The 8.3 filename format seems to disallows the use of a comma:

Code: Select all

 Datenträger in Laufwerk Z: ist Test
 Volumeseriennummer: 0123-4567

 Verzeichnis von Z:\

23.01.2014  01:21                 0 A_B~1.TXT    a, b.txt
23.01.2014  01:21                 0 A_B~2.TXT    a,b.txt
               2 Datei(en)              0 Bytes
               0 Verzeichnis(se), 162.422.374.400 Bytes frei
So if short names are available this script may do what you want:

Code: Select all

@echo off
setlocal
set "filename=a filename with, a comma.txt"
set "file=%~dp0%filename%"

for /F "tokens=1-3*" %%a in ('dir /X "%filename%" ^| findstr /R /C:"%filename%"') do set "shortname=%%d"
call set "shortname=%%shortname:%filename%=%%|"
for /L %%a in (1,1,11) do set "shortname=%shortname: |=|%"
set "shortname=%shortname:|=%"

set "sfile=%~dp0%shortname%"
WMIC DATAFILE WHERE name="!sfile:\=\\!" GET lastmodified | find "."

pause
endlocal

penpen

Re: How to handle a comma in a filename with WMIC?

Posted: 22 Jan 2014 23:11
by foxidrive
Your idea works penpen (the sfile needs to be using %var% syntax), though it will fail where the time indicator has am/pm, without some extra logic - true?

I wonder if the following will work in previous windows versions also, or if the DIR layout is different to Win 8.x?

The thought occurs to me that the folder path could contain a comma too, and getting the short path and filename reliably is a real chore.

Code: Select all

@echo off
setlocal
set "filename=a filename with, a comma.txt"
echo abc>"%filename%"

for /F "delims=" %%a in ('dir /X "%filename%" ^| findstr /I /R /C:"%filename%"') do set "line=%%a"
set "shortname=%line:~36,12%"
set "shortname=%shortname: =%"
if defined shortname (set "filename=%~dp0%shortname%") else (set "filename=%~dp0%filename%")

WMIC DATAFILE WHERE name="%filename:\=\\%" GET lastmodified | find "."

pause
endlocal

Re: How to handle a comma in a filename with WMIC?

Posted: 23 Jan 2014 03:59
by penpen
This "=%line:~36,12%" part seems to on all systems with default short date format (TT.MM.JJJJ), but if other date formats are in use (for example "T.M.JJ" or "TTTT, TT.MM.JJJJ") it may slides to the left or right.

Above i didn't know how the 8.3 format is used, and if spaces and commas are really disallowed, but luckily i've found:
https://support.microsoft.com/kb/142982/EN-US

Also i have reread the for help, and found something very usefull :D .
So this shortest, that should (hopefully) work everywhere is:

Code: Select all

@echo off
setlocal
set "filename=a filename with, a comma.txt"

if not exist "%filename%" echo test>"%filename%"

for %%a in ("%filename%") do set "sfile=%%~sdpnxa"
WMIC DATAFILE WHERE name="%sfile:\=\\%" GET lastmodified | find "."

pause
endlocal

penpen

Re: How to handle a comma in a filename with WMIC?

Posted: 23 Jan 2014 07:34
by foxidrive
penpen wrote:This "=%line:~36,12%" part seems to on all systems with default short date format (TT.MM.JJJJ), but if other date formats are in use (for example "T.M.JJ" or "TTTT, TT.MM.JJJJ") it may slides to the left or right.


Thanks, I guess that is another damper on my method.

for %%a in ("%filename%") do set "sfile=%%~sdpnxa"


This would be perfect, but for many many years the short path\filename feature of %%~s has a bug where in certain cases the path\filename is corrupted as a segment of the string is added to the end.

EDIT: see this thread for some further discussion, but related to short filename bug using %%~s viewtopic.php?f=3&t=5310

Re: How to handle a comma in a filename with WMIC?

Posted: 23 Jan 2014 07:36
by penpen
I've found the bug: http://stackoverflow.com/questions/8354305/batch-parameter-s1-gives-incorrect-8-3-short-name

I've finished a workaround, not short, but it seems to be reliable now:

Code: Select all

@echo off
cls
setlocal
set "filename=a filename with, a comma [1].txt"
if not exist "%filename%" echo test>"%filename%"


for %%a in ("%filename%\") do set "buffer=%%~dpnxa"
set "N=-1"
for %%a in ("%buffer:\=" "%") do set /A "N+=1"
for %%a in ("%filename%\") do set "buffer=%%~sdpa"
set "M=0"
set "sfile="
setlocal enabledelayedExpansion
for %%a in ("%buffer:\=" "%") do (
   set /A "M+=1"
   if !M! LEQ !N! set sfile=!sfile! "%%~a"
)
endlocal & set sfile=%sfile%
set sfile=%sfile:" "=\%
set "sfile=%sfile:"=%"
set "sfile=%sfile: =%"
echo %sfile%


echo WMIC DATAFILE WHERE name="%sfile:\=\\%" GET lastmodified | find "."

pause
endlocal

penpen

Re: How to handle a comma in a filename with WMIC?

Posted: 23 Jan 2014 08:55
by foxidrive
I can confirm that this worked in XP Pro in a VM.

I also tried the test case I listed in viewtopic.php?f=3&t=5310 using a Vista VM and the short filename bug looks like it was fixed in Vista.