Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
456 views
in Technique[技术] by (71.8m points)

cmd - How to escape both comma and closing parenthesis in WHERE clause of WMIC?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:atunusual names2c,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:atunusual names2c, comma2c,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:atunusual names28(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:atUnusual Names2c,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:atunusual names2c,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:atunusual names2c,comma_28(parens_29).txt 20160513103717.765243+120

==> ::
==> :: even more readable
==> ::
==> set "_filePath=D:atUnusual Names2c,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:atunusual names2c,comma_28(parens_29).txt 20160513103717.765243+120

==>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...