473,725 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How can I unlink/delete an open file in Windows?

Thanks for any advice.

~ CP

Oct 18 '08 #1
18 14633
"Coffee Pot" <no****@nospam. comwrote in message
Thanks for any advice.
I don't know about Windows specifically but generally you need to close the
file or the attempted deletion will fail.

system("del C:\\path\\to\\f ile");

will delete.

Oct 18 '08 #2
"Malcolm McLean" <re*******@btin ternet.comwrite s:
"Coffee Pot" <no****@nospam. comwrote in message
>Thanks for any advice.
I don't know about Windows specifically but generally you need to
close the file or the attempted deletion will fail.

system("del C:\\path\\to\\f ile");

will delete.
To keep this semi-on-topic, standard C provides the remove() function.
However the results are "implementa tion defined" if the file is open, so
to be portable you would need to close it first. I also don't know what
Windows implementations usually do but I think they might fail if the
file is open. See the documentation for your C compiler / library.

As a side note, the "generally" may be an overstatement; on Unix it is
entirely legal to remove an open file. The file will be "unlinked" (so
it no longer appears in the filesystem, unless there are other links to
it) but the data will remain on the disk, accessible to whoever has it
open, until they all close it, at which point the data is actually
deleted. This is frequently a useful feature. I do not know if Windows
provides anything equivalent but my guess is no; it's my impression that
the Windows philosophy is to forbid you from messing with an open file.
Oct 18 '08 #3
Nate Eldredge <na**@vulcan.la nwrites:
"Malcolm McLean" <re*******@btin ternet.comwrite s:
>"Coffee Pot" <no****@nospam. comwrote in message
>>Thanks for any advice.
I don't know about Windows specifically but
[...]
>
To keep this semi-on-topic, standard C provides the remove() function.
However the results are "implementa tion defined" if the file is open, so
to be portable you would need to close it first. I also don't know what
Windows implementations usually do but
[...]

