移动特定起始位置的“快捷方式”
快捷方式都对应一个的目标和“起始位置”,现在想要把特定起始位置的快捷方式移动到一个文件夹中。
新建文本文档,输入如下内容:
# 设置变量
$oldPath = "D:\111\111_1"
$newPath = "D:\111\111_1\2"
# 检查目标文件夹是否存在,如果不存在则创建
if (-not (Test-Path -Path $newPath)) {
New-Item -ItemType Directory -Path $newPath -Force
}
# 获取旧路径的长度
$oldPathLength = $oldPath.Length
# 获取目标文件夹中的所有快捷方式
$shortcuts = Get-ChildItem $oldPath -Filter *.lnk
# 遍历每个快捷方式
foreach ($shortcut in $shortcuts) {
# 获取快捷方式的目标路径
$targetPath = (New-Object -ComObject WScript.Shell).CreateShortcut($shortcut.FullName).TargetPath
# 如果目标路径以旧路径开头
if ($targetPath.Substring(0, $oldPathLength) -eq $oldPath) {
# 将快捷方式移动到新目标文件夹中
Move-Item -Path $shortcut.FullName -Destination $newPath -Force
}
}
路径根据情况修改。代码中的 `$oldPath` 和 `$newPath` 分别代表需要查找的快捷方式起始路径和将要把快捷方式