0

I'm using LAME encoder to encode an audio file from .wav to .mp3, to achieve this, I create a procces that encodes the audio file and tries to delete the old .wav file.

//command = start the lame exe
//params = lame encode params
    if (CreateProcess(command, params, NULL, NULL, false,CREATE_NEW_CONSOLE, NULL, NULL, &sInfo, &pInfo)){
        WaitForSingleObject(pInfo.hProcess, INFINITE);
        CloseHandle(pInfo.hThread);
        CloseHandle(pInfo.hProcess);
        //Encoding process ended.
        remove(filePath);
    }

The problem comes when the program tries to delete the file, it's opened by an unknown task, so the deletion becomes impossible.

I need that file to be deleted there.

Is there any way to close all tasks using the file? If not, is it possible to force a delete while the file is being used?

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
user6385193
  • 53
  • 1
  • 8
  • What if one of those tasks is Explorer? – David Schwartz May 26 '16 at 09:02
  • How can I know which task is managing the file? – user6385193 May 26 '16 at 09:07
  • 3
    Don't use `remove`. Use [`DeleteFile`](https://msdn.microsoft.com/en-us/library/windows/desktop/aa363915(v=vs.85).aspx), which should mark the file for deletion and perform said-action as soon as the last handle is liberated. – WhozCraig May 26 '16 at 09:09
  • @WhozCraig Will it still work when the program closes? The program closes just after the file remove is performed. – user6385193 May 26 '16 at 09:11
  • 1
    I'll be honest, its been so long I honestly can't recall, but I believe it should. You need delete permission on the file (obviously). Once you make that call, I believe its a done deal when the last man shuts out the lights. Should be easy enough to test. – WhozCraig May 26 '16 at 09:13
  • 2
    *"How can I know which task is managing the file?"* - That's a different question. The answer: [How do I find out which process has a file open?](https://blogs.msdn.microsoft.com/oldnewthing/20120217-00/?p=8283) – IInspectable May 26 '16 at 09:24
  • @WhozCraig after 5 minutes of the delete execution, nothing happened, so I reboot wiindows and still the files aren't deleted. – user6385193 May 26 '16 at 09:24
  • Command `lsof` will list the applications which use a specified file. `lsof file_name` – where23 May 26 '16 at 09:27
  • @where23 that command only works on linux and osx. – user6385193 May 26 '16 at 09:37
  • 1
    @WhozCraig No, I don't think it works like that. There is `MoveFileEx` with `MOVEFILE_DELAY_UNTIL_REBOOT`. – David Heffernan May 26 '16 at 10:05
  • 1
    @user6385193 Do you want to delete on a reboot? Do you want to delete when the last handle to the file is closed? Or do you want to forcibly (and possibly painfully) close/delete the file out from under any processes that have it open? These are three different things and it's not clear which you want. – David Schwartz May 26 '16 at 10:09
  • I want to forcibly close/delete the file. – user6385193 May 26 '16 at 10:14
  • You could have easily Googled for this first, before asking the question: http://stackoverflow.com/questions/3763526/how-to-remove-the-file-which-has-opened-handles, http://stackoverflow.com/questions/301174/delete-a-file-in-use-in-runtime, http://stackoverflow.com/questions/215461/how-can-i-force-the-deletion-of-locked-files-in-c-c – Cody Gray - on strike May 26 '16 at 11:41
  • @WhozCraig You're right that the documentation does say something along those lines, but I'm like David. I'm pretty sure either that is wrong, or only true in certain contexts. In my experience, DeleteFile just fails if the file is open, without going back later and deleting it. Which is pretty common if you try to call DeleteFile immediately after CloseHandle. The kernel tends to have a several second delay in actually getting around to closing its outstanding references. A more reliable solution is the `FILE_FLAG_DELETE_ON_CLOSE` flag (which applies to the handle, of course, not the file). – Cody Gray - on strike May 26 '16 at 11:47
  • @CodyGray I said that I wanted to forcibly close/delete the file, choosing between those three options. The question itslef, doesn't change, so I don't think the point of marking the question as duplicate when it isn't. I still want to know the asnwer of my question, so I would apreciate that you reconsider that duplicate. Thanks. – user6385193 May 26 '16 at 12:29
  • You want to delete the file in spite of other processes using it? Are you prepared to accept that is not possible? – David Heffernan May 26 '16 at 12:33
  • I'm asking if there is any way to close all tasks using the file or if there is a way to stop other precesses using the file. – user6385193 May 26 '16 at 12:34
  • It seems you already know how to do that: CloseHandle. – Cody Gray - on strike May 26 '16 at 12:35
  • You can kill the processes using the file, perhaps. Perhaps not. You can't persuade the other tasks to stop. Why are you asking this at all? – David Heffernan May 26 '16 at 12:37
  • Thanks. I'll give a try. Still the question itself isn't duplicated. – user6385193 May 26 '16 at 12:37
  • I don't understand your question @DavidHeffernan . Am not I able to ask when I don't know something? – user6385193 May 26 '16 at 12:39
  • It's a dupe many times over. I'm wanting to know why you feel the need to delete a file that is in use. – David Heffernan May 26 '16 at 12:48
  • @DavidHeffernan in theory, it shouldn't be in use, that's why I create the process, to wait until the conversion finishes. – user6385193 May 26 '16 at 12:50
  • Looks like you asked the wrong then – David Heffernan May 26 '16 at 12:55
  • @DavidHeffernan Do you really feel superior making me feel bad? I'm just not an advanced programmer and I was asking a question, but now I feel bad. Huge THANKS in advice, maybe one day you'll notice that all people doesn't know all about coding. – user6385193 May 26 '16 at 12:56
  • 1
    Please stop reacting like that. You asked the wrong question. A simple statement. What you should be asking is why the file is still in use after the process has terminated. One might guess that your anti-malware software has it locked. Had you asked that question it would also have been a dupe many times over. You really aren't interested in killing the processes that have the file locked since you won't be able to do it, and it would be a bad idea in any case. – David Heffernan May 26 '16 at 13:00
  • It is possible for a privileged process to take the file out from under all the processes that currently have it open and then close the file. It's extremely dangerous, but it definitely can be done if you're 100% sure it's what you really want to do. (It's sometimes useful when combating malware.) – David Schwartz May 27 '16 at 16:55

0 Answers0