3

I am trying to retrieve the modification date of a file in a locale-independent manner, using the following wmic command:

wmic DataFile WHERE Name="D:\\Data\\sample.txt" GET LastModified

This works perfectly as long as the given file path does not contain any comma ,.

The method below allows commas in the file path but fails if a closing parenthesis ) appears:

wmic DataFile WHERE (Name="D:\\Data\\sample.txt") GET LastModified

Up to now, I tried numerous different combinations, but without success:

WHERE Name=D:\\Data\\sample.txt (this fails in general, I guess due to wrong data type)
WHERE Name="D:\\Data\\sample.txt" (this fails with ,)
WHERE Name='D:\\Data\\sample.txt' (this fails with ,)*
WHERE (Name="D:\\Data\\sample.txt") (this fails with ))
WHERE (Name='D:\\Data\\sample.txt') (this fails with ))*
WHERE 'Name="D:\\Data\\sample.txt"' (this fails with ,)
WHERE "Name='D:\\Data\\sample.txt'" (this fails with ,)
WHERE "Name=\"D:\\Data\\sample.txt\"" (this fails with ,)*
WHERE ^"Name=\"D:\\Data\\sample.txt\"^" (this fails with ,)
escaping of , and/or ) with \ does not work either;

*) This attempts that I do not like, because there are no "" involved to enclose the file path, which could lead to problems with delimiters (SPACE, TAB, ;, = and the ,) or special characters like ^, &, ( and ).

So is there any way to allow both characters , and ) in the file path for the wmic query not to fail? Is there any special character (sequence) to escape commas or closing parentheses? Or is there perhaps another method to work around the issue, with a different type of query or WHERE clause?


There is a similar question: How do I escape comma in WMIC inside like string; but its is about escaping the , only and does not fully elaborate on escaping the ) also. That is why I am asking...

Community
  • 1
  • 1
aschipfl
  • 33,626
  • 12
  • 54
  • 99

3 Answers3

2

The method below allows commas in the file path but fails if a closing parenthesis ) appears:

==> wmic DataFile WHERE (Name = "D:\\bat\\Unusual Names\\2c,comma.txt") get Name, LastModified
LastModified               Name
20160513080305.362206+120  d:\bat\unusual names\2c,comma.txt

Edit. The following example added in response to @Rublacava comments:

==> wmic DataFile WHERE (Name = "d:\\bat\\Unusual Names\\2c, comma\\2c,comma.txt") get Name, LastModified
LastModified               Name
20160514132334.866055+120  d:\bat\unusual names\2c, comma\2c,comma.txt

On the contrary, the method below allows a closing parenthesis ) in the file path but fails if a comma , appears:

==> wmic DataFile WHERE "Name = 'D:\\bat\\Unusual Names\\28(parens_29).txt'" get Name, LastModified
LastModified               Name
20160513104341.746838+120  d:\bat\unusual names\28(parens_29).txt

It does not look to exist a common approach for both comma , and closing parenthesis ) together in the file path e.g. 2c,comma_28(parens_29).txt.

However, here's a workaround using PowerShell:

powershell -Command Get-WmiObject -Query """Select * from CIM_DataFile where name = 'D:\\bat\\Unusual Names\\2c,comma_28(parens_29).txt'""" ^| select name, LastModified ^| ft -AutoSize
::
::  a bit more readable
::
powershell -Command Get-WmiObject -Query """Select * from CIM_DataFile where "^
  "name = 'D:\\bat\\Unusual Names\\2c,comma_28(parens_29).txt'""" ^
  ^| select name, LastModified ^| ft -AutoSize
::
:: even more readable
::
set "_filePath=D:\bat\Unusual Names\2c,comma_28(parens_29).txt"
powershell -Command Get-WmiObject -Query ^
  """Select * from CIM_DataFile where name = '%_filePath:\=\\%'""" ^
  ^| select name, LastModified ^| ft -AutoSize

Output (above code snipped pasted into an open cmd window):

==> powershell -Command Get-WmiObject -Query """Select * from CIM_DataFile where
 name = 'D:\\bat\\Unusual Names\\2c,comma_28(parens_29).txt'""" ^| select name,
LastModified ^| ft -AutoSize

name                                            LastModified
----                                            ------------
d:\bat\unusual names\2c,comma_28(parens_29).txt 20160513103717.765243+120

==> ::
==> ::  a bit more readable
==> ::
==> powershell -Command Get-WmiObject -Query """Select * from CIM_DataFile where "^
More?   "name = 'D:\\bat\\Unusual Names\\2c,comma_28(parens_29).txt'""" ^
More?   ^| select name, LastModified ^| ft -AutoSize

