正如提到另外一个问题,如果你尝试做一个Get-ChildItem -filter ...命令你比,如果你使用的更有限-include替代-filter.我想阅读文件系统提供程序的过滤语法的官方文档,但经过半小时的搜索后,我仍然没有找到它们.谁知道在哪里看?
现在我使用PowerShell 5.0.10586.0更新到Windows 10 TH2 Build 10586
现在我遇到了Get-ChildItem的问题
$files = Get-ChildItem -LiteralPath $path -Force -Recurse -Include *.txt
Run Code Online (Sandbox Code Playgroud)
这将返回$ path中的所有文件,即使它们不是.txt.这在更新之前有效.当我改为
$files = Get-ChildItem -Path $path -Force -Recurse -Include *.txt
Run Code Online (Sandbox Code Playgroud)
它又有效了.但这不是我想要的.这是一个错误还是我做错了什么?
我正在遵循以下示例:
Get-ChildItem c:\scripts\*.* -include *.txt,*.log
Run Code Online (Sandbox Code Playgroud)
https://technet.microsoft.com/en-us/library/ee176841.aspx
是什么赋予了?当我尝试使用include时,为什么不取回我的test.txt文件列表?
作为旁注,是什么c:\scripts\*.*.它似乎在说包含任何具有任何格式名称的文件.但是不包含在指定中吗?无论如何,更感兴趣的是为什么我看似基本的代码不起作用.
我正在查找C:\ProgramFiles名为 的 jar 文件log4j-core-x.y.z.jar。我试图匹配最后一位数字z,它可以是一位数也可以是两位数(0-99)。我似乎无法获得正确的全局模式来完成此任务。
Code:
PS C:\Users\Administrator> Get-ChildItem -Path 'C:\Program Files\' -Filter log4j-core-*.*.[1-9][0-9].jar -Recurse -ErrorAction SilentlyContinue -Force | %{$_.FullName}
Run Code Online (Sandbox Code Playgroud)
这不会产生任何结果,但是当我只执行所有通配符时,如 ,-Filter log4j-core-*.*.*.jar我得到:
C:\Program Files\apache-log4j-2.16.0-bin\apache-log4j-2.16.0-bin\log4j-core-2.16.0-javadoc.jar
C:\Program Files\apache-log4j-2.16.0-bin\apache-log4j-2.16.0-bin\log4j-core-2.16.0-sources.jar
C:\Program Files\apache-log4j-2.16.0-bin\apache-log4j-2.16.0-bin\log4j-core-2.16.0-tests.jar
C:\Program Files\apache-log4j-2.16.0-bin\apache-log4j-2.16.0-bin\log4j-core-2.16.0.jar
Run Code Online (Sandbox Code Playgroud)
我唯一关心的是C:\Program Files\apache-log4j-2.16.0-bin\apache-log4j-2.16.0-bin\log4j-core-2.16.0.jar,log4j-core-2.16.0.jar
我错误地输入了一条路,而不是c:\foo.txt写了c:foo.txt.我预计它要么失败要么解决c:\foo.txt,但它似乎解决foo.txt了当前用户的主文件夹.
Powershell返回:
PS C:\> [System.IO.Path]::GetFullPath("c:\foo.txt")
c:\foo.txt
PS C:\> [System.IO.Path]::GetFullPath("c:foo.txt")
C:\Users\Administrator\foo.txt
PS C:\> [System.IO.Path]::GetFullPath("g:foo.txt")
G:\foo.txt
Run Code Online (Sandbox Code Playgroud)
从命令行运行explorer.exe并将其中的任何一个结果传递给要打开的C:\ Users\Administrator\Documents.
我没有找到任何相关文档,我完全糊涂了,请解释一下这种行为.
我从堆栈溢出中获得了以下代码,并且工作正常。
$TargetFolder = “Pathofyourfolder”
$Files = Get-ChildItem $TargetFolder -Exclude (gc List.txt) -Recurse
foreach ($File in $Files)
{
write-host “Deleting File $File” -foregroundcolor “Red”;
Remove-Item $File | out-null
}
Run Code Online (Sandbox Code Playgroud)
现在,我要删除列表中具有文件名的文件。我尝试了上述方法的一些变体,例如用Include替换Exclude,但没有成功。有人可以帮忙吗?