Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 267
  • Last Modified:

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
0
Jason05
Asked:
Jason05
  • 6
  • 3
  • 2
1 Solution
 
nffvrxqgrcfqvvcCommented:
Find the window of the folder, then use Postmessage API
0
 
nffvrxqgrcfqvvcCommented:
'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
0
 
nffvrxqgrcfqvvcCommented:
Just to let you know I tested it on windows xp pro..
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
nffvrxqgrcfqvvcCommented:
Remember when asking this type of question to let us know the operating system
0
 
Shiju SasidharanAssoc Project ManagerCommented:
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
0
 
Jason05Author Commented:
Hello,

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

Jason
0
 
Shiju SasidharanAssoc Project ManagerCommented:
Hi Jason05
  Have u tried my code ?

;-)
Shiju
0
 
Jason05Author Commented:
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.
0
 
nffvrxqgrcfqvvcCommented:
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.

0
 
nffvrxqgrcfqvvcCommented:
Jason Im sorry I forgot to add the WM_CLOSE ...Here this will work sorry about that..

Option Explicit

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)

Const WM_CLOSE = &H10

Private Sub Command1_Click()

Dim I As Integer
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
0
 
Jason05Author Commented:
Very nice, thanks egl.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now