0

i have a file named as new.bat.

  1. using echo creates another "1.bat" batch file and writes code in it
  2. self delete command is also added in 1.bat
  3. 1.bat is run by start /MIN 1.bat

my main file (new.bat) file is getting deleted and cmd process exit, leaving behind 1.bat which i want to delete.

i know del "%~f0" & exit with this command self batch file is deleted, but wrong batch file is deleted

here are my below files

New.bat

echo echo 1 >>1.bat
echo del "%~f0" & exit >>1.bat
start /MIN 1.bat

pl Help

Prayag
  • 211
  • 1
  • 13

1 Answers1

1

Try either:

Echo Echo 1 >>1.bat
Echo Del "%%~f0" ^& Exit >>1.bat
Start /MIN 1.bat

Or:

(   Echo Echo 1
    Echo Del "%%~f0" ^& Exit
)>1.bat
Start /MIN 1.bat
Compo
  • 36,585
  • 5
  • 27
  • 39
  • The reason being that in the original code the `%~f0` was being interpreted _inside_ `new.bat` and so was adding a command to delete `new.bat` to `1.bat`. The double `%%` prevents interpretation by the `echo` command, so that `del "%~f0"` ends up in `1.bat` and when _that_ command is run, it correctly deletes `1.bat`. – TripeHound May 08 '17 at 12:14