htmlbatch-filehta

How do I have my .hta file deleted after a period of time?


@echo off
@title Login
echo ^<htm^> >> "start.hta"
echo ^<head^> >> "start.hta"
echo ^<link rel="stylesheet" link="style.css"^> >> "start.hta"
echo ^</head^> >> "start.hta"
echo ^<body^> >> "start.hta"
echo ^<p^>Hello!^</p^> >> "start.hta"
echo ^</body^> >> "start.hta"
echo ^</html^> >> "start.hta"
start.hta

Above was my code. I want to have a sort of a timer where we just delete this file after the timer finishes.


Solution

  • Get the process id by incorporating some powershell, sleep 10 seconds and kill by process id.

    @echo off
    @title Login
    echo ^<htm^> > "start.hta"
    echo ^<head^> >> "start.hta"
    echo ^<link rel="stylesheet" link="style.css"^> >> "start.hta"
    echo ^</head^> >> "start.hta"
    echo ^<body^> >> "start.hta"
    echo ^<p^>Hello!^</p^> >> "start.hta"
    echo ^</body^> >> "start.hta"
    echo ^</html^> >> "start.hta"
    for /f %%i in ('powershell "(Start-Process start.hta -passthru).ID"') do (
        timeout /t 10
        taskkill /F /PID %%i
        del "start.hta"
    )