Thread: Making a program delete itself using bat files

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    30

    Making a program delete itself using bat files

    Hello, Im making a function that will cause my program to create a temporary bat file that will terminate and delete the main program and then delete itself:

    Code:
         ofstream myfiledel;
        myfiledel.open ("tempdel.bat");
        myfiledel << "@echo off"<<endl;
        myfiledel <<"taskkill /im program.exe /f"<<endl;
          myfiledel <<"echo Deleting..."<<endl;
           myfiledel <<"del program.exe"<<endl;
           myfiledel <<"del tempdel.bat"<<endl;
        myfiledel.close();
    
    system("start tempdel.bat");
    If I run the .exe, it will start with creating the bat and then start the bat file in another window, but after the bat file is done I first get the "deleting..." echo, and then I get this message (translated from swedish to english):
    Can not find the command file.

    Both the bat and exe are deleted, but that message shows up in a cmd window.
    If I instead manually create and execute the bat file without using the command in the exe, everything is deleted without this message showing up.
    So I think it is becouse the system() launches the bat from inside a cmd window (might be wrong), And therefore I think I would need a replacement for system ().
    Could anyone give me some advice here?

    PS: Im not that good at english and I suck at describe things, but I hope you understand what Im trying to do.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    In the first place, I'm not sure how this program is going to help you do anything constructive, since writing and saving a bat file yourself, then executing it is all you need.

    Anyway, start has a switch called /Dpath that you probably need to use to set the starting directory, so you can point to the file you want to delete.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    30
    Sorry if I didnt explain it properly, The files are deleted without any problem, but the window will not shut down like it does if I would launch the bat manually. Instead it will show the message.
    And the bat file is created by the exe.
    I just created it manually for testing purposes.
    I think that the message shows up becouse it couldnt find the file after the file was deleted. I simply want the window to close when the file is deleted like it does if I launch the bat manually
    Last edited by Danne; 02-19-2011 at 05:38 PM.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Well you might want to turn echo on just to see where the problem is exactly.

    But batch files won't close without a problem if you ask them to delete themselves.

    Code:
    del temp.bat
    Even if this is all temp.bat says, you should get an error.

    But you can let the C++ program delete the batch file for you. See remove(). Just be sure you've closed any streams pointing to it.

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    30
    If I make a bat that deletes itself and click it, it will do that without any errors.
    If I launch the bat with System() the error shows up.
    If I launch the bat from the CMD the error shows up.

    And how would I use remove() in the c++ file if the c++ file is the .exe that is terminated and deleted from the batch?

    Edit: if it matters im using XP
    Last edited by Danne; 02-19-2011 at 05:43 PM.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Try running the batch file with system("start cmd /k ....");

    You'd probably be better off writing a dedicated program to do the deletion - that way, you get away from the vagaries of command interpreters and their variation between installations.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    Jan 2011
    Posts
    30
    Quote Originally Posted by grumpy View Post
    Try running the batch file with system("start cmd /k ....");
    Would you mind explaining what that does?
    If it launches the bat from within a CMD window then it wont work without the error.

    And I dont need a entire program for the deletion. The problem is just that the bat file dont want to delete itself without showing an error if it is launched from syetem().

    To make things clear: Problem is that the cmd window dont close after the bat deleted itself, instead it shows a message. thats the only problem. all files are deleted as the are suposed to.
    Last edited by Danne; 02-19-2011 at 05:52 PM.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    And I dont need a entire program for the deletion. The problem is just that the bat file dont want to delete itself without showing an error if it is launched from syetem().
    Let me be abundantly clear: I do not think that this is a problem with system() or how you invoke your batch. If you ask a batch to delete itself you would get an error. The command line would attempt to return to the batch for the next command, not find it, and produce the error.

    Looking over the program that produces and executes the batch file, I also hope that program.exe is not the same program as this one...

    I hope I can put this in a blunt way you understand. There is such a thing as an orderly exit, and in programs it's supposed to be a necessity, not a feature.

  9. #9
    Registered User
    Join Date
    Jan 2011
    Posts
    30
    I understand that its becouse the cmd cant find the bat. But why doesnt that happend if I just click on a bat that would delete itself??

    And yes, the program.exe is the same that produces the bat, but the program.exe is deleted without any problem. So I dont understand what you mean.

    Is there any way to simply get the cmd to close when it cant find the bat after its deleted? that would solve the problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 11-17-2010, 11:20 PM
  2. Multi - File Program not finding files
    By johnh444 in forum C++ Programming
    Replies: 2
    Last Post: 07-03-2004, 01:48 AM
  3. Problem need help
    By Srpurdy in forum C++ Programming
    Replies: 1
    Last Post: 07-24-2002, 12:45 PM
  4. Program to delete files
    By Drane85 in forum Windows Programming
    Replies: 1
    Last Post: 04-04-2002, 04:52 AM
  5. memory management...
    By master5001 in forum Game Programming
    Replies: 24
    Last Post: 01-07-2002, 05:50 PM