6

When I have to restart explorer.exe, usually I have many folders open, which get closed in the process. So is there a way to reopen these folders automatically after restarting explorer?

goldnick7
  • 440
  • 1
  • 5
  • 14

3 Answers3

7

This batch script:

  1. Makes a list of currently opened windows → saves it to a txt file
  2. Restarts windows explorer.exe
  3. Re-opens folders from the txt list → deletes txt file

@echo off
setlocal enabledelayedexpansion

powershell @^(^(New-Object -com shell.application^).Windows^(^)^).Document.Folder.Self.Path >> prevfolderpaths.txt

taskkill /im explorer.exe /f

start explorer.exe

FOR /F "tokens=*" %%f IN (prevfolderpaths.txt) DO (

set "var=%%f" set "firstletters=!var:~0,2!"

IF "!firstletters!" == "::" ( start /min shell:%%~f ) ELSE ( start /min "" "%%~f" )

)

del "prevfolderpaths.txt"


Once you save the code as restart_explorer.bat .. next you should

  1. Right Click → Sent to → Desktop (create shortcut)
  2. Right Click shortcut → Run: Minimized → and add your shortcut
goldnick7
  • 440
  • 1
  • 5
  • 14
2

so this script justs opens the windows minimized

I can't comment yet but would like to improve goldnick7's answer. You can keep your folders maximized by deleting second /min:

IF "!firstletters!" == "::" ( start /min shell:%%~f ) ELSE ( start "" "%%~f" )"

I left first /min there to remember this flag exists.


Also, in multi-user environment you can use this command to affect only your processes:

taskkill /f /fi "UserName eq %UserName%" /im explorer.exe

It is important that /fi goes before /im, otherwise it won't work.

TTxD
  • 21
1
powershell $open_folders = @((New-Object -com shell.application).Windows()).Document.Folder.Self.Path; Stop-Process -Name explorer -Force;  foreach ($element in $open_folders){Invoke-Item $($element)}