migrate_envs.ps1
3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# 需要以管理员身份运行 (右键 -> 以管理员身份运行 PowerShell,然后执行本脚本)
# 作用:把 D:\software_install\anaconda\envs 下的环境完整迁移到 D:\software_install\anaconda_env\envs
# - askdata / data_report / gl_agent / hello_agent_py312 / openSpec_test / qwen_llm_test / study_agent_demo: 完整搬过去
# - wiki_knowledge: 目标目录已有更新的版本,直接删除源目录里的旧版本
$ErrorActionPreference = "Stop"
$isAdmin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if (-not $isAdmin) {
Write-Host "请以管理员身份运行本脚本!(右键 PowerShell -> 以管理员身份运行,再执行本脚本)" -ForegroundColor Red
exit 1
}
$src = "D:\software_install\anaconda\envs"
$dst = "D:\software_install\anaconda_env\envs"
$envsToMove = @("askdata","data_report","gl_agent","hello_agent_py312","openSpec_test","qwen_llm_test","study_agent_demo")
foreach ($e in $envsToMove) {
$s = Join-Path $src $e
$d = Join-Path $dst $e
if (-not (Test-Path $s)) {
Write-Host "跳过 $e:源目录不存在(可能已迁移过)" -ForegroundColor Yellow
continue
}
Write-Host "迁移 $e ..." -ForegroundColor Cyan
# robocopy 完整复制(含权限、时间戳),失败自动重试
robocopy $s $d /E /COPYALL /R:2 /W:2 /NFL /NDL /NJH /NJS /MT:8
$rc = $LASTEXITCODE
# robocopy 退出码 0-7 都算成功(8+ 才是真错误)
if ($rc -ge 8) {
Write-Host " robocopy 失败,退出码 $rc,跳过删除源目录 $e" -ForegroundColor Red
continue
}
# 校验文件数量一致再删除源目录
$sCount = (Get-ChildItem $s -Recurse -Force -ErrorAction SilentlyContinue | Measure-Object).Count
$dCount = (Get-ChildItem $d -Recurse -Force -ErrorAction SilentlyContinue | Measure-Object).Count
if ($sCount -eq $dCount) {
Remove-Item $s -Recurse -Force
Write-Host " 完成,源目录已删除 ($e, $dCount 个文件)" -ForegroundColor Green
} else {
Write-Host " 文件数量不一致 (源=$sCount 目标=$dCount),未删除源目录,请人工检查 $e" -ForegroundColor Red
}
}
# 处理同名的 wiki_knowledge:目标目录已是最新版本,直接删除源目录里的旧版本
$oldWiki = Join-Path $src "wiki_knowledge"
if (Test-Path $oldWiki) {
Write-Host "删除源目录里多余的 wiki_knowledge(目标目录版本为准)..." -ForegroundColor Cyan
Remove-Item $oldWiki -Recurse -Force
Write-Host " 完成" -ForegroundColor Green
} else {
Write-Host "源目录 wiki_knowledge 不存在,跳过" -ForegroundColor Yellow
}
# 清理 conda 的环境注册表,去掉指向旧目录的记录
$envTxt = "$env:USERPROFILE\.conda\environments.txt"
if (Test-Path $envTxt) {
$lines = Get-Content $envTxt | Where-Object { $_ -notlike "D:\software_install\anaconda\envs\*" -and $_ -ne "D:\software_install\anaconda" }
Set-Content $envTxt $lines
Write-Host "已清理 environments.txt 里指向旧目录的记录" -ForegroundColor Green
}
Write-Host ""
Write-Host "全部完成!现在 $src 应该只剩空目录结构,$dst 应该有全部 8 个环境。" -ForegroundColor Green
Write-Host "剩余内容:" -ForegroundColor Cyan
Get-ChildItem $src -Directory -ErrorAction SilentlyContinue | ForEach-Object { Write-Host " 源目录仍有: $($_.Name)" -ForegroundColor Yellow }
Get-ChildItem $dst -Directory -ErrorAction SilentlyContinue | ForEach-Object { Write-Host " 目标目录: $($_.Name)" }