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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Coffee Pot

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

    Thanks for any advice.

    ~ CP

  • Malcolm McLean

    #2
    Re: How can I unlink/delete an open file in Windows?

    "Coffee Pot" <nospam@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.

    Comment

    • Nate Eldredge

      #3
      Re: How can I unlink/delete an open file in Windows?

      "Malcolm McLean" <regniztar@btin ternet.comwrite s:
      "Coffee Pot" <nospam@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.

      Comment

      • Keith Thompson

        #4
        Re: How can I unlink/delete an open file in Windows?

        Nate Eldredge <nate@vulcan.la nwrites:
        "Malcolm McLean" <regniztar@btin ternet.comwrite s:
        >"Coffee Pot" <nospam@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) kst-u@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"

        Comment

        • Antoninus Twink

          #5
          Re: How can I unlink/delete an open file in Windows?

          On 18 Oct 2008 at 22:12, Malcolm McLean wrote:
          "Coffee Pot" <nospam@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.

          Comment

          • Flash Gordon

            #6
            Re: How can I unlink/delete an open file in Windows?

            Antoninus Twink wrote, On 19/10/08 08:40:
            On 18 Oct 2008 at 22:12, Malcolm McLean wrote:
            >"Coffee Pot" <nospam@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 smap@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/

            Comment

            • jacob navia

              #7
              Re: How can I unlink/delete an open file in Windows?

              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

              Comment

              • Coffee Pot

                #8
                Re: How can I unlink/delete an open file in Windows?

                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.

                Comment

                • Mark McIntyre

                  #9
                  Re: How can I unlink/delete an open file in Windows?

                  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 >

                  Comment

                  • Ben Bacarisse

                    #10
                    Re: How can I unlink/delete an open file in Windows?

                    Mark McIntyre <markmcintyre@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.

                    Comment

                    • Keith Thompson

                      #11
                      Re: How can I unlink/delete an open file in Windows?

                      jacob navia <jacob@nospam.c omwrites:
                      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.
                      That might be the best possible answer to your question. But since
                      most of us here aren't Windows experts, you're not going to get very
                      many other suggestions, so you can't really tell.
                      You should get the documentation, it is freely available by
                      Microsoft. Download the SDK.
                      And you should ask in a newsgroup that discusses Windows programming.

                      --
                      Keith Thompson (The_Other_Keit h) kst-u@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"

                      Comment

                      • Mark McIntyre

                        #12
                        Re: How can I unlink/delete an open file in Windows?

                        Ben Bacarisse wrote:
                        Mark McIntyre <markmcintyre@T ROUSERSspamcop. netwrites:
                        >
                        >Coffee Pot wrote:
                        >>Basically I am trying to port the following UNIX code to Windows.
                        >>>
                        >>FILE *tmpfil;
                        >>char template[]="/tmp/XXXXXX";
                        >>mktemp(templa te);
                        >>tmpfil = fopen(template, "w+");
                        >>unlink(templa te);
                        >>>
                        >>This guarantees that the temporary file will be removed at the end of
                        >>execution even if the program terminates abnormally.
                        Note this sentence...
                        >As has already been suggested, you need to ask in a windows
                        >programming group.
                        >
                        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?
                        From his post above what he wanted was a guarantee the file would be
                        cleaned up. As the section you quoted made clear, for tmpfile() this is
                        implementation defined.

                        >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.
                        Agreed, but i suspect its at least as likely the file will get used
                        before being unlink()ed, so....

                        --
                        Mark McIntyre

                        CLC FAQ <http://c-faq.com/>
                        CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >

                        Comment

                        • Ben Bacarisse

                          #13
                          Re: How can I unlink/delete an open file in Windows?

                          Mark McIntyre <markmcintyre@T ROUSERSspamcop. netwrites:
                          Ben Bacarisse wrote:
                          >Mark McIntyre <markmcintyre@T ROUSERSspamcop. netwrites:
                          >>
                          >>Coffee Pot wrote:
                          >>>Basically I am trying to port the following UNIX code to Windows.
                          >>>>
                          >>>FILE *tmpfil;
                          >>>char template[]="/tmp/XXXXXX";
                          >>>mktemp(templ ate);
                          >>>tmpfil = fopen(template, "w+");
                          >>>unlink(templ ate);
                          >>>>
                          >>>This guarantees that the temporary file will be removed at the end of
                          >>>execution even if the program terminates abnormally.
                          >
                          Note this sentence...
                          Yes, but we both know that this is not true, in general, for the above
                          code so it seems unlikely that the OP really needs that as a hard and
                          fast guarantee. If the program did need that behaviour, then the
                          original code would not do the bad mktemp/unlink trick.
                          >>As has already been suggested, you need to ask in a windows
                          >>programming group.
                          >>
                          >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?
                          >
                          From his post above what he wanted was a guarantee the file would be
                          cleaned up. As the section you quoted made clear, for tmpfile() this
                          is implementation defined.
                          All I am suggesting is that tmpfile should be the first answer when
                          someone posts this question here. Is there any reason to think that
                          tmpfile does not do what the OP needs when the program terminates
                          abnormally? Maybe I am being overly generous to Windows C
                          implementation, but surely if it can be done at all, tmpfile will do
                          it.
                          >>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.
                          >
                          Agreed, but i suspect its at least as likely the file will get used
                          before being unlink()ed, so....
                          I think what they posted was the code they are porting (at least I
                          can't see any reason to assume otherwise at this stage). I.e. the
                          unlink follows immediately after the fopen.

                          --
                          Ben.

                          Comment

                          • Willem

                            #14
                            Re: How can I unlink/delete an open file in Windows?

                            Mark McIntyre wrote:
                            ) Ben Bacarisse wrote:
                            )I suspect the unlink follows the fopen (this is an old but common
                            )idiom) so such a crash is not likely.
                            )
                            ) Agreed, but i suspect its at least as likely the file will get used
                            ) before being unlink()ed, so....

                            Doubtful. The usual trick is to unlink() it directly after open()ing it.

                            (As you may or may not know, as long as the unlinked file remains open,
                            you can still use it.)


                            SaSW, Willem
                            --
                            Disclaimer: I am in no way responsible for any of the statements
                            made in the above text. For all I know I might be
                            drugged or something..
                            No I'm not paranoid. You all think I'm paranoid, don't you !
                            #EOT

                            Comment

                            • Mark McIntyre

                              #15
                              Re: How can I unlink/delete an open file in Windows?

                              Ben Bacarisse wrote:
                              Yes, but we both know that this is not true, in general, for the above
                              code so it seems unlikely that the OP really needs that as a hard and
                              fast guarantee.
                              *shrug*. You may be right, but I dont propose to second-guess the OP. He
                              said he wanted a guaranteed method, C doesn't provide one, his OS may,
                              this isn't the place to ask about that.
                              All I am suggesting is that tmpfile should be the first answer when
                              someone posts this question here.
                              I'd agree, except that his original question wasn't about tmpfile, and
                              when he clarified what he wanted, tmpfile didn't fit the bill !
                              >Is there any reason to think that
                              tmpfile does not do what the OP needs when the program terminates
                              abnormally?
                              Take a look in the %temp% directory just after crash-rebooting a Windows
                              PC by pulling the power cable.
                              Maybe I am being overly generous to Windows C
                              implementation, but surely if it can be done at all, tmpfile will do
                              it.
                              ??? You're having a laugh, surely.

                              --
                              Mark McIntyre

                              CLC FAQ <http://c-faq.com/>
                              CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >

                              Comment

                              Working...