1

I see answers here to parts of my question, but none that covers it all together. Apologies if I missed one.

Here's what I have: A batch file that calls out other batch files, deletes the files at the end, and reboots the computer. I am using this for certain configs on new machines I set up. Right now, I have to select multiple files, throw them on the new desktop, and run them there.

I modified the batch to run these out of a single directory that I place on the desktop, and it deleted the folder when it's done, but it's lost the ability to reboot. Here are the last 3 lines:

cd C:\Users\XXXX\Desktop
RD /S /Q "C:\Users\XXXX\Desktop\YYYYYYYYY"
shutdown -r
(del %0)

What is the best way to execute this? Do I create a new batch file designed to do nothing but the last 3 actions above(I listed the first one because I assume I have to get out of the directory first to delete it)? If so, does it really just have to be those three lines? Like the first action run would be to copy the file in question to the desktop and then execute it as the last action after changing the directory?

Sorry if I made this post too wordy, just trying to provide as much detail as possible.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Michael Wells
  • 43
  • 1
  • 3
  • 6
  • possible duplicate of [How to make a batch file delete itself?](http://stackoverflow.com/questions/20329355/how-to-make-a-batch-file-delete-itself) – Bacon Bits May 27 '15 at 17:27
  • So is this batch file in C:\Users\XXXX\Desktop\YYYYYYYYY? If yes, the last two lines are never read to be executed. Also, when you issue shutdown -r, it might kill the cmd session so it won't get to the last command in the script, so use shutdown /t:5 to give it 5 seconds to complete (1 or 2 would probably be enough, though). – Tony Hinkle May 27 '15 at 18:17
  • @TonyHinkle your comment should be an answer – Toby Allen May 27 '15 at 20:52
  • @TonyHinkle, what I have it doing currently is copying the 'Cleanup' file I posted above, and then the 'RD' command is to delete the master folder where everything else lives. So it copies the file above to the desktop, runs the main batch file, exits out of the directory, deletes the directory, gives the shutdown command, and then deletes itself. I hope this makes sense... – Michael Wells May 28 '15 at 18:29

2 Answers2

3

Batch files are read and executed line by line, so you can not execute that code in that way. However, code blocks (several lines enclosed in parentheses) are first read/parsed and then the whole block is executed, so the solution to your problem is this:

(
cd C:\Users\XXXX\Desktop
RD /S /Q "C:\Users\XXXX\Desktop\YYYYYYYYY"
del "%~F0"
shutdown -r
)

Of course, this method will produce an error message when cmd.exe try to read the next line and the batch file no longer exists, but that message don't matters in this case.

Aacini
  • 65,180
  • 12
  • 72
  • 108
  • The error message can be avoided, see the post of dbenham to this problem at [SO: How to make a batch file delete itself?](http://stackoverflow.com/a/20333575/463115) – jeb May 27 '15 at 20:45
1

Batch files are read line by line, so if you delete the batch file before the script is complete, it will not be able to read the next line. You can easily demonstrate this with the following script:

@echo off 
echo Beginning
del %0.bat
echo End

Run the script from a command prompt, and you'll see that it never echos "End" and gives you an error "The batch file cannot be found." So if your batch file is in the directory you are removing, you'll need to move it elsewhere, or like you say, create a different batch script.

Secondly, I found in my test that you have to add the .bat extension onto the %0 as shown in the script above for it to delete the file.

Lastly, if you issue a command to shut down the system, it is going to start killing processes, your cmd session being one of them. Accordingly, you need to delay it by using shutdown /t:5 /r so that your script has a few seconds to complete.

Tony Hinkle
  • 4,706
  • 7
  • 23
  • 35