Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @echo off
- Title %~n0
- GoTo Begin
- :End
- ::"Self-destruction" part that deletes batch file copy in %Temp% folder and confirms whether file was actually deleted.
- Start /min cmd /c (@echo off^&Title "%~nx0" Temp Batch File Deletion^&echo [93mDeleting file "%Temp%\%~nx0" . . . [39m^&TimeOut 5 /NoBreak ^>nul 2^>^&1^&Del /a "%Temp%\%~nx0"^&ClS^&If Exist "%Temp%\%~nx0" ^(echo [91mFile "%Temp%\%~nx0" could not be deleted![39m^&Pause ^>nul 2^>^&1^) else ^(echo [92mFile "%Temp%\%~nx0" deleted successfully![39m^&TimeOut 5 /NoBreak ^>nul 2^>^&1^))
- If NOT Defined Elev (@TimeOut 5 /NoBreak >nul 2>&1)
- GoTo :EoF
- :Begin
- ::Setting variable %_nx0% derived from %~nx0 with several characters escaped for copying file to %Temp% folder
- Set "_nx0=%~nx0"
- Set "_nx0=%_nx0:(=^(%"
- Set "_nx0=%_nx0:)=^)%"
- Set "_nx0=%_nx0:,=^,%"
- Set "_nx0=%_nx0:;=^;%"
- Set "_nx0=%_nx0:&=^&%"
- Set "_nx0=%_nx0:@=^@%"
- Set "_nx0=%_nx0: =^ %"
- ::Setting variable %_dpnx0% derived from %~dpnx0 with several characters escaped for passing file name to PowerShell
- Set "_dpnx0=%~dpnx0"
- Set "_dpnx0=%_dpnx0:)=^)%"
- Set "_dpnx0=%_dpnx0:,=^,%"
- Set "_dpnx0=%_dpnx0:;=^;%"
- ::How to Escape & and @ for Passing to PowerShell's Start-Process in a For Loop?
- Set "_dpnx0=%_dpnx0:&=^^^&%"
- Set "_dpnx0=%_dpnx0:@=^^^@%"
- Set "_dpnx0=%_dpnx0: =^ %"
- ::Just a test line to debug new variables
- ::echo %_nx0%&echo %_dpnx0%&TimeOut 3 >nul
- ::Commands to confirm whether file is in %Temp% folder and to copy it there if it isn't
- If NOT "%~dp0" == "%Temp%\" (Copy /y "%~dpnx0" "%Temp%" >nul 2>&1&&Start cmd /c "%Temp%\%_nx0%"&&exit /b)
- ::Commands to confirm via PowerShell command whether file is run with admin privileges and to request them by restarting script via PowerShell's "-Verb RunAs" command if file is run without admin privileges
- ::a "For" loop is used to set variable %Elev% to pass elevation confirmation from PowerShell's "$?" command back to cmd as "True" or "False"
- ::If file is run with admin privileges run :Main part below and end script with :End self-destruction command above
- ::If File is run without admin privileges end script via ":EoF" command
- net file >nul 2>&1
- If %ErrorLevel%==0 (Call :Main&GoTo End) Else (
- For /f "Tokens=* UseBackQ" %%f in (`PowerShell -NoLogo -Command "& { Start-Process \"%_dpnx0%\" -Verb RunAs;$? }"`) Do (Set "Elev=%%f"))
- If %Elev%==False (GoTo End) else (GoTo :EoF)
- :Main
- ::Main part of script after header
- echo [38;5;202mTesting the batch header . . . [39
- TimeOut /t 5 >nul
Advertisement
Add Comment
Please, Sign In to add comment