3

Possible Duplicate:
DirectoryInfo.Delete(True) Doesn't Delete When Folder Structure is Open in Windows Explorer

Folks, I am writing a python testing harness and part of the project involves uninstalling an application and then reinstalling it every night. Part of the uninstall task involves deleting the data directory. During the day testers/developer will be logging into the computer and occasionally a command prompt and/or log file is left open. This causes my script to fail as windows will not let me delete a file if another process has a handle to it.

I would like to know if it is possible to get the pid of the process that is holding onto the file so I can kill the process with WMI or something like that? I would like to do this without logging people out.

If it is impossible then is there a way, from python, to force logout of all users to to get a system where my script can keep working without waiting for me to show up and kill process/log out users?

Any suggestions at all are greatly welcome. I am not an experienced windows programmer.

Community
  • 1
  • 1
user573225
  • 209
  • 1
  • 2
  • 6

1 Answers1

1

So it looks like sysinternals provides the solution.

handle.exe does the job.

Do a system call (subprocess module in python for example) and search the output for the directory you are trying to delete. Get the pid and then kill the process with another system call to psKill.exe.

I have not written the code yet but I went through the procedure on the command like and it works. Should be trivial to script this with any programming language doing system calls to the psTools.

user573225
  • 209
  • 1
  • 2
  • 6
  • I guess I should say 'a solution'. There might be a better way, but this way will work for my project. – user573225 Jan 12 '11 at 19:57
  • I guess this is better than rewrite the [OpenedFileFinder][1] tool using ctypes and py2exe. [1]: http://www.codeproject.com/KB/shell/OpenedFileFinder.aspx – cgohlke Jan 12 '11 at 20:56