Check if Directory is currently open - without additional modules!?

Hello pyRevit Friends :slight_smile:

I want to check if the folder “C:\example” is currently open in the windows explorer.
I searched the web and came across modules that would do this job, like win32gui, pywinauto, pyautogui, etc.

I never installed a module before and it seems that i can´t without admin rights. Also I don´t know how i could share the script with my team if I use additional modules.

So is there a method I can use without using an additional module, or is there a way I can install it?

Appreciate any advice! Kind regards :slight_smile:

there’s an interesting suggestion on stackoverflow to rename the directory and try for a permission error:

import os

def try_to_rename(src, dst):
    while True:
        try:
            os.rename(src, dst)
            break
        except PermissionError:
            input(f"Unable to rename {src} to {dst}. If one or both "
                   "files/folders are open, please close them. Press Enter to "
                   "continue.")
1 Like

Hello @HikoKibar and thanks for your reply!

I tried that with the following results.

I can rename a folder succesfull even if it is open:

import os

src="C:\\Print15"
dst="C:\\Print20"

try:
	os.rename(src, dst)
except:
	print "Cant rename folder"

For files, i can rename them if they are closed but not if they are open:

import os

src="C:\\Print\\xxxx.pdf"
dst="C:\\Print\\aaaa.pdf"

try:
	os.rename(src, dst)
except:
	print "Cant rename file"

It seems this method would only work for files, but not for folders!

edit:

can´t get the full code to run:

import os

src="C:\\Users\\...\\Desktop\\test60"
dst="C:\\Users\\...\\Desktop\\test80"

def try_to_rename(src, dst):
    while True:
        try:
            os.rename(src, dst)
            break
        except PermissionError:
            input("Unable to rename src to dst. If one or both files/folders are open, please close them. Press Enter to continue.")

print try_to_rename(src, dst)
**IronPython Traceback:**
Traceback (most recent call last):
File "C:\Users\...\OneDrive - AFRY\Desktop\Share\AFRY.extension\AFRY.tab\AFRY.panel\Develop.pushbutton\Develop_script.py", line 14, in <module>
File "C:\Users\...\OneDrive - AFRY\Desktop\Share\AFRY.extension\AFRY.tab\AFRY.panel\Develop.pushbutton\Develop_script.py", line 9, in try_to_rename
NameError: global name 'PermissionError' is not defined

**Script Executor Traceback:**
IronPython.Runtime.UnboundNameException: global name 'PermissionError' is not defined

A workaround would be to try to close the explorer window “C:\example”, but this is also not possible without additional modules.

Anyway, i managed to install python, pip and the pywinauto module, so I should get this working. Just don´t know how i can later share my code with my team members.

Hi @Gerhard.P
What’s your ultimate goal? If it is to open the explorer window if not already opened, you can use

import os

os.startfile(path, "open")

source

If you really need to do what you asked and use pyautoguim follow this guide to create a redistributable library extension

1 Like

Hello @sanzoghenzo,

“startfile” will open a new explorer window, no matter if the folder is already open or not.

I have a tool that prints pdfs. I want to open the print directory folder if it is not already open. Current status is that every print will open the folder over and over again.

Hi @Gerhard.P
That’s strange, on my system it’s working as advertised. I’m on WIndows 10; tested on python 3 and pyrevits 2.7.10 interpreter (this is the version that lets me spawn the rpw interactive console, so I don’t have to create a script just for these tests)

2 Likes

I´m now on another machine than as i opened this thread so i tried it again.

You are right, os.startfile(“C:\Print”) really does not open the folder again, it focuses on the open window. Thats what i wanted :slight_smile: I found other users on the web with the same problem, i don´t know whats the reason…

So thanks for making me try again!

Additional info:

pyrevit.coreutils. open_folder_in_explorer ("C:\Print")

will open a folder over and over again.

1 Like