Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 2.0 读取名称包含括号的文本文件?_Powershell 2.0 - Fatal编程技术网

Powershell 2.0 读取名称包含括号的文本文件?

Powershell 2.0 读取名称包含括号的文本文件?,powershell-2.0,Powershell 2.0,当我尝试从文件夹中读取文本文件时,我发现一些有趣的事情: $files = Get-ChildrenItem "C:\MyFolder" -Recursive | ?{$_.Extension -like '.txt'} foreach ($file in $files) { [string]fileFullName = $file.FullName $content = Get-Content -Path $fileName .... } 如果文件名包含[and

当我尝试从文件夹中读取文本文件时,我发现一些有趣的事情:

 $files = Get-ChildrenItem "C:\MyFolder" -Recursive | ?{$_.Extension -like '.txt'}
 foreach ($file in $files) {
    [string]fileFullName = $file.FullName
    $content = Get-Content -Path $fileName
    ....
 }
如果文件名包含[and]字符,例如[1].txt,我会遇到一些异常


我猜在PS中每件事都是对象。因此,就我的理解而言,[..]在文件名中可能被视为索引访问。有没有办法处理带括号的文件?

您需要
-LiteralPath

$files = Get-ChildItem "C:\MyFolder" -Recurse | ? { $_.Extension -like '.txt' }

foreach ($file in $files) {
    [string]$fileFullName = $file.FullName
    $content = Get-Content -LiteralPath $fileFullName
    $content
}
在详细帮助()中,您将看到:

-文字路径 指定项目的路径。与Path不同,LiteralPath的值的使用方式与键入的完全相同。没有字符是 解释为通配符。如果路径包含转义字符, 用单引号将其括起来。单引号表示 Windows PowerShell不将任何字符解释为转义 序列