How to handle a comma in a filename with WMIC?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

How to handle a comma in a filename with WMIC?

#1 Post by foxidrive » 21 Jan 2014 08:16

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

AiroNG
Posts: 46
Joined: 17 Nov 2013 15:00
Location: Germany

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

#2 Post by AiroNG » 21 Jan 2014 10:58

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".

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

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

#3 Post by penpen » 21 Jan 2014 11:53

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.
Last edited by penpen on 22 Jan 2014 18:21, edited 1 time in total.

Sponge Belly
Posts: 231
Joined: 01 Oct 2012 13:32
Location: Ireland
Contact:

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

#4 Post by Sponge Belly » 21 Jan 2014 14:01

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

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

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

#5 Post by Ed Dyreen » 21 Jan 2014 15:11

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:

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

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

#6 Post by Endoro » 21 Jan 2014 16:31

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?

Sponge Belly
Posts: 231
Joined: 01 Oct 2012 13:32
Location: Ireland
Contact:

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

#7 Post by Sponge Belly » 21 Jan 2014 17:23

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
Last edited by Sponge Belly on 22 Jan 2014 06:29, edited 1 time in total.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

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

#8 Post by aGerman » 21 Jan 2014 17:33

The key is to use double quotes instead of single quotes around the file path.

Regards
aGerman

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

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

#9 Post by foxidrive » 21 Jan 2014 22:34

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.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

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

#10 Post by penpen » 22 Jan 2014 18:26

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

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

#11 Post by foxidrive » 22 Jan 2014 23:11

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

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

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

#12 Post by penpen » 23 Jan 2014 03:59

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

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

#13 Post by foxidrive » 23 Jan 2014 07:34

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

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

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

#14 Post by penpen » 23 Jan 2014 07:36

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

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

#15 Post by foxidrive » 23 Jan 2014 08:55

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.

Post Reply