name                                            LastModified
----                                            ------------
d:\bat\unusual names\2c,comma_28(parens_29).txt 20160513103717.765243+120

==> ::
==> :: even more readable
==> ::
==> set "_filePath=D:\bat\Unusual Names\2c,comma_28(parens_29).txt"

==> powershell -Command Get-WmiObject -Query ^
More?   """Select * from CIM_DataFile where name = '%_filePath:\=\\%'""" ^
More?   ^| select name, LastModified ^| ft -AutoSize

name                                            LastModified
----                                            ------------
d:\bat\unusual names\2c,comma_28(parens_29).txt 20160513103717.765243+120

==>
JosefZ
  • 28,460
  • 5
  • 44
  • 83
  • I can confirm that, contrary to "The method below allows commas in the file path", said method -- `wmic DataFile WHERE (Name = "D:\\bat\\Unusual Names\\2c,comma.txt") get Name, LastModified` -- does not work if there is one or more commas in a directory name. (e.g., `wmic DataFile WHERE (Name = "D:\\a\\Unusual\\2c, Names\\comma.txt") get LastModified`) – Rublacava Jul 28 '19 at 13:10
  • Maybe this is because it interprets "Unusual\2c, Names" as if "2c, Names" is a subdir of "Unusual" (and not one dir, as it ought to interpret it - "Unusual, Names"). If the directory with the comma is the parent directory of current directory then `set "olddirname=dirname2keep" && set "newdirname=nocommadirname" && cd .. && rename "%olddirname%" "%newdirname%" && cd %newdirname% && wmic DataFile WHERE (Name = "D:\\a\\%newdirname%\\comma.txt") get LastModified timeout 2 && cd .. && rename "%newdirname%" "%olddirname%"` works (with two newline characters between "LastModified" and "timeout"). – Rublacava Jul 28 '19 at 14:08
  • @Rublacava Sorry, I don't understand however see the updated answer. – JosefZ Jul 28 '19 at 15:15
  • @ JosefZ - I was elaborating on how your method does not work on directory names which have one or more commas, e.g., with "D:\dir\directory\sub,directory\filename.txt". – Rublacava Jul 29 '19 at 05:48
1

Meanwhile I found an (unfortunately incomprehensive) solution myself, which relies on the fact that there are 8.3 filenames enabled on the target disk drive, because for a filename containing commas a short filename without , is generated, so I only have to regard the closing parenthesis ). For example, the long file path:

"D:\Testing Data\(sh)f,n.txt"

becomes a short one like:

"D:\TESTIN~1\(SH)F_~1.txt"

To retrieve the short filename a for loop and the ~s modifier of its meta-variable are used:

rem // Return full file path with only short names:
for %%F in ("D:\Testing Data\(sh)f,n.txt") do @set "FILE=%%~sfF"

wmic DataFile WHERE "EightDotThreeFileName='%FILE:\=\\%'" GET LastModified

This nicely even works with a mixed path as input, meaning that the pure filename is short but the directory names in the parent path are long (though this would of course fail when the parent path contains , then):

rem // Return long parent path and short filename:
for %%F in ("D:\Data\(sh)f,n.txt") do @set "PPTH=%~dpF" & @set "NAME=%%~snxF"

wmic DataFile WHERE "EightDotThreeFileName='%PPTH:\=\\%%NAME%'" GET LastModified
aschipfl
  • 33,626
  • 12
  • 54
  • 99
1

Solution:

D:\>mklink /H "1.jpg" "D:\p\a\t\h\fi,le.jpg"
Hardlink created for 1.jpg <<===>> D:\p\a\t\h\fi,le.jpg

D:\>wmic datafile where Name="D:\\1.jpg" list /format:list

I understand not wanting to rename a file, view its metadata, then rename it back to its original filename. I re-tried @JosefZ solutions and they didn't work. I also tried JosefZ's Powershell solution and that didn't work. @aschipfl - Windows 10 didn't show the 8.3 filenames of files in an NTFS external HDD when I ran dir/x in cmd. (Windows 7 did show such filenames, and running fsutil behavior set disable8dot3 0 didn't fix it for Windows 10.)

It's not a perfect solution, for wmic is not a perfect program. Users who also had trouble escaping commas: Escaping strings when using wmic

Rublacava
  • 414
  • 1
  • 4
  • 16
  • Nice work-around! When you enable 8.3 file names, already present files do not get a short file name then, only for new ones (or renamed ones) such short names are generated… – aschipfl Sep 21 '20 at 14:03