PowerShell 脚本使 Windows 文件浏览器记住打开的路径
2024年10月4日大约 1 分钟
核心脚本
auto.ps1
可添加到开机自启动(使用后面的方法消除命令窗口)
$sleep = 10 * 60
$objShell = New-Object -COMObject "Shell.Application"
foreach($line in Get-Content -Encoding utf8NoBOM -Path '.\Folder_paths.log') {
$objShell.explore($line)
Start-Sleep -Milliseconds 500
}
Start-Sleep $sleep
while($true){
$processingTime = measure-command {
$contentStr = @($objShell.Windows()).Document.Folder.Self.Path
if($contentStr){
Set-Content -Encoding utf8NoBOM -Path '.\Folder_paths.log' -Value $contentStr
}
}
$newSleep = $sleep - $processingTime.totalSeconds
if ($newSleep -lt 0){
$newSleep = 0
}
Start-Sleep $newSleep
}
.\Folder_paths.log
为保存路径的文件
save.ps1
手动保存当前打开着的路径
$objShell = New-Object -COMObject "Shell.Application"
$contentStr = @($objShell.Windows()).Document.Folder.Self.Path
if($contentStr){
Set-Content -Encoding utf8NoBOM -Path '.\Folder_paths.log' -Value $contentStr
}
reopen.ps1
手动打开保存的路径
$objShell = New-Object -COMObject "Shell.Application"
foreach($line in Get-Content -Encoding utf8NoBOM -Path '.\Folder_paths.log') {
$objShell.explore($line)
Start-Sleep -Milliseconds 500
}
无窗口运行(不显示命令窗口)
auto.js
var wshShell = new ActiveXObject("WScript.Shell");
wshShell.Run('"pwsh.exe" -WindowStyle Hidden ".\\auto.ps1"', 0, false);
创建快捷方式,目标命令为 wscript.exe .\auto.js
save.js
var wshShell = new ActiveXObject("WScript.Shell");
wshShell.Run('"pwsh.exe" -WindowStyle Hidden ".\\save.ps1"', 0, false);
创建快捷方式,目标命令为 wscript.exe .\save.js
reopen.js
var wshShell = new ActiveXObject("WScript.Shell");
wshShell.Run('"pwsh.exe" -WindowStyle Hidden ".\\reopen.ps1"', 0, false);
创建快捷方式,目标命令为 wscript.exe .\reopen.js
参考资料
batch - How to get which folders are currently open in explorer? - Super User