?
Solved

Close All Folders

Posted on 2005-04-01
11
Medium Priority
?
254 Views
Last Modified: 2006-11-18
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
Comment
Question by:Jason05
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 2
11 Comments
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 13685466
Find the window of the folder, then use Postmessage API
0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 13685494
'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
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 13685504
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!

 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 13685511
Remember when asking this type of question to let us know the operating system
0
 
LVL 14

Expert Comment

by:Shiju Sasidharan
ID: 13686489
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
 

Author Comment

by:Jason05
ID: 13686887
Hello,

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

Jason
0
 
LVL 14

Expert Comment

by:Shiju Sasidharan
ID: 13687032
Hi Jason05
  Have u tried my code ?

;-)
Shiju
0
 

Author Comment

by:Jason05
ID: 13687073
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
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 13687971
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
 
LVL 29

Accepted Solution

by:
nffvrxqgrcfqvvc earned 2000 total points
ID: 13688001
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
 

Author Comment

by:Jason05
ID: 13688073
Very nice, thanks egl.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month10 days, 18 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question