The above answers should be a strong clue that this is not the best
place for your question. (I don't know

Standard C provides the remove() function; how it behaves if the file
is open depends on the system.

If you want Windows-specific information (which appears to be what you
need), try asking in comp.os.ms-windows.program mer.win32. Wherever
you post, I suggest including the question in the body of your
article.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 19 '08 #4
On 18 Oct 2008 at 22:12, Malcolm McLean wrote:
"Coffee Pot" <no****@nospam. comwrote in message
>Thanks for any advice.
I don't know about Windows specifically but generally you need to
close the file or the attempted deletion will fail.
I wonder what the rational for this behavior was - it does seem more
like a bug than a feature.

It's been a while since I used Windows, but I think the only reliable
way is to arrange for the file to be deleted on the next reboot. I think
that's what installers do to get rid of shared libraries that might
still be in use, etc.

I guess you could do this by appending a line

DEL c:\path\to\file

to the AUTOEXEC.BAT file (here DEL is the DOS equivalent of rm), or
probably there's some way of hooking into the registry to achieve the
same effect.

Oct 19 '08 #5
Antoninus Twink wrote, On 19/10/08 08:40:
On 18 Oct 2008 at 22:12, Malcolm McLean wrote:
>"Coffee Pot" <no****@nospam. comwrote in message
>>Thanks for any advice.
I don't know about Windows specifically but generally you need to
close the file or the attempted deletion will fail.
<snip>
I guess you could do this by appending a line

DEL c:\path\to\file

to the AUTOEXEC.BAT file (here DEL is the DOS equivalent of rm), or
As usual Antoninus has guessed something that is very poor advice.
probably there's some way of hooking into the registry to achieve the
same effect.
He has also failed to mention the need to go else where to discus what
might be slightly closer to a possible solution, although one with
server restrictions. As mentioned else-where the best advice is to
discus the problem in a Windows group. When doing so also say *why* you
want to delete an open file as this might have a big impact on the best
solution.
--
Flash Gordon
If spamming me sent it to sm**@spam.cause way.com
If emailing me use my reply-to address
See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/
Oct 19 '08 #6
Coffee Pot wrote:
Thanks for any advice.

~ CP
The DeleteFileTrans acted function marks a file for deletion on close.
Therefore, the file deletion does not occur until the last handle to the
file is closed.

You should get the documentation, it is freely available by
Microsoft. Download the SDK.
--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Oct 19 '08 #7
When doing so also say *why* you
want to delete an open file as this might have a big impact on the best
solution.
Thanks for the replies.

Basically I am trying to port the following UNIX code to Windows.

FILE *tmpfil;
char template[]="/tmp/XXXXXX";
mktemp(template );
tmpfil = fopen(template, "w+");
unlink(template );

This guarantees that the temporary file will be removed at the end of
execution even if the program terminates abnormally.

Oct 19 '08 #8
Coffee Pot wrote:
>When doing so also say *why* you
want to delete an open file as this might have a big impact on the best
solution.

Thanks for the replies.

Basically I am trying to port the following UNIX code to Windows.

FILE *tmpfil;
char template[]="/tmp/XXXXXX";
mktemp(template );
tmpfil = fopen(template, "w+");
unlink(template );

This guarantees that the temporary file will be removed at the end of
execution even if the program terminates abnormally.
As has already been suggested, you need to ask in a windows programming
group. I strongly suspect windows has its own mechanism for creating and
clearing up temp files which will work very similarly but using
platform-specific functions.

BTW AFAIK the above code isn't guaranteed to work on Linux/Unix either,
if the app crashed between the fopen and the unlink.
--
Mark McIntyre

CLC FAQ <http://c-faq.com/>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >
Oct 19 '08 #9
Mark McIntyre <ma**********@T ROUSERSspamcop. netwrites:
Coffee Pot wrote:
>>When doing so also say *why* you
want to delete an open file as this might have a big impact on the best
solution.

Thanks for the replies.

Basically I am trying to port the following UNIX code to Windows.

FILE *tmpfil;
char template[]="/tmp/XXXXXX";
mktemp(templat e);
tmpfil = fopen(template, "w+");
unlink(templat e);

This guarantees that the temporary file will be removed at the end of
execution even if the program terminates abnormally.

As has already been suggested, you need to ask in a windows
programming group. I strongly suspect windows has its own mechanism
for creating and clearing up temp files which will work very similarly
but using platform-specific functions.
I can't see why. Surely the standard C function

include <stdio.h>
FILE *tmpfile(void);

The tmpfile function creates a temporary binary file that is
different from any other existing file and that will automatically
be removed when it is closed or at program termination. If the
program terminates abnormally, whether an open temporary file is
removed is implementation-defined. The file is opened for update
with "wb+" mode.

is what the OP needs?
BTW AFAIK the above code isn't guaranteed to work on Linux/Unix
either, if the app crashed between the fopen and the unlink.
I suspect the unlink follows the fopen (this is an old but common
idiom) so such a crash is not likely. If one were to give any
off-topic advice about the Unix code it would be: "don't use mktemp --
it is a ghoul from the past and there is no point in trying to
replicate its various faults in new code".

--
Ben.
Oct 19 '08 #10

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

Similar topics

2
6693
by: Eric | last post by:
Hello, I created a form to upload files. The formfield contents are stored as records in an Ascii file "data.dat"; for reasons I am not using a MySql database. The upoaded file itself is stored in a directory called "files". With an administration form I can read all contents of the data.dat file and select records to delete (one at a time). So far so good.
4
3716
by: rbt | last post by:
Can someone detail the differences between these two? On Windows which is preferred? Also, is it true that win32api.DeleteFile() can remove the 'special' files located in the 'special' folders only accessible by the shell object such as Temporary Internet Files, etc. Thanks!
10
7600
by: lorenzogordon | last post by:
Hi there, I'd greatly appreciate any insights into the following problem: I've got PHP running fine on IIS (OS: Server 2003, SP1; IIS: 6.0; PHP: 4.3.11). In PHP, the user uploads a file, which is then processed and the contents are inserted into a new file, created in PHP, onto the server.
6
16775
by: Benjamin | last post by:
Hello, I'm running PHP 5.2.0 as a Apache 2.2 server module. When, I try to unlink I get this: "Warning: unlink(file.txt) : Permission denied..." What and how do I change this so it works? Thanks
4
12828
by: nitinpatel1117 | last post by:
Hi I am trying to delete a csv file using the unlink() function, but i keep getting a permission denied warning Warning: unlink(UsedNOS200704191.CSV): Permission denied in E:\webroot\dev\file_test\index.php on line 416 My script basically reads email attachments and downloads the attachment file into the current directory of the running script. The data in the attachements is then read and stored into MySQL. Once i've finished reading...
8
3898
by: jessica87 | last post by:
hi there.... i need some guidance here with this code? Code: ( text ) 1. $myFile = "testFile.txt"; 2. $fh = fopen($myFile, 'w') or die("can't open file");
15
3767
by: Morteneistrom | last post by:
Im a complete n00b at PHP so please forgive me if this i a stupid question Why wont the following work. PHP Code: <?php foreach (glob('../images/profil/*.jpg') as $filename) { echo "$filename <br>". "<a href="unlink('$filename');">Delete</ a>";
11
4462
by: byteoart | last post by:
I'm a newbie to php. I am trying to delete a file from the server that I put there (one file, not the whole directory). I am using unlink ($filename); I did the path as a relative path as follows: .../images/6/image_001.JPG I did a scandir $files = scandir("../images/6"); The print_r ($files); shows that the file is one of the files present.
9
8461
code green
by: code green | last post by:
I have this piece of code to tidy up after a function that calls move_uploaded_file() if(file_exists($destination.$filename)) { $exitmsg .= "<br>File still in temporary location <br>$filename <br>Attempting deletion "; if(unlink($destination.$filename)) $exitmsg .= '<br>Success deleting. Please try again '; else
0
8872
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8747
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9392
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9091
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8069
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6694
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5997
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4505
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2619
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.