473,405 Members | 2,310 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Deleting files and folders used by other processes on Windows

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.

Jul 17 '07 #1
1 1624
tk*****@gmail.com wrote:
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.
Jul 18 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Khaled Hussein | last post by:
Hello, I would like to know how can I restrict access for specific files and folders for specific running processes. For example: Lets say we have three running process P1, P2, and P3, I want to...
2
by: Glen | last post by:
As I understand it, when the first page of an application is accessed, all ASPX/ASCX/etc. files in the same folder are compiled using the JIT compiler. Is there a way to turn this feature off? ...
6
by: Martin Bischoff | last post by:
Hi, I'm creating temporary directories in my web app (e.g. ~/data/temp/temp123) to allow users to upload files. When I later delete these directories (from the code behind), the application...
5
by: George | last post by:
VB.net 2003 standard, XP windows home edition. Installed first application OK today. When I removed the application via Control Panel, there were no problems and the app folders were deleted. ...
4
by: Shiraz | last post by:
Hi I'm using Visual Studio Installer to make my installer, and have not as yet figured out a straightforward way to use it to set environmental variables. Amongst the various things I tried, I'm...
7
by: eSolTec, Inc. 501(c)(3) | last post by:
Thank you in advance for any and all assistance. I have an application that pulls files, folders and registry keys of installed programs. I'm wanting to with a context menu selection of "Delete...
3
by: Kimera.Kimera | last post by:
I'm trying to write a program in VB.net 2003 that basically deletes all files, folders, sub-folders and sub-sub folders (etc). The program is simply for deleting the Windows/Temp folder contents,...
20
by: mike | last post by:
I help manage a large web site, one that has over 600 html pages... It's a reference site for ham radio folks and as an example, one page indexes over 1.8 gb of on-line PDF documents. The site...
0
by: tkondal | last post by:
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.