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

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

问题描述

我试图使用以下 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 (这通常会失败,我猜是由于错误的数据类型)

WHERE Name = D:\\Data\\sample.txt (此操作失败,出现

WHERE Name ='D:\\Data\\sample.txt'(thi s以 *

WHERE(Name = D:\\ Data\\sample.txt)(此操作失败,出现

在哪里(Name ='D:\\Data\\sample.txt')(此操作失败, *

WHERE'Name = D:\\Data\\sample.txt'(此操作失败,

其中名称='D:\\Data\\sample.txt' (以失败)

WHERE Name = \ D:\\ Data\\sample.txt\ (此操作失败,出现 *

WHERE ^ Name = \ D:\\Data\\sample.txt\ ^ (此操作失败,

转义和/或 \ 的c>也不起作用;

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 ; = )或特殊字符,例如 ^ &

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

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

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?

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

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:\bat\unusual names\2c,comma.txt

编辑。为响应@Rublacava注释而添加了以下示例:

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

对于逗号 右括号 在一起,例如 2c,comma_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.

不过,以下是使用PowerShell的解决方法:

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

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

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

==>

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

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