【问题标题】:How to check if explorer window is open by title in batch?如何检查资源管理器窗口是否按标题批量打开?
【发布时间】:2021-09-30 23:51:17
【问题描述】:

如何批量检查资源管理器窗口是否打开?

我需要在已选择文件的情况下截取目录的屏幕截图,但必须在窗口打开之后。窗口标题始终相同。

例子:

REM Opening folder with a file selected
START /W EXPLORER /SELECT,\\10.10.10.10\C$\ThisFolder\FileToHighlight.txt
REM Unreliably waiting for window to open
TIMEOUT /T 3 /NOBREAK >NUL
REM Taking screenshot of window with a third-party app
START .\Bin\screenshot-cmd.exe -wt "ThisFolder" -o .\Screenshots\%var%.png

【问题讨论】:

  • 看看start /waitss64.com/nt/start.html
  • 看起来它一直等到应用程序终止?这对我没有好处,因为我只有在截屏后才关闭窗口
  • 你能发布你的代码吗?

标签: batch-file window wait explorer


【解决方案1】:

如果没有其他窗口与打开的文件夹标题相同,则可以使用tasklist

@echo off
rem DON'T ADD A TRAILING \ IN THE PATH!
set folder=d:\path\foldername
explorer "%folder%"
:wait
    timeout 1 >nul
    for %%z in ("%folder%") do (
        tasklist /fi "windowtitle eq %%~nxz" /fi "imagename eq explorer.exe" ^
        | find "explorer.exe" >nul
        if errorlevel 1 goto wait
    )
pause

【讨论】:

  • 感谢您的回复。一个规定是我需要为屏幕截图选择一个文件,所以我使用 EXPLORER /SELECT,%folder% %folder% 类似于 "D:\Folder\FileWithInFolder.txt" 这会不会导致你的循环不为我工作?它不会暂停。
  • 当然,因为代码提取的是 FOLDER 名称,而不是 FILE 名称。见Get parent directory name for a particular file using DOS Batch scripting
【解决方案2】:

很容易检查if an explorer window is openedScriptable Shell Objects,这是100% 可靠,不像使用tasklist 的解决方案有时可能无法工作,因为当前文件夹路径在任务信息。这是一个PowerShell脚本,用于检查文件夹是否已打开,如果未打开则休眠1秒

$path = '\\10.10.10.10\C$\ThisFolder\FileToHighlight.txt'
$shell = New-Object -ComObject Shell.Application
do {
    sleep 1
    $window = $shell.Windows() | where { $_.LocationURL -ieq ([uri]$path).AbsoluteUri }
} while ($window -eq $null) # wait until the folder is opened

# Explorer window has opened, take the screenshot
start .\Bin\screenshot-cmd.exe -wt "ThisFolder" -o ".\Screenshots\$var.png"

或者,您可以将$_.LocationURL -ieq ([uri]$path).AbsoluteUri 更改为

$_.Document.Folder.Self.Path -ieq $path

如果您愿意,也可以使用 VBS 或 Jscript。检查我上面的链接以查看 Jscript 中的等效版本,甚至是混合批处理 Jscript 解决方案

【讨论】:

    猜你喜欢
    • 2015-12-20
    • 1970-01-01
    • 2010-09-24
    • 1970-01-01
    • 1970-01-01
    • 2018-08-24
    • 2010-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多