我正在使用此答案中的PowerShell脚本来执行文件复制.当我想使用过滤器包含多个文件类型时出现问题.
Get-ChildItem $originalPath -filter "*.htm" | `
foreach{ $targetFile = $htmPath + $_.FullName.SubString($originalPath.Length); `
New-Item -ItemType File -Path $targetFile -Force; `
Copy-Item $_.FullName -destination $targetFile }
Run Code Online (Sandbox Code Playgroud)
像梦一样工作.但是,当我想使用过滤器包含多个文件类型时,会出现问题.
Get-ChildItem $originalPath `
-filter "*.gif","*.jpg","*.xls*","*.doc*","*.pdf*","*.wav*",".ppt*") | `
foreach{ $targetFile = $htmPath + $_.FullName.SubString($originalPath.Length); `
New-Item -ItemType File -Path $targetFile -Force; `
Copy-Item $_.FullName -destination $targetFile }
Run Code Online (Sandbox Code Playgroud)
给我以下错误:
Get-ChildItem : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Filter'. Specified method is not supported.
At F:\data\foo\CGM.ps1:121 char:36
+ Get-ChildItem $originalPath …
Run Code Online (Sandbox Code Playgroud) 来自文档:
-包括
仅检索指定的项目.此参数的值限定Path参数.输入路径元素或模式,例如"*.txt".允许使用通配符.
仅当命令包含Recurse参数或路径指向目录内容(例如C:\ Windows*)时,Include参数才有效,其中通配符指定C:\ Windows目录的内容.
我的第一个理解是:
c:\test\a.txt
c:\test\b.txt
Run Code Online (Sandbox Code Playgroud)
所以要得到'a.txt'和'b.txt'我可以写:
gci -Path "c:\test\*" -Include "*.txt"
Run Code Online (Sandbox Code Playgroud)
这很有效.但现在考虑这样的等级:
c:\test\a.txt
c:\test\b.txt
c:\test\c.txt\c.txt
Run Code Online (Sandbox Code Playgroud)
相同的命令返回:a.txt,b.txt,c.txt
实际逻辑似乎是:
-Include用于匹配-Path指定的所有实体.如果匹配元素是文件 - 返回它.如果匹配的元素是文件夹,请查看内部并返回匹配的第一级子项.
此外,文件说:
仅当命令包含Recurse参数或路径指向目录内容时,Include参数才有效...
这也是错误的.例如
gci -Path "c:\test" -Include "*.txt"
Run Code Online (Sandbox Code Playgroud)
它什么都不返回,而没有-Include我得到文件夹内容.所以 - 包含绝对是"有效的".这里到底发生了什么?-Path指定"c:\ test",-Include尝试匹配此路径.由于"*.txt"与"test"不匹配,因此没有返回任何内容.但看看这个:
gci -Path "c:\test" -Include "*t"
Run Code Online (Sandbox Code Playgroud)
它返回a.txt,b.txt和c.txt作为"*t"匹配的"test"并匹配所有子项.
毕竟,即使知道Include如何工作,我也不明白何时使用它.为什么我需要它来查看子文件夹内?为什么要这么复杂?
我的C:目录中有一个包含许多文件的列表.如果我尝试在其上运行-Exclude,则不会返回.与-Include相同.如果我使用过滤器,它会返回我期望的结果.我不明白它应该做什么?
以下是我正在运行并且什么都没得到的示例:
Get-ChildItem -Path C: -Exclude "*.txt"
Run Code Online (Sandbox Code Playgroud)
我一无所获.如果我跑
Get-Childitem -filter "*.txt"
Run Code Online (Sandbox Code Playgroud)
我得到了回报:
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 11/7/2007 8:00 AM 17734 eula.1028.txt
-a---- 11/7/2007 8:00 AM 17734 eula.1031.txt
-a---- 11/7/2007 8:00 AM 10134 eula.1033.txt
-a---- 11/7/2007 8:00 AM 17734 eula.1036.txt
-a---- 11/7/2007 8:00 AM 17734 eula.1040.txt
-a---- 11/7/2007 8:00 AM 118 eula.1041.txt
-a---- 11/7/2007 8:00 AM 17734 eula.1042.txt
-a---- 11/7/2007 8:00 AM 17734 eula.2052.txt
-a---- 11/7/2007 8:00 AM 17734 eula.3082.txt
7/7/2016 8:50 AM 93 HaxLogs.txt
-a---- …
Run Code Online (Sandbox Code Playgroud) 使用“ -filter”时:
Get-ChildItem -file -filter "*.txt" | foreach-object { write-host $_.FullName
}
我得到了当前文件夹中的4个.txt文件的列表。
我尝试使用“ -include”
Get-ChildItem -file -include *.txt | foreach-object { write-host $_.FullName }
Get-ChildItem -file -include *txt | foreach-object { write-host $_.FullName }
Run Code Online (Sandbox Code Playgroud)
我什么也没得到。我尝试使用和不使用“ -file”参数,都没有区别。
我看过各种指南/示例(ss64.com/TechNet等),并且据说我做得对。
有什么想法我可能做错了吗?谢谢!