本文介绍了PowerShell get-childitem不能处理以[字符开头,即使使用转义字符的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在c:\ temp目录中有一个名为[default] .xml的文件。
当我在powershell控制台中发出命令:get-childitemc:\temp` [default] .xml时,它不会返回任何内容。但是如果我发出这个命令:get-childitemc:\temp * default] .xml,它会按预期工作。

I have a file in c:\temp directory named as [default].xml.When I issue command: get-childitem "c:\temp`[default].xml" in the powershell console, it returns me nothing. However if I issue this command: get-childitem "c:\temp*default].xml", it works as expected.

不工作?

推荐答案

请参阅get-help about_wildcards

See get-help about_wildcards

[]是通配符globbing运算符。要对具有这些字符的文件使用get-childitem,请使用-literalpath开关来禁用文件规范的通配符:

The [] are wildcard "globbing" operators. To use get-childitem on files that have those characters, use the -literalpath switch to disable wildcarding of the file spec:

get-childitem -literalpath "c:\temp[default].xml"

工作在directoryinfo对象的.getfiles()方法中:

Alternatively, normal wildcarding still works in the .getfiles() method of a directoryinfo object:

(get-item c:\).getfiles("temp[default].*")

这篇关于PowerShell get-childitem不能处理以[字符开头,即使使用转义字符的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 17:48