我想递归地在目录“ x”中查找文件类型“ y”,不包括子目录“ m”并将文件复制到子目录“ m”
我正在尝试
$TargetDir = read-host "Please enter target Dir"
$sourceDir = read-host "Please enter source Dir"
$format = read-host "Format to look for"
Get-ChildItem -Path $sourceDir -Recurse -Filter $format | where FullName -Not -Like $TargetDir |
Copy-Item -Destination $TargetDir
答案 0 :(得分:1)
您最好尝试使用foreach
循环。这是搜索照片和视频的示例。
# Making an array of all files from Get-ChildItem
$searchItems = Get-ChildItem -Path $sourceDir -Recurse -Include *.mp4,*.jpg,*.jpeg | where FullName -notlike $TargetDir
# Copy files one by one in cycle
foreach($currentFile in $searchItems) {
Copy-Item -Path $currentFile.Fullname -Destination $TargetDir
}
答案 1 :(得分:0)
我想通了,但是要解释一些事情真是太好了 代码:
$TargetDir = read-host "Please enter target Dir"
$sourceDir = read-host "Please enter source Dir"
$format = read-host "Format to look for"
$folders = Get-ChildItem -Path $sourceDir -Recurse -filter $format | Where
{($_.PSIsContainer) -and ($TargetDir -notcontains $_.Name)}
foreach ($f in $folders){
Write-Host "These Files will be copied: $f"
Copy-Item - path $sourceDir$f -Destination $TargetDir\$f -Recure -Force}
我不明白:
Where {($_.PSIsContainer) -and ($TargetDir -notcontains $_.Name)}
为什么
Write-Host "These Files will be copied: $f"
运行时不显示
答案 2 :(得分:0)
这个...
[写主机“这些文件将被复制:$ f”]
...是因为您正在执行此操作,所以这是一个循环,并且Write-Host清除缓冲区。除了需要彩色屏幕输出时,Write-Host几乎没有用。好吧,除了某些格式化用例。通常只是不使用它。甚至PowerShell的作者也这么说。
除非另有说明,否则输出到屏幕是PowerShell的默认设置。 Write-Host的更好选择是Write-Output或Out-Host或Tee-Object。查看帮助文件的详细信息。
Get-ChildItem有多个开关,如果您只想要目录,那么可以使用一个开关,并且-Exclude很好。
# Get specifics for a module, cmdlet, or function
(Get-Command -Name Get-ChildItem).Parameters
(Get-Command -Name Get-ChildItem).Parameters.Keys
# Results
<#
...
Exclude
...
Directory --- (This is what that $_.PSIsContainer/$PSItem.PSIsContainer this is doing, but that is legacy stuff.)
...
#>
Get-help -Name Get-ChildItem -Examples
Get-help -Name Get-ChildItem -Full
Get-help -Name Get-ChildItem -Online
About Objects --- Objects in Pipelines
因此...
# Get directory names only and return only the full UNC
(Get-ChildItem -Directory -Path 'D:\SourceFolder'-Recurse).FullName
# Results
<#
D:\SourceFolder\est
D:\SourceFolder\here
D:\SourceFolder\hold
D:\SourceFolder\TargetFolder
#>
# Just like the above, but exclude the TargetFolder
(Get-ChildItem -Directory -Path 'D:\SourceFolder' -Exclude 'TargetFolder' -Recurse).FullName
# Results
<#
D:\SourceFolder\est
D:\SourceFolder\here
D:\SourceFolder\hold
#>
因此,对代码进行少量重构。
# Recursively get all text files, in all directories, except one for copy action
Get-ChildItem -Path 'D:\SourceFolder' -Filter '*.txt' -Exclude 'TargetFolder' -Recurse |
ForEach {
"These Files will be copied: $($PSItem.FullName)"
# Remove the -whatIf for the actual run.
Copy-Item -Path $PSItem.FullName -Destination 'D:\SourceFolder\TargetFolder' -Recurse -WhatIf
}
# Results
<#
These Files will be copied: D:\SourceFolder\here\mytest - Copy.txt
What if: Performing the operation "Copy File" on target "Item: D:\SourceFolder\here\mytest - Copy.txt Destination: D:\SourceFolder\TargetFolder\mytest - Copy.txt".
These Files will be copied: D:\SourceFolder\here\mytest.txt
What if: Performing the operation "Copy File" on target "Item: D:\SourceFolder\here\mytest.txt Destination: D:\SourceFolder\TargetFolder\mytest.txt".
These Files will be copied: D:\SourceFolder\hold\awél.txt
What if: Performing the operation "Copy File" on target "Item: D:\SourceFolder\hold\awél.txt Destination: D:\SourceFolder\TargetFolder\awél.txt".
#>
当然,您需要调整以上内容才能使用您的变量,并删除所有注释。发表评论是一件好事,但过度评论与评论不足或评论不佳/无法理解一样糟糕。所有这些都有各自的偏好。
我的规则: