0

I just need to refresh a folder. A hypothetical ideal example would be:

from aModule import refreshdir # fake
refreshdir("C:\path\to\directory")

Context: I am using Autodesk Desktop Connector, a service that sync data on the cloud with local folders. To avoid expending resources, this tool just checks for new updates when the user opens the file or refresh the directory (so manually). However, in order to automate some operations, I need to refresh the directory with Python. There is no API for this tool.

Thanks in advance! =)

Edit: New files can be added in the cloud. That's why it is important to refresh the folder. Example: Before refreshing: enter image description here After refreshing: enter image description here os.listdir cannot catch those highlited files before refreshing.

  • In addition to the answer, here's a great list of all possibilities: https://stackoverflow.com/questions/3207219/how-do-i-list-all-files-of-a-directory – Cow Aug 04 '21 at 11:56

2 Answers2

2

Refreshing a directory is not an operating system operation, but a function of the filesystem browser / explorer. A refresh is essentially just reading in the directory contents anew.

Most likely that Adobe tool is hooking into the filesystem functions that do this enumeration of a directory's contents. If this is the case, then the task should be as simple as

import os
os.listdir("C:/path/to/directory")

Keep in mind that backslashes (\) in standard string literals start an escape sequence, i.e. if you wanted to put an actual backslash there, you'd have to write "\\". However Windows will happily use forward slashes as directory separator as well, so you can just use that :-)

datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • Thank you for the answer datenwolf. That was very helpful. Your code does refresh the contents that were visible last time you entered that folder. However, if new files are added, these are not listed =S – Juan Gonzalez Aug 06 '21 at 15:35
  • @JuanGonzalez well, without doing some reverse engineering on the Adobe code it's impossible to tell, what methods exactly it hooks into. I'm a little bit surprised that the Adobe tool doesn't simply establish a file system change notification (as described in https://learn.microsoft.com/en-us/windows/win32/fileio/obtaining-directory-change-notifications) which would render your quest unnecessary… – datenwolf Aug 06 '21 at 19:09
0

To solve this problem I created a script in Python using the pywinauto library to do a manually task that clicks on the file and then clicks on the Sync option. In this case you'll need to know the name of the files you want to sync. The code was made to AutoCAD Plant 3D project, you'll need to change the path to your files.

from pywinauto import Application

raiz = "C:\\Users\\YOUR_USERNAME\\ACCDocs\\ORGANIZATION_NAME\\PROJECT_NAME\\Project Files\\PLANT3D_PROJ_NAME\\Plant 3D Models"

Application().start('explorer.exe ' + raiz, timeout=10)

explorer = Application(backend='uia').connect(path='explorer.exe', title="Plant 3D Models")
#Plant3DModels is a variable automatically created with the title of the windows opened
explorer.Plant3DModels.set_focus()

# 'Infra-Geral.dwg' is the name of the file that I will Sync 
file = explorer.Plant3DModels.ItemsView.get_item('Infra-Geral.dwg')
file.right_click_input()
explorer.ContextMenu.Sync.invoke()
Hugo Mata
  • 315
  • 2
  • 11
  • 1
    If you have a new question, please ask it by clicking the [Ask Question](https://stackoverflow.com/questions/ask) button. Include a link to this question if it helps provide context. - [From Review](/review/low-quality-posts/30010972) – Ian Campbell Oct 06 '21 at 20:26
  • I only suggested a python code that I used in my case to Juan Gonzalez. – Hugo Mata Oct 06 '21 at 22:00
  • 1
    @HugoMata did your code snippet actually work for you? This is unclear. If it worked, please update your answer accordingly. – Joël Oct 07 '21 at 12:36
  • 1
    Yes it worked. I editted the answer accordingly. – Hugo Mata Oct 07 '21 at 13:26