Close folder's window with batch file

后端 未结 4 1927
孤独总比滥情好
孤独总比滥情好 2021-01-06 18:32

I have a folder on my Desktop named test. I want to create a batch file in order to close the window automatically when this folder is opened with Windows Explo

相关标签:
4条回答
  • 2021-01-06 19:12

    The error results when you've enabled display full path in title, under Folder View options.

    You may replace 'test' with full path, or disable full path display.

    By default, explorer runs as single process, and any windows that open are just a thread of the process.
    Normally, to close a program, you'd send a close message to the process. In this case, closing explorer.exe will close all explorer windows.

    To close individual windows, you'd open each window via it's own process. This can be done via registry setting or enabling under View->Options->View->Advanced Settings: "Launch ... separate process"

    a) Find PID (process ID) of window you wanna close.

    via taskmanager:
    1. In list of processes, click the arrow to the left of "Windows Explorer"
    2. Check the window name matches the window you wanna close
    3. Right click on "Windows Explorer", click "Go to Details"
    4. Record the pid

    via CMD:
    tasklist /V /FI "IMAGENAME eq explorer.exe"

    If each explorer window is open in it's own process, the above command would display the window title in the last column.
    Otherwise "N/A" would be displayed.

    The pid of all explorer windows would be the same. Explorer.exe processes have their own pid, and title "N/A"
    If 'separate process' has been enabled eg. via Folder View option, then each window can be closed via the process id & filter option of taskkill.

    To close, the desired window has to be activated first, otherwise closing with pid will close the last active window, or closing with window title filter will give error:

    INFO: No tasks running with the specified criteria.

    b) taskkill /pid <pid> will close the last active window.
    Repeating this command will the next window.

    or taskkill /im explorer.exe /fi "windowtitle eq <window name>"
    or taskkill /fi "IMAGENAME eq explorer.exe" /fi "windowtitle eq <window name>"

    < window name > is not case sensitive
    If full path in title bar has been enabled in Folder view, then include full path or wildcards.

    To close all explorer windows:
    taskkill /im explorer.exe

    Notes:

    1. To activate explorer window, issue same command to open the window, if window reusing is enabled.
    2. The pid of explorer window(ing) process is in the last row of the response table, in column "PID"; can be accessed via FOR loop.
    3. A vbs workaround to close window from @HelpingHand:
      https://superuser.com/questions/1263315/how-to-close-a-particular-opened-folder-using-cmd-or-batch-file
    4. A vbs workaround to activate window:
      http://superuser.com/questions/327676/application-to-automatically-switch-between-two-applications-in-windows

    Tested on Win 10

    0 讨论(0)
  • 2021-01-06 19:13

    This works in Windows 8.1 with explorer folder options set to single window but from the answer by Mofi and comments in Mofi's answer, then the solution depends on the Windows version.

    What works in Windows 8.1 doesn't work in Windows XP.

    TASKKILL /F /FI "WINDOWTITLE eq %userprofile%\desktop\test" /IM explorer.exe
    
    0 讨论(0)
  • 2021-01-06 19:17

    The solution I found here worked well on XP SP 3. All you need is the small tool NirCmd:

    nircmd.exe win close title "some window title"
    
    0 讨论(0)
  • 2021-01-06 19:26

    It is not possible to close the Windows Explorer window of a folder using command taskkill if in Windows registry under

    HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced
    

    the double word SeparateProcess has the value 0 respectively option Launch folder windows in a separate process on View tab of the Folder Options is not checked.

    And as foxidrive found out (read comments below) and I can also confirm, enabling this setting has on Windows XP SP3 no effect on number of explorer.exe running on opening folders even after a restart of Windows.

    Therefore it depends on version of Windows (2000, XP, Vista, 7, 8, 8.1) and the Folder Options

    • Display the full path in the title bar
    • Launch folder windows in a separate process

    if it is possible at all to use the command taskkill to close an Explorer folder window and which string to find in title bar, just the name of the folder or the full path of the folder.

    On Windows XP or with Launch folder windows in a separate process not being enabled the folder window is not opened as separate task respectively as separate process.

    Independent on how many folder windows are opened, the number of explorer.exe processes for each user is always just 1. Every folder window is in real just a window of always running Windows Explorer (desktop) opened in a separate thread. Windows Explorer just pretends that the folder window is a separate task on Windows task bar and on Applications tab of the Windows task manager. But only one explorer.exe is listed on Processes tab of Windows task manager even with multiple folder windows opened.

    This behavior of Windows Explorer can be better watched with free tool Process Explorer of Sysinternals by selecting explorer.exe and viewing in lower pane on the handles of this process containing among lots of other handles also the handles of the opened folder windows.

    A console application is required which finds a window by title, get the handle of this window and sends the WM_CLOSE event message to this folder window. See for example

    • How to close Windows Explorer from CMD?
    • How do I keep Windows Explorer from interfering with deleting a folder?
    • How to close Windows Explorer windows with opened folders from a certain drive?
    0 讨论(0)
提交回复
热议问题