Search code examples
filebatch-filecmdselfbatch-delete

cannot echo self delete command in another batch file


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


Solution

  • 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