Roo*_*ian 6 powershell scripting move
我试图将所有构建输出文件和文件夹复制到Bin文件夹(OutputDir/Bin),但保留在OutputDir中的一些文件除外.在滨文件夹将永远不会被删除.
初始条件:
Output
config.log4net
file1.txt
file2.txt
file3.dll
ProjectXXX.exe
en
foo.txt
fr
foo.txt
de
foo.txt
Run Code Online (Sandbox Code Playgroud)
目标:
Output
Bin
file1.txt
file2.txt
file3.dll
en
foo.txt
fr
foo.txt
de
foo.txt
config.log4net
ProjectXXX.exe
Run Code Online (Sandbox Code Playgroud)
我的第一次尝试:
$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
Run Code Online (Sandbox Code Playgroud)
这不起作用,因为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
Run Code Online (Sandbox Code Playgroud)
有没有其他方法使用PowerShell以更简单的方式递归移动文件?
您可以使用Move-Item命令替换该命令,Copy-Item然后,只需调用Remove-Item以下命令即可删除您移动的文件:
$a = ls | ? {$_.Name -notlike "ProjectXXX.*" -and $_.Name -ne "config.log4net" -and $_.Name -ne $binFolderName }
$a | cp -Recurse -Destination bin -Force
rm $a -r -force -Confirm:$false
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6362 次 |
| 最近记录: |