Deleting files and folders used by other processes on Windows

T

tkondal

Hi,

I have been looking into making my file cleaning script more
intelligent. The goal of the script is to delete everything on a
drive except for a couple of folders which are skipped by the script.
Recently, I noticed that some files where not being deleted because a
process was using them. Is there a recipe (needs to run on Windows XP/
2003) for doing the following (more specifically, I need a way to
determine which process is holding a lock on my file):


file=GetFilename();
bLock = IsThereAFileLockOnFile(file)
If (bLock)
{
process = GetLockingProcess(file);
TerminateProcess(process);
}
DeleteFile(file);



Thanks.
 
B

brad

Hi,

I have been looking into making my file cleaning script more
intelligent. The goal of the script is to delete everything on a
drive except for a couple of folders which are skipped by the script.
Recently, I noticed that some files where not being deleted because a
process was using them.

Try this:

try:
# Make the file's attributes normal so file can be
deleted.
win32api.SetFileAttributes(os.path.join(root, f),
win32con.FILE_ATTRIBUTE_NORMAL)
# HKLM/SYSTEM/CurrentControlSet/Control/Session
Manager/PendingFileRenameOperations
win32api.MoveFileEx(os.path.join(root, f), None,
win32con.MOVEFILE_DELAY_UNTIL_REBOOT)
except Exception, e:
print e

Upon reboot the file will be gone... careful though, this canl delete
any Windows system file too.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top