Simple VBA like MessageBox in Python

Some days ago I found a very easy pythonic way to show a VBA-like MessageBox. It was created with only one line of cpython code.

The code is very very easy. Simply change the last integer parameter to change the style of the message box.

import ctypes

msgbox = ctypes.windll.user32.MessageBoxA
ret = msgbox(None, 'Press OK to end the demo.', 'Deviare Python Demo', 0)
print ret

Find selected files in Windows Explorer

I searched a long time for this and also Google could not give me an answer to the following question. How can I detect the selected files in a Windows Explorer window? I found following by trial and error…

import win32com.client
# look in the makepy output for IE for the 'CLSIDToClassMap'
# dictionary, and find the entry for 'ShellWindows'
clsid='{9BA05972-F6A8-11CF-A442-00A0C90A8F39}'
ShellWindows=win32com.client.Dispatch(clsid)

# a busy state can be detected:
# while ShellWindows[0].Busy == False:
# go in for-loop here

for i in range(ShellWindows.Count):
    print ShellWindows[i].LocationURL
    for j in range(ShellWindows[i].Document.SelectedItems().Count):
        print '  ', ShellWindows[i].Document.SelectedItems().Item(j).Path

# Be careful: Internet Explorer uses also the same CLSID. You should implement a detection!

PathWatcher.py keeps an eye on any changes in any directory

This little script (based on Tim Goldens work) runs only on windows and watches at low level into a givven directory. If any changes occur, it will immidiatly show you what has changed. I use it regularly to watch into a directory (works also with UNC pathes and samba) and follow, which files were modified. It watches into the complete directory tree with sub directories. Very simple, use it like this:

python.exe c:\temp\PathWatcher.py \\SRVXY\released_docs
import os, sys

import win32file
import win32con

ACTIONS = {
  1 : "Created",
  2 : "Deleted",
  3 : "Updated",
  4 : "Renamed from something",
  5 : "Renamed to something"
}

FILE_LIST_DIRECTORY = 0x0001

path_to_watch = sys.argv[1]
print 'Watching changes in', path_to_watch
hDir = win32file.CreateFile (
  path_to_watch,
  FILE_LIST_DIRECTORY,
  win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE,
  None,
  win32con.OPEN_EXISTING,
  win32con.FILE_FLAG_BACKUP_SEMANTICS,
  None
)
while 1:

  results = win32file.ReadDirectoryChangesW (
    hDir,
    1024,
    True,
    win32con.FILE_NOTIFY_CHANGE_FILE_NAME |
     win32con.FILE_NOTIFY_CHANGE_DIR_NAME |
     win32con.FILE_NOTIFY_CHANGE_ATTRIBUTES |
     win32con.FILE_NOTIFY_CHANGE_SIZE |
     win32con.FILE_NOTIFY_CHANGE_LAST_WRITE |
     win32con.FILE_NOTIFY_CHANGE_SECURITY,
    None,
    None
  )
  for action, file in results:
    full_filename = os.path.join (path_to_watch, file)
    print full_filename, ACTIONS.get (action, "Unknown")

Show the path of all Explorer Windows

This piece of Python code shows the current path of all opened Explorer Windows

from win32com.client.gencache import EnsureDispatch

for w in EnsureDispatch("Shell.Application").Windows():
    print w.LocationName + "=" + w.LocationURL

I wanted to write a code, which can make use of a handler to a Windows Explorer Window, so that I can show a small Tooltip or anything else dynamically belonging to a selected Path of the User. But I think this is the better way. Hope someone alse can use this…

Also the output shows a path of Internet Explorer and Microsoft Outlook:

>>> 
outlook:Posteingang outlook:Posteingang
Tools file:///C:/Users/me/Tools
Kurz notiert http://www.goermezer.de/content/blogsection/2/633
>>>

May be we would need a detection here for Outlook or Internet Explorer.

A file open dialog with win32ui

import win32ui
o = win32ui.CreateFileDialog( 1, ".txt", "default.txt", 0, "Text Files (*.txt)|*.txt|All Files (*.*)|*.*|")
o.DoModal()
print o.GetPathName()