如何保持批处理文件继续运行,直到打开的文件夹窗口关闭?

问题描述 投票:0回答:1

我已成功使用 /WAIT 命令运行批处理文件,该命令加载程序然后保持运行状态,直到程序再次关闭。我想这样做,但是是针对文件夹而不是程序,但是当我运行以下命令时,它会在加载文件夹内容时关闭

启动/等待explorer.exe“路径/名称/此处”

任何人都可以帮助我如何更改此设置以保持批处理文件运行,直到我关闭文件夹窗口?

batch-file cmd directory wait explorer
1个回答
0
投票

您可以通过在

PowerShell
内部创建一个函数来使用
batch-file
的一些帮助。是的,您只能使用
PowerShell
,但我不知道您当前的脚本有多少代码,因此不希望您在`PowerShell 中重写所有内容。

@echo off
set "fpath=d:\"

start "" "%fpath%"
call :runps

:try
for /F "delims=" %%i in ('powershell -file "%temp%\get_folders.ps1"') do (
    if /i "%%~i" == "%fp%" timeout /t 1 >null && goto :try
)

::Function
goto :EOF
:runps
echo $shell = New-Object -ComObject Shell.Application >"%temp%\get_folders.ps1"
echo $windows = $shell.Windows()>>"%temp%\get_folders.ps1"
echo foreach ($window in $windows) { >> "%temp%\get_folders.ps1"
echo    $fp = $window.Document.Folder.Self.Path >> "%temp%\get_folders.ps1"
echo    Write-Output "$fp" >>"%temp%\get_folders.ps1"
echo } >>"%temp%\get_folders.ps1"

脚本将打开您选择的位置,然后在您的

PowerShell
目录中创建
%temp%
脚本。 for 循环将每 1 秒调用
PowerShell
脚本。

批处理脚本中的

PowerShell script will loop through all open folders and simply print them. We catch the exact match using 
if`,如果我们发现它仍然处于活动状态,我们将继续循环,直到它不再找到处于活动状态的文件夹。

注意,请在

::Function
标签上方创建所有其他代码。

© www.soinside.com 2019 - 2024. All rights reserved.