Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Close Windows Explorer windows

Status
Not open for further replies.

NZTechworks

Programmer
Oct 29, 2012
3
NZ
I run a small network with Win XP and Win7 workstations and mappings area a pain as they dont always get invoked and you have to open an explorer window and click on the mapped drive with the red X before ite will "refresh".

Yes I know where must be a "network" fix for this, but I gave up on that years ago!

So my solution is to run a script at startup to:

1) disconnect all mappings
2) reconnect mappings
3) open a windows explorer window for each mapping
4) close each explorer window

I have got steps 1-3 working, but cant find any way to close one or more windows explorer windows.

Here is my script:

------------------------------------------
On Error Resume Next
Set objNetwork = CreateObject("Wscript.Network")
Set colDrives = objNetwork.EnumNetworkDrives
Set objShell = CreateObject("Wscript.Shell")
set shell = WScript.CreateObject("WScript.Shell")
'strPath = Wscript.Arguments(0)

intMapping = _
Msgbox("Refresh Work Mappings?", _
vbYesNo, "Mapping Refresh Script")

If intMapping = vbYes Then
objNetwork.RemoveNetworkDrive "P:"
objNetwork.MapNetworkDrive "P:", "\\TW05\_Install"

objNetwork.RemoveNetworkDrive "S:"
objNetwork.MapNetworkDrive "S:", "\\TW05\_SharedData"

objNetwork.RemoveNetworkDrive "W:"
objNetwork.MapNetworkDrive "W:", "\\TW05\_Backup_Data"

objNetwork.RemoveNetworkDrive "Y:"
objNetwork.MapNetworkDrive "Y:", "\\TW05\_Archive"

objNetwork.RemoveNetworkDrive "J:"
objNetwork.MapNetworkDrive "j:", "\\Tw06\_SharedData"

objNetwork.RemoveNetworkDrive "K:"
objNetwork.MapNetworkDrive "K:", "\\TW05\_SD_Tools"

'now open mapped drives

objShell.Run "explorer.exe /e, P:\"
objShell.Run "explorer.exe /e, S:\"
objShell.Run "explorer.exe /e, W:\"
objShell.Run "explorer.exe /e, Y:\"
objShell.Run "explorer.exe /e, J:\"
objShell.Run "explorer.exe /e, K:\"
End If



intClose = _
Msgbox("Close explorer sessions?", _
vbYesNo, "Close Explorer Sessions")

If intClose = vbYes Then

'Shell.Run "cmd /c Taskkill /F /IM explorer.exe"
'Nope .. this kills taskbar as wel!!!!!!

'objShell.quit "explorer.exe"
'nope dosent work

' successP = shell.appactivate("P:\")
' if successP then shell.sendkeys "%{F4}"
' successS = shell.appactivate("S:\")
' if successS then shell.sendkeys "%{F4}"
' successW = shell.appactivate("W:\")
' if successW then
' shell.sendkeys "%{F4}"
' end if
' successY = shell.appactivate("Y:\")
' if successY then
' shell.sendkeys "%{F4}"
' end if
' successJ = shell.appactivate("J:\")
' if successJ then
' shell.sendkeys "%{F4}"
' end if
' successK = shell.appactivate("K:\")
' if successK then
' shell.sendkeys "%{F4}"
' end if
'NOPE, only clsoes last window

'close = CloseExplorerWindow("\\tw05\_Backup_Data")
'NOPE just returns "False" and doent close anything
'msgbox(close)

end if


Function CloseExplorerWindow(sFolderPath)
Dim bTest

bTest = false
with createobject("shell.application")
for each wndw in .windows
if typename(wndw.document) = "IShellFolderViewDual2" then
if lcase(wndw.document.folder.self.path) = lcase(sFolderPath) then
on error resume next
wndw.quit
bTest = err.number = 0
on error goto 0
end if
end if
next
end with ' shell.application
CloseExplorerWindow = Cstr(bTest)

end function
------------------------------------

you will see I have remmed out a few ways I've tried that havnt worked

I suspect the mapping issue is related to a delay in the workstations poling each other when they start up, and also that one of them is usually hybernated rather than turned off when disconnected.

Any suggestions much appreciated

Cheers

Grant






 
Each explorer window has a title. "Focus" on the window by using .AppActivate(<title>) followed by .SendKeys() to close it.

Code:
objShell.AppActivate "J:\"
objShell.SendKeys "(%{F4})" 'ALT + F4

-Geates

NOTE: There are more elegant ways to close a window.
 
Thanks Geates, but that doesnt work as expected.

To try and isolate things I've now split the process into two scripts so the first creating the mappings and the second one closing the explorer windows as follows:

The problem with objShell.AppActivate "P:\" is you cant set it to a variable so you can test if there is this window open.

Here is what does partially work:

------------------------------
Set objShell = CreateObject("Wscript.Shell")
set shell = WScript.CreateObject("WScript.Shell")


success = shell.appactivate("w:\")
if success then shell.sendkeys "%{F4}"

success = shell.appactivate("Y:\")
if success then shell.sendkeys "%{F4}"

success = shell.appactivate("K:\")
if success then shell.sendkeys "%{F4}"
------------------------------------
but only for the first window, after that it gets screwed up and closed other objects and not the windows explorer session I want. Sometimes it prompts to close Windows completly, so obviously the sendkey command is not such a good way to close an app as its totally dependant on what has the focus when invoked.

So what is the "more elegent" way to cloe these instances of windows explorer?


 
Yes? I would bet there is but I'm not familiar with it. Unfortunately, Explorer windows seem not to be registered as a process so there is no elegant way (that I know of) to get the explorer windows.

However, to get running processes:
Code:
set colProcesses = GetObject("winmgmts:\\" & strComputerName & "\root\cimv2").ExecQuery("Select * from Win32_Process")
for each objProcess in colProcesses
   ...
   [URL unfurl="true"]http://msdn.microsoft.com/en-us/library/windows/desktop/aa394372(v=vs.85).aspx[/URL]
next

you could try putting a delay after .sendkeys to "let the os catch-up"
Code:
if (shell.appactivate("w:\")) then
   shell.sendkeys "%{F4}" 
   [red]wscript.sleep 500[/red]
end if

if (shell.appactivate("Y:\")) then
   shell.sendkeys "%{F4}" 
   [red]wscript.sleep 500[/red]
end if

if (shell.appactivate("K:\")) then
   shell.sendkeys "%{F4}" 
   [red]wscript.sleep 500[/red]
end if

-Geates
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top