Powershell:递归移动文件
我正在尝试将所有构建输出文件和文件夹复制到 Bin 文件夹 (OutputDir/Bin) 中,但保留在 OutputDir 中的某些文件除外。 Bin 文件夹永远不会被删除。
初始条件:
Output
config.log4net
file1.txt
file2.txt
file3.dll
ProjectXXX.exe
en
foo.txt
fr
foo.txt
de
foo.txt
目标:
Output
Bin
file1.txt
file2.txt
file3.dll
en
foo.txt
fr
foo.txt
de
foo.txt
config.log4net
ProjectXXX.exe
我的第一次尝试:
$binaries = $args[0]
$binFolderName = "bin"
$binFolderPath = Join-Path $binaries $binFolderName
New-Item $binFolderPath -ItemType Directory
Get-Childitem -Path $binaries | ? {$_.Name -notlike "ProjectXXX.*" -and $_.Name -ne "config.log4net" -and $_.Name -ne $binFolderName } | Move-Item -Destination $binFolderPath
这不起作用,因为 Move-Item
无法覆盖文件夹。
我的第二次尝试:
function MoveItemsInDirectory {
param([Parameter(Mandatory=$true, Position=0)][System.String]$SourceDirectoryPath,
[Parameter(Mandatory=$true, Position=1)][System.String]$DestinationDirectoryPath,
[Parameter(Mandatory=$false, Position=2)][System.Array]$ExcludeFiles)
Get-ChildItem -Path $SourceDirectoryPath -Exclude $ExcludeFiles | %{
if ($_ -is [System.IO.FileInfo]) {
$newFilePath = Join-Path $DestinationDirectoryPath $_.Name
xcopy $_.FullName $newFilePath /Y
Remove-Item $_ -Force -Confirm:$false
}
else
{
$folderName = $_.Name
$folderPath = Join-Path $DestinationDirectoryPath $folderName
MoveItemsInDirectory -SourceDirectoryPath $_.FullName -DestinationDirectoryPath $folderPath -ExcludeFiles $ExcludeFiles
Remove-Item $_ -Force -Confirm:$false
}
}
}
$binaries = $args[0]
$binFolderName = "bin"
$binFolderPath = Join-Path $binaries $binFolderName
$excludeFiles = @("ProjectXXX.*", "config.log4net", $binFolderName)
MoveItemsInDirectory $binaries $binFolderPath $excludeFiles
是否有其他方法可以使用 PowerShell 以更简单的方式递归移动文件?
I am trying to copy all build output files and folders into a Bin folder (OutputDir/Bin) except of some files which stay in the OutputDir. The Bin folder will never be deleted.
Initial condition:
Output
config.log4net
file1.txt
file2.txt
file3.dll
ProjectXXX.exe
en
foo.txt
fr
foo.txt
de
foo.txt
Target:
Output
Bin
file1.txt
file2.txt
file3.dll
en
foo.txt
fr
foo.txt
de
foo.txt
config.log4net
ProjectXXX.exe
My first try:
$binaries = $args[0]
$binFolderName = "bin"
$binFolderPath = Join-Path $binaries $binFolderName
New-Item $binFolderPath -ItemType Directory
Get-Childitem -Path $binaries | ? {$_.Name -notlike "ProjectXXX.*" -and $_.Name -ne "config.log4net" -and $_.Name -ne $binFolderName } | Move-Item -Destination $binFolderPath
This does not work, because Move-Item
is not able to overwrite folders.
My second try:
function MoveItemsInDirectory {
param([Parameter(Mandatory=$true, Position=0)][System.String]$SourceDirectoryPath,
[Parameter(Mandatory=$true, Position=1)][System.String]$DestinationDirectoryPath,
[Parameter(Mandatory=$false, Position=2)][System.Array]$ExcludeFiles)
Get-ChildItem -Path $SourceDirectoryPath -Exclude $ExcludeFiles | %{
if ($_ -is [System.IO.FileInfo]) {
$newFilePath = Join-Path $DestinationDirectoryPath $_.Name
xcopy $_.FullName $newFilePath /Y
Remove-Item $_ -Force -Confirm:$false
}
else
{
$folderName = $_.Name
$folderPath = Join-Path $DestinationDirectoryPath $folderName
MoveItemsInDirectory -SourceDirectoryPath $_.FullName -DestinationDirectoryPath $folderPath -ExcludeFiles $ExcludeFiles
Remove-Item $_ -Force -Confirm:$false
}
}
}
$binaries = $args[0]
$binFolderName = "bin"
$binFolderPath = Join-Path $binaries $binFolderName
$excludeFiles = @("ProjectXXX.*", "config.log4net", $binFolderName)
MoveItemsInDirectory $binaries $binFolderPath $excludeFiles
Is there any alternative way of moving files recursively in a more easy way using PowerShell?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将
Move-Item
命令替换为Copy-Item
命令,之后,您只需调用Remove-Item
即可删除移动的文件代码>:You could replace the
Move-Item
command with anCopy-Item
command and after that, you can delete the files you moved by simply callingRemove-Item
:如前所述,Move-Item 不会覆盖文件夹,因此您只能进行复制。另一种解决方案是使用 /MOV 开关(等等!)为 for-each 循环中的每个文件调用 Robocopy;这将移动然后删除源文件。
As already stated, Move-Item will not overwrite folders so you're left with copying. An alternative solution would be to call Robocopy for each file in your for-each loop with the /MOV switch (amongst others!); this will move then delete the source file.