如何用PowerShell复制多版本发布目录下的Patch目录并保留原层级结构
PowerShell 实现代码
# 定义源根目录与目标根目录,可根据实际路径修改 $sourceRoot = "\Releases" $targetRoot = "C:\Temp\eureka" # 筛选符合版本规则的app版本目录 $matchVersionDirs = Get-ChildItem -Path $sourceRoot -Directory -Filter "app_26.6.*" | Where-Object { $versionSegments = $_.Name.Split('.') # 校验版本号第三段取值为1-7 $versionSegments.Count -ge 3 -and $versionSegments[2] -match '^[1-7]$' } # 遍历复制Patch目录,保留层级结构 foreach ($dir in $matchVersionDirs) { $patchSourcePath = Join-Path -Path $dir.FullName -ChildPath "Patch" if (Test-Path -Path $patchSourcePath -PathType Container) { $patchTargetPath = Join-Path -Path $targetRoot -ChildPath "$($dir.Name)\Patch" Copy-Item -Path $patchSourcePath -Destination $patchTargetPath -Recurse -Force } }
关键逻辑说明
- 版本匹配:通过分割目录名称校验版本号第三段取值范围,完全符合
26.6.[1-7].*的规则 - 层级保留:直接复用原版本目录名称拼接目标路径,自动保留
父目录\子目录的结构 - 异常兼容:增加了Patch目录存在性判断,避免无Patch目录的版本抛出执行错误
- 若不需要覆盖目标路径已存在的文件,删除
Copy-Item后的-Force参数即可
内容的提问来源于stack exchange,提问作者AAron
相关产品推荐
相关产品推荐

