Sign in to follow this  
Followers 0
Psychotic

New User: Closing open explorer windows

11 posts in this topic

As the topic says, I'm a new user in need of help.

I wrote some code that will basically open up a drive and then close it. i.e. open C: and then close it.

I've tried with WinClose("C:") since that is the name of the drive but it refuses to close. I then tried WinKill("ACTIVE") IIRC but that closes the desktop.

The overall aim of the program will be to access three drives that get disconnected every hour or so without the need to manually go to My Computer and then accessing the drives and also to check whether a specific process is running, but I'll deal with that later.

; AutoIt v3.0
;

; minimize all windows
WinMinimizeAll ( )

; PATH: X:\
Run('explorer.exe /n /m,"X:\"')
WinClose("X:\")
Exit

; PATH: Y:\
Run('explorer.exe /n /m,"Y:\"')
WinClose("Y:\")
Exit

; PATH: Z:\
Run('explorer.exe /n /m,"Z:\"')
WinClose("Z:\")
Exit
; finished

Thanks

p.s. I'm running AutoIt v3.3.6.1 on Windows 7 Ultimate x64 but the script will be run on Vista Business x86 once it's done.

p.s.2 The drives have labels (though I've tried using the labels as well) Backup,Design,Secretary in that order.

Share this post


Link to post
Share on other sites



Opt("WinTitleMatchMode", 3)

WinMinimizeAll ( )

Local $aPath[3] = ["X:\", "Y:\", "Z:\"]

For $i = 0 To UBound($aPath) - 1
 Run('explorer.exe /n, ' & $aPath[$i])
 Sleep(2000)
 WinClose($aPath[$i])
Next


Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Share this post


Link to post
Share on other sites

Opt("WinTitleMatchMode", 3)

WinMinimizeAll ( )

Local $aPath[3] = ["X:\", "Y:\", "Z:\"]

For $i = 0 To UBound($aPath) - 1
 Run('explorer.exe /n, ' & $aPath[$i])
 Sleep(2000)
 WinClose($aPath[$i])
Next

I see what you did there. You basically added the three drives in an array and used the For loop to go through the array and execute the commands for each. I tried it like that and again the windows come up but they don't close. I'll try it on another pc to see if maybe there is something wrong with this one (I've been meaning to format it for a while but can't find the time to backup everything) and let you know.

Share this post


Link to post
Share on other sites

It's probably just a matter of correctly identifying the windows.

You are attempting to use just the drive letter as the title of the window to close. What is the actual title of these windows that aren't closing?

:mellow:


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

For me it works very well. The important line is script for me was Opt("WinTitleMatchMode", 3), because testing in Scite and drive c:\ so Scite was closed instead of the explorer window. The for loop was just easier for testing.

I don't know why this does not work for you. But you could try WinClose("").


Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Share this post


Link to post
Share on other sites

Tried it on another pc running XP SP3 and still no go.

The title of the window is just the drive letter as far as I know. If you open My Computer and you enter you hard drive then the title should be the drive letter, right?

Tried WinClose("") but again it closed my desktop. I tried using the name as shown on the windows (i.e. Computer > System (C:)) but again no dice. Something that I've noticed is that if the $aPath doesn't exist, it will open the folder of the user that is currently logged on the PC (using Win 7 as mentioned before).

Shouldn't the title that is used to open the window also close the window?

p.s. funkey could you please attach the exe file so that I can try your compiled version? Maybe there is something wrong with the way I compile the program.

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Note that window titles are case sensitive so "c:\" <> "C:\"

:mellow:

Edit: Another way to look at it:

For $i = 0 To UBound($aPath) - 1
    Run('explorer.exe /n, ' & $aPath[$i])
    $iTimer = TimerInit()
    Do
        Sleep(100)
        $hExplr = WinGetHandle("[CLASS:CabinetWClass; REGEXPTITLE:" & "(?i)^" & StringLeft($aPath[$i], 1) & ":\\]", "")
        If $hExplr Then
            WinClose($hExplr)
            ExitLoop
        EndIf
    Until TimerDiff($iTimer) >= 5000
Next

:P

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Explorer windows can be either class CabinetWClass or ExploreWClass (the latter is what you get when you right-click a folder and 'Explore' it [though its possible to get the Explorer view through other means too]). Plus, root drive titles most always have the volume name in the title and no backslash, in my experience, in this format: "MyDrive (C:)" or "Local Disk (C:)". I tested this on Win XP and Windows 7. :mellow:

So lets see, using PsaltyDS's method, we could do something like this:

$hExplr = WinGetHandle("[REGEXPCLASS:(Explore|Cabinet)WClass; REGEXPTITLE:(?i)\("&StringLeft($aPath[$i],1)&"\:\)]")

Of course, if you get different results, you'll have to adjust things until you find the easiest method that works for you. (I still can't reproduce what O/S funkey is seeing just a drive letter with a backslash).

*edit: removed $sDrive="C:\" (copy and paste mistake)

Edited by Ascend4nt

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

$hExplr = WinGetHandle("[REGEXPCLASS:(Explore|Cabinet)WClass; REGEXPTITLE:(?i)\("&StringLeft($aPath[$i],1)&"\:\)]")
RegExp geek! :mellow:

:P

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

hahaha PsaltyDS - I thought that title went to you (RegEx geek)! :P

I do find that it is fun messing with PCRE's nowadays though. Back when I first started, I was just like :mellow: wth is that, chinese?!

Oh - for some reason I forgot to remove that '$sDrive=' line when I did a copy&pasted. You can ignore that line, Psychotic.

Share this post


Link to post
Share on other sites

Just had some time to try it out and Ascend4nt's code seems to do the trick.

Thanks for all the help.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0