Skip to content

Commit 8c079ee

Browse files
committed
add. _posts\2024-09-30-Software-Config.md
1 parent 386eb7c commit 8c079ee

File tree

2 files changed

+107
-1
lines changed

2 files changed

+107
-1
lines changed

_posts/2012-06-22-Win10-OS-Install/doc.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: 「备忘」再再次谈系统安装【2021】
33
date: 2021-06-22 14:00:59
44
tags:
55
- 操作系统
6-
- win10
6+
- Win10
77
- 备忘
88
- 重装系统
99
categories:

_posts/2024-09-30-Software-Config.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
---
2+
title: 「备忘」使用符号链接映射软件配置文件夹
3+
date: 2024-09-30 12:42:45
4+
tags:
5+
- Windows
6+
- 备忘
7+
- 重装系统
8+
- Win10
9+
categories:
10+
- 电脑网络
11+
id: 670
12+
alias: 20220826264
13+
---
14+
15+
一些软件会把配置文件放在 `%USERPROFILE%\AppData\Local``%USERPROFILE%\AppData\Roaming`,其中一些需要备份一下在重装系统后恢复。。
16+
17+
<!--more-->
18+
19+
> 另:`%AppData%` 变量等同于 `%USERPROFILE%\AppData\Roaming`,而 `%LocalAppData%` 变量等同于 `%USERPROFILE%\AppData\Local`。。。
20+
21+
下边是我使用的方案:
22+
23+
- 1、在 `C:\config` 下存放真实的配置文件,`C:\config\Roaming``C:\config\Local` 分别对应 `%USERPROFILE%\AppData\Roaming``%USERPROFILE%\AppData\Local`
24+
- 2、并不是所有配置都需要备份,所以只映射需要的文件夹,比如 `C:\config\Local\Everything``C:\config\Roaming\Everything`
25+
- 3、重装系统前备份整个 C 盘到外置硬盘,重装系统后恢复 `C:\config`,再映射相应的文件夹;
26+
27+
再下边是自动化的 PowerShell 脚本:
28+
29+
- 1、保存脚本至 `C:\config\config.ps1`,按需调整 `$directoryList`
30+
- 2、在相应的软件安装前执行,「右键」→「使用 PowerShell 运行」,需要允许管理员权限;
31+
- 3、首次运行会自动创建 `C:\config\Roaming``C:\config\Local` 及内部的空文件夹,然后创建符号链接;
32+
- 4、如果软件已经安装,会提示:`The path $dir\$name is not a symbolic link.`,可以剪切**合并**`C:\config` 内相应文件夹,之后再次运行脚本;
33+
- 5、恢复时只需将 `C:\config` 复制到新系统,再次运行脚本,同样应在软件安装前执行;
34+
35+
**注:如果遇到「禁止运行脚本」,使用 `set-executionpolicy remotesigned` 修改执行策略;**
36+
37+
```powershell
38+
Set-Location "C:\config"
39+
40+
# 用于创建目录,两个参数,dir 和 name,判断 dir/name 是否存在,不存在则创建
41+
function CreateDirectory($dir, $name) {
42+
# $pwd = Get-Location
43+
if (!(Test-Path "$dir\$name")) {
44+
# 直接创建目录
45+
New-Item -ItemType Directory -Path "$dir\$name"
46+
}
47+
else {
48+
Write-Output "The path $pwd\$dir\$name already exists"
49+
}
50+
# 输出换行
51+
Write-Output ""
52+
}
53+
54+
# 用于创建符号链接到 $env:AppData 或 $env:LocalAppData
55+
function CreateSymbolicLink($dir, $name) {
56+
$srcDir = "$pwd\$dir\$name"
57+
# $dir 判断 Roaming 或 Local,替换成 $env:AppData 或 $env:LocalAppData
58+
if ($dir -eq "Roaming") {
59+
$dir = $env:AppData
60+
}
61+
elseif ($dir -eq "Local") {
62+
$dir = $env:LocalAppData
63+
}
64+
if (!(Test-Path "$dir\$name")) {
65+
# 创建符号链接
66+
cmd /c mklink /d "$dir\$name" $srcDir
67+
}
68+
else {
69+
$item = Get-Item "$dir\$name"
70+
if ($item.Attributes -band [System.IO.FileAttributes]::ReparsePoint) {
71+
Write-Host "The path $dir\$name is a symbolic link."
72+
}
73+
else {
74+
Write-Output "+++++++++++++++++++++++++++++"
75+
Write-Host "The path $dir\$name is not a symbolic link."
76+
Write-Output "+++++++++++++++++++++++++++++"
77+
}
78+
# Write-Output "$dir\$name already exists"
79+
}
80+
# 输出换行
81+
Write-Output ""
82+
}
83+
84+
# 判断管理员权限
85+
If (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]"Administrator")) {
86+
Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`" $PSCommandArgs" -Verb RunAs
87+
Exit
88+
}
89+
90+
# 获取需要创建的目录的列表
91+
$directoryList = @("Everything", "qBittorrent", "Resilio Sync", "Resilio Sync Service")
92+
93+
# 循环遍历目录列表,检查目录是否存在,不存在则创建
94+
foreach ($directory in $directoryList) {
95+
CreateDirectory "Roaming" $directory
96+
CreateSymbolicLink "Roaming" $directory
97+
CreateDirectory "Local" $directory
98+
CreateSymbolicLink "Local" $directory
99+
Write-Output "------------------------"
100+
Write-Output ""
101+
}
102+
103+
# 输入任意键关闭窗口
104+
Read-Host -Prompt "Press any key to continue..."
105+
106+
```

0 commit comments

Comments
 (0)