How to handle a comma in a filename with WMIC?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

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

#16 Post by Endoro » 24 Jan 2014 00:48

using an elevated cmd window, you can make your own short file name:

Code: Select all

fsutil file setshortname "%cd%\test.tmp" blah

This also works for folders.

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

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

#17 Post by foxidrive » 24 Jan 2014 01:23

That's interesting Endoro.

As the short filename bug seems fixed in Vista and later then %%~sfa seems to be the easiest solution - XP users need to upgrade in April anyway. ;)

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

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

#18 Post by Endoro » 24 Jan 2014 01:46

Btw. %~sI and %~fsI give the same result, but M$ doesn't know it:

Code: Select all

The modifiers can be combined to get compound results:

    %~dpI       - expands %I to a drive letter and path only
    %~nxI       - expands %I to a file name and extension only
    %~fsI       - expands %I to a full path name with short names only
    %~dp$PATH:I - searches the directories listed in the PATH
                   environment variable for %I and expands to the
                   drive letter and path of the first one found.
    %~ftzaI     - expands %I to a DIR like output line


Part of the 'for' help text (Win8).

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

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

#19 Post by foxidrive » 24 Jan 2014 03:11

hehe

This works too:

Code: Select all

@echo off
for %%a in (*) do echo "%%~pxnda"

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

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

#20 Post by penpen » 26 Jan 2014 09:36

I found a simpler workaround:

Code: Select all

@echo off
cls
setlocal
set "filename=a filename with, a comma [1].txt"
if not exist "%filename%" ((echo test)>"%filename%")
if not exist "%SystemRoot%\system32\command.com" echo this workarround needs command.com.

:: get short form of the absolute file name
pushd .
%SystemRoot%\system32\command.com /? >nul 2>nul
set /A "N=2" & for %%a in ("%filename%") do for %%a in (%%~ta) do set /A "N+=1"
for /F "tokens=%N%" %%a in ('^(dir "%filename%" /X 2^>^&1^)^| findstr /L /C:"%filename%"') do set "sfile=%CD%\%%~a"
popd

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

endlocal
goto :eof

Or in other words: Does the following code works in other Windows versions with "command.com", too (funny way, to get the short path :lol: )?

Code: Select all

@echo off
pushd .
echo actual path: %CD%
%SystemRoot%\system32\command.com /? >nul 2>nul
echo short  path: %CD%
popd


penpen

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

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

#21 Post by foxidrive » 26 Jan 2014 09:52

Yep, it works in Win 8.1

Nice work.

actual path: "d:\abc\abc & def\123 456"
short path: "d:\abc\ABC&DE~1\123456~1"
Press any key to continue . . .


WMIC DATAFILE WHERE name="d:\\abc\\ABC&DE~1\\123456~1\\AFILEN~1.TXT" GET lastmodified
20140127025101.025142+660
Press any key to continue . . .


npocmaka_
Posts: 516
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

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

#22 Post by npocmaka_ » 27 Jan 2014 04:34

My first impulse here was to try with WQL query with like+wildcards , but I cant do it work for data file :? .While it works fine with services for example...

Code: Select all

c:\>echo test>c:\test.file
c:\>wmic DataFile where 'path like "%C:\\test.file.bat%"' get lastmodified


This always returns `No Instance(s) Available.` Is it even possible to call wmic+like and datafile?

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

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

#23 Post by penpen » 27 Jan 2014 05:43

If wmic query on xp does not differ from other windows versions, you have some errors/wrong assumptions in there, see this example:

Code: Select all

Z:\>md test.path

Z:\>echo test>"z:\test.path\test.file"

Z:\>echo test2>"z:\test.path\test2.file"

Z:\>wmic DataFile where 'drive="Z:" and path like "%test.path%"' get lastmodified
LastModified
20140127121614.497867+060
20140127121618.834630+060

Z:\>wmic DataFile where 'drive="Z:" and path like "%test%"' get lastmodified
LastModified
20140127121614.497867+060
20140127121618.834630+060

penpen

npocmaka_
Posts: 516
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

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

#24 Post by npocmaka_ » 27 Jan 2014 08:39

ahaam. the path property does not contain the drive.But still cannot make it work in all cases at least on my machine.


c:\tmpf>cd c:\

c:\>md tmpf2

c:\>cd tmpf2

c:\tmpf2>echo a>testFileWithUniqueName

c:\tmpf2>wmic DataFile where 'drive="C:" and name like "%testFileWithUniqueName%"' get lastmodified,path,name
No Instance(s) Available.

c:\tmpf2>wmic DataFile where 'drive="C:" and path like "%testFileWithUniqueName%"' get lastmodified,path,name
No Instance(s) Available.

c:\tmpf2>wmic DataFile where 'drive="C:" and name like "%tmpf2%"' get lastmodified,path,name
No Instance(s) Available.

c:\tmpf2>wmic DataFile where 'drive="C:" and path like "%tmpf2%"' get lastmodified,path,name
No Instance(s) Available.


But have no problem to find older files...Also I have a suspicious that if there's an variable like %expressions% it is substituted

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

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

#25 Post by penpen » 27 Jan 2014 11:50

This is strange... the second wmic command line produces "No Instance(s) Available." as the filename is not its path,
but the other should work, so the output should look like this:

Code: Select all

Z:\>test

Z:\>md tmpf2

Z:\>cd tmpf2

Z:\tmpf2>echo a 1>testFileWithUniqueName

Z:\tmpf2>echo b 1>b

Z:\tmpf2>wmic DataFile where 'drive="Z:" and name like "%testFileWithUniqueName%"' get lastmodified,path,name
LastModified               Name                             Path
20140127183824.143528+060  z:\tmpf2\testfilewithuniquename  \tmpf2\


Z:\tmpf2>wmic DataFile where 'drive="Z:" and path like "%testFileWithUniqueName%"' get lastmodified,path,name
Keine Instanzen verfügbar.



Z:\tmpf2>wmic DataFile where 'drive="Z:" and name like "%tmpf2%"' get lastmodified,path,name
LastModified               Name                             Path
20140127183824.153542+060  z:\tmpf2\b                       \tmpf2\
20140127183824.143528+060  z:\tmpf2\testfilewithuniquename  \tmpf2\


Z:\tmpf2>wmic DataFile where 'drive="Z:" and path like "%tmpf2%"' get lastmodified,path,name
LastModified               Name                             Path
20140127183824.153542+060  z:\tmpf2\b                       \tmpf2\
20140127183824.143528+060  z:\tmpf2\testfilewithuniquename  \tmpf2\
Maybe the xp wmic differs from your oses one.

penpen

Edit: I get your output if the paramters interferes with environment variables only, for example (actually i have no other idea what might happened):

Code: Select all

set "testFileWithUniqueName=abc"
set "tmpf2=def"

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

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

#26 Post by Sponge Belly » 28 Jan 2014 16:26

Hi Again! :-)

I wondered why nobody replied to my posts. Then I realised Foxi was talking about square brackets in the filename and not the parentheses around the name clause of the wmic command. :oops:

Anyways, here’s my mshta solution which is very much in vogue these days thanks to npocmaka and Einstein1969:

Code: Select all

set "prop=LastModified"
set lm1=CreateObject(""Scripting.FileSystemObject""^).^
GetStandardStream(1^).Write GetObject(""winMgmts:CIM_DataFile.Name
set lm2=""^).%prop%:Close

for /f %%m in ('mshta vbscript:execute("%lm1%='%~dpf1'%lm2%"^)') ^
do echo(file "%~nx1" last modified on: %%m


Works with poison characters in folders and filenames. Change LastModified to CreationDate or any other CIM_DataFile property name you want. Unlike wmic, there’s no CR at end of string. And thanks to Dave Benham for pointing out that there’s no need to pipe mshta’s output through find or more if mshta is inside a for /f loop’s in (…) clause.

- SB

Edit: Tidied up code snippet. Added link to CIM_DataFile.
Last edited by Sponge Belly on 03 Feb 2014 10:32, edited 1 time in total.

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

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

#27 Post by foxidrive » 29 Jan 2014 03:05

Sponge Belly wrote:I realised Foxi was talking about square brackets in the filename and not the parentheses around the name clause of the wmic command. :oops:


It was commas originally, and the work around to fix them broke round brackets.

Your solution works here in Windows 8.1

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

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

#28 Post by Sponge Belly » 29 Jan 2014 16:49

Thanks Foxi! :D

I forgot to mention that it should also work for any poor Windows XP Home bunnies out there who don't have the wmic command available to them.

Btw, does anyone know how to do the mshta equivalent of the following:

Code: Select all

wmic os get localdatetime


It might prove useful.

TIA! ;-)

- SB

npocmaka_
Posts: 516
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

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

#29 Post by npocmaka_ » 29 Jan 2014 17:35

Sponge Belly wrote:
Btw, does anyone know how to do the mshta equivalent of the following:

Code: Select all

wmic os get localdatetime


It might prove useful.

TIA! ;-)

- SB


Here it is http://msdn.microsoft.com/en-us/library ... 71(v=vs.85).aspx

Win32_LocalTime class --> http://www.activexperts.com/admin/scrip ... ript/0130/

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

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

#30 Post by Sponge Belly » 31 Jan 2014 15:24

Thanks for the pointers, npocmaka. :-)

But this thread on a Russian forum proved to be instructive. Retouching the script I found there, I ended up with:

Code: Select all

set ^"js=javascript:close(new ActiveXObject('Scripting.^
FileSystemObject'^).GetStandardStream(1^).Write(new String(new ^
Enumerator(new ActiveXObject('WbemScripting.SWbemLocator'^).^
ConnectServer('.','root\\cimv2'^).ExecQuery('Select LocalDateTime ^
from Win32_OperatingSystem where Primary=True'^)^).item(^).^
LocalDateTime^)^)^);^"

for /f %%t in ('mshta "%js%"') do echo(WMI LocalDateTime: %%t


Long-winded, but it works! And it should work for everyone, even any poor Windows XP Home bunnies still left out there. ;-)

- SB

PS: Rewrote LocalDateTime code in VBScript and it turned out to be less verbose than the JavaScript version! :shock:
Last edited by Sponge Belly on 03 Feb 2014 10:05, edited 2 times in total.

Post Reply