我正在尝试使用以下 wmic
命令以与语言环境无关的方式检索文件的修改日期:
wmic DataFile WHERE Name="D:\\Data\\sample.txt" GET LastModified
只要给定的文件路径不包含任何逗号 ,
,这就可以完美地工作。
下面的方法允许在文件路径中使用逗号,但如果出现右括号 )
则失败:
wmic DataFile WHERE (Name="D:\\Data\\sample.txt") GET LastModified
到目前为止,我尝试了很多不同的组合,但都没有成功:
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;
<子>
*) 我不喜欢这种尝试,因为没有 ""
包含文件路径,这可能导致分隔符问题 (SPACE, TAB, ;
, =
和 ,
) 或特殊字符,如 ^
, &
、(
和 )
。
那么有没有办法让 wmic
查询的文件路径中的字符 、
和 )
不失败?是否有任何特殊字符(序列)来转义逗号或右括号?或者是否有另一种方法可以解决此问题,使用不同类型的查询或 WHERE
子句?
有个类似的问题:How do I escape comma in WMIC inside like string ;但它只是关于转义 ,
并且没有完全详细说明转义 )
。这就是为什么我要问...
最佳答案
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
编辑。添加以下示例以响应@Rublacava 评论:
==> 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
对于逗号 ,
和 右括号 )
together 似乎不存在通用的方法文件路径,例如2c,comma_28(parens_29).txt
.
但是,这里有一个使用 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
输出(以上代码片段粘贴到打开的 cmd
窗口中):
==> 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
==>
关于cmd - 如何在 WMIC 的 WHERE 子句中转义逗号和右括号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37198153/