Link to home
Start Free TrialLog in
Avatar of Jason05
Jason05

asked on

Close All Folders

Hi,

Does anyone know any code to close all the folders that are open on my machine. So if I had C:/Downloads/ or C:/Applications/ or whatever open, it would close them all?

Cheers
Avatar of nffvrxqgrcfqvvc
nffvrxqgrcfqvvc

Find the window of the folder, then use Postmessage API
'It would look something like this...


Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Sub Command1_Click()
For i = 1 To 100 ' I doubt you will have more than 100 folders open so this will work
Dim cabinetwclass As Long
cabinetwclass = FindWindow("cabinetwclass", vbNullString)
Call PostMessage(cabinetwclass, WM_CLOSE, 0&, 0&)
Sleep 100
DoEvents
Next i

End Sub
Just to let you know I tested it on windows xp pro..
Remember when asking this type of question to let us know the operating system
hi
simple way is to kill explorer
try this
'====================================
Option Explicit

Private Const PROCESS_TERMINATE = &H1
Private Const TH32CS_SNAPPROCESS As Long = 2&
Private Const MAX_PATH As Integer = 260
Private Type PROCESSENTRY32
    dwSize As Long
    cntUsage As Long
    th32ProcessID As Long
    th32DefaultHeapID As Long
    th32ModuleID As Long
    cntThreads As Long
    th32ParentProcessID As Long
    pcPriClassBase As Long
    dwflags As Long
    szExeFile As String * MAX_PATH
End Type

Private Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
Private Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Sub CloseHandle Lib "kernel32" (ByVal hPass As Long)
Private Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long

Private Sub Command1_Click()
   GetExplorerAndKill
End Sub

Private Function GetExplorerAndKill()
    Dim hToolhelpSnapshot As Long
    Dim tProcess As PROCESSENTRY32
    Dim r As Long
    Dim m_lProcessID As Long
    Dim hProcess As Long
    hToolhelpSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
    If hToolhelpSnapshot = 0 Then Exit Function
    With tProcess
        .dwSize = Len(tProcess)
        r = ProcessFirst(hToolhelpSnapshot, tProcess)
        Do While r
            If InStr(1, .szExeFile, vbNullChar) <> 0 Then
               If LCase(Left(.szExeFile, InStr(1, .szExeFile, vbNullChar) - 1)) = "explorer.exe" Then
                    m_lProcessID = .th32ProcessID
                    'Killing explorer
                    hProcess = OpenProcess(PROCESS_TERMINATE, False, m_lProcessID)
                    TerminateProcess hProcess, -1&
                    CloseHandle hProcess
                    Exit Do
               End If
            End If
            r = ProcessNext(hToolhelpSnapshot, tProcess)
        Loop
    End With
    Call CloseHandle(hToolhelpSnapshot)
End Function
'==============================================

;-)
Shiju
Avatar of Jason05

ASKER

Hello,

I am running XP Home Edition. I tried the code egl1044 but couldn't get it to work.

Jason
Hi Jason05
  Have u tried my code ?

;-)
Shiju
Avatar of Jason05

ASKER

I tried the code Shiju, i couldn't get it to work, but it also seems such a bodge closing the whole of explorer as all I wanted to do is to terminate any open folders, such as my "My Documents" folder.
Jason it works

Start a new project Standard .Exe

Add 1 command button

Double click the form and remove everything so it is blank, the copy and paste my code in the form declerations section.

ASKER CERTIFIED SOLUTION
Avatar of nffvrxqgrcfqvvc
nffvrxqgrcfqvvc

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jason05

ASKER

Very nice, thanks egl.