我想做一个脚本,这将采取一个父目录,其中有许多子目录,其中有文件。使用一个列表,我希望将子目录中的所有文件移到父目录中。
到目前为止,我已经创建了以下代码,它列出了子目录中指定类型的所有文件,但我不确定如何批量移动所有子文件。
Write-host "Please enter source Dir:"
$sourceDir = read-host
Write-Host "Format to look for with . :"
$format = read-host
#Write-host "Please enter output Dir:"
#$outDir = read-host
$Dir = get-childitem -Path $sourceDir -Filter $format -recurse | format-table name
$files = $Dir | where {$_.extension -eq "$format"}
$files #| format-table name发布于 2011-08-23 04:29:02
以下是一些事情:
WhatIf开关将显示要移动的文件。删除它以执行命令。您可能还需要twick它来处理重复的文件名。
$sourceDir = read-host "Please enter source Dir"
$format = read-host "Format to look for"
Get-ChildItem -Path $sourceDir -Filter $format -Recurse | Move-Item -Destination $sourceDir -Whatif发布于 2011-08-23 02:58:21
如果我没理解错的话,您可以在文件上使用Move-Item将它们移动到输出目录:
$Dir = get-childitem $sourceDir -recurse
$files = $Dir | where {$_.extension -eq "$format"}
$files | move-item -destination $outDir发布于 2017-12-07 14:36:34
之前的一个帖子指出,该脚本将覆盖同名的文件。通过测试扩展脚本并避免这种可能性是可能的。如下所示:
$sourceDir = read-host "Please enter source Dir"
$format = read-host "Format to look for?"
$destDir = read-host "Please enter Destination Dir"
Get-ChildItem -Path $sourceDir -Filter $format -Recurse | Copy-Item -Destination $DestDir
$files = $DestDir | where {$_.extension -eq "$format"}
If (Test-Path $files) {
$i = 0
While (Test-Path $DestinationFile) {
$i += 1
$DestinationFile = "$files$i.$format"
Copy-Item -Destination $DestDir $DestinationFile
}
}
Else {
$DestinationFile = "$files$i.$format"
Copy-Item -Destination $DestDir $DestinationFile
}
Copy-Item -Path $SourceFile -Destination $DestinationFile -Forcehttps://stackoverflow.com/questions/7151408
复制相似问题