如何在 WMIC 的 WHERE 子句中同时转义逗号和右括号? [英] How to escape both comma and closing parenthesis in WHERE clause of WMIC?

查看:27
本文介绍了如何在 WMIC 的 WHERE 子句中同时转义逗号和右括号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下 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(这通常会失败,我猜是由于错误的数据类型)
WHERE Name="D:\Data\sample.txt"(使用 , 失败)
WHERE Name='D:\Data\sample.txt'(这失败了 ,)*
WHERE (Name="D:\Data\sample.txt")(这失败了 ))
WHERE (Name='D:\Data\sample.txt')(这失败了 ))*
WHERE 'Name="D:\Data\sample.txt"'(使用 , 失败)
WHERE "Name='D:\Data\sample.txt'"(使用 , 失败)
WHERE "Name="D:\Data\sample.txt""(这失败了 ,)*
WHERE ^"Name="D:\Data\sample.txt"^" (这失败了 ,)
使用 转义 和/或 ) 也不起作用;

<子>*) 我不喜欢这种尝试,因为没有涉及到包含文件路径的 "",这可能会导致分隔符出现问题 (SPACE, TAB;=,) 或特殊字符,如 ^, &().

那么有什么方法可以让 wmic 查询的文件路径中的字符 ,) 不失败?是否有任何特殊字符(序列)来转义逗号或右括号?或者是否有另一种方法可以解决这个问题,使用不同类型的查询或 WHERE 子句?

<小时>

有一个类似的问题:如何在 WMIC 中像字符串一样转义逗号;但它只是关于转义 , 并且没有完全详细说明转义 ) 也.这就是为什么我要问...

解决方案

下面的方法允许在文件路径中使用逗号,但 如果右括号 ) 出现:

==>wmic DataFile WHERE (Name = "D:\bat\Unusual Names\2c,comma.txt") get Name, LastModified上次修改名称20160513080305.362206+120 d:atunusual names2c,comma.txt

编辑.添加以下示例以响应@Rublacava 评论:

==>wmic DataFile WHERE (Name = "d:\bat\Unusual Names\2c,comma\2c,comma.txt") get Name, LastModified上次修改名称20160514132334.866055+120 d:atunusual names2c,comma2c,comma.txt

<块引用>

相反,下面的方法允许一个右括号 ) 在文件路径,但如果出现逗号 , 则失败:

==>wmic DataFile WHERE "Name = 'D:\bat\Unusual Names\28(parens_29).txt'" get Name, LastModified上次修改名称20160513104341.746838+120 d:atunusual names28(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'""" ^|选择名称,LastModified ^|英尺 - 自动调整大小:::: 更具可读性::powershell -Command Get-WmiObject -Query """Select * from CIM_DataFile where "^"name = 'D:\bat\Unusual Names\2c,comma_28(parens_29).txt'""" ^^|选择名称,LastModified ^|英尺 - 自动调整大小:::: 更具可读性::设置_filePath=D:atUnusual Names2c,comma_28(parens_29).txt"powershell -Command Get-WmiObject -Query ^"""Select * from CIM_DataFile where name = '%_filePath:=\%'""" ^^|选择名称,LastModified ^|英尺 - 自动调整大小

输出(以上代码剪切粘贴到打开的cmd窗口中):

==>powershell -Command Get-WmiObject -Query """Select * from CIM_DataFile wherename = 'D:\bat\Unusual Names\2c,comma_28(parens_29).txt'""" ^| 选择名称,上次修改 ^|英尺 - 自动调整大小名称 LastModified---- ------------d:atunusual names2c,comma_28(parens_29).txt 20160513103717.765243+120==>::==>:: 更具可读性==>::==>powershell -Command Get-WmiObject -Query """Select * from CIM_DataFile where "^更多的?"name = 'D:\bat\Unusual Names\2c,comma_28(parens_29).txt'""" ^更多的?^|选择名称,LastModified ^|英尺 - 自动调整大小名称 LastModified---- ------------d:atunusual names2c,comma_28(parens_29).txt 20160513103717.765243+120==>::==>:: 更具可读性==>::==>设置_filePath=D:atUnusual Names2c,comma_28(parens_29).txt"==>powershell -Command Get-WmiObject -Query ^更多的?"""Select * from CIM_DataFile where name = '%_filePath:=\%'""" ^更多的?^|选择名称,LastModified ^|英尺 - 自动调整大小名称 LastModified---- ------------d:atunusual names2c,comma_28(parens_29).txt 20160513103717.765243+120==>

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

解决方案

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

==>

这篇关于如何在 WMIC 的 WHERE 子句中同时转义逗号和右括号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