Page 2 of 3

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

Posted: 24 Jan 2014 00:48
by Endoro
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.

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

Posted: 24 Jan 2014 01:23
by foxidrive
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. ;)

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

Posted: 24 Jan 2014 01:46
by Endoro
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).

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

Posted: 24 Jan 2014 03:11
by foxidrive
hehe

This works too:

Code: Select all

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

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

Posted: 26 Jan 2014 09:36
by penpen
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

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

Posted: 26 Jan 2014 09:52
by foxidrive
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 . . .


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

Posted: 27 Jan 2014 04:34
by npocmaka_
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?

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

Posted: 27 Jan 2014 05:43
by penpen
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

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

Posted: 27 Jan 2014 08:39
by npocmaka_
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

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

Posted: 27 Jan 2014 11:50
by penpen
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"

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

Posted: 28 Jan 2014 16:26
by Sponge Belly
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.

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

Posted: 29 Jan 2014 03:05
by foxidrive
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

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

Posted: 29 Jan 2014 16:49
by Sponge Belly
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

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

Posted: 29 Jan 2014 17:35
by npocmaka_
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/

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

Posted: 31 Jan 2014 15:24
by Sponge Belly
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: