Guest User

BfT's Master Script

a guest
Feb 2nd, 2022
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.48 KB | None | 0 0
  1. @echo off
  2. Title %~n0
  3. GoTo Begin
  4. :End
  5.  
  6. ::"Self-destruction" part that deletes batch file copy in %Temp% folder and confirms whether file was actually deleted.
  7.  
  8. Start /min cmd /c (@echo off^&Title "%~nx0" Temp Batch File Deletion^&echo Deleting file "%Temp%\%~nx0" . . . ^&TimeOut 5 /NoBreak ^>nul 2^>^&1^&Del /a "%Temp%\%~nx0"^&ClS^&If Exist "%Temp%\%~nx0" ^(echo File "%Temp%\%~nx0" could not be deleted!^&Pause ^>nul 2^>^&1^) else ^(echo File "%Temp%\%~nx0" deleted successfully!^&TimeOut 5 /NoBreak ^>nul 2^>^&1^))
  9. If NOT Defined Elev (@TimeOut 5 /NoBreak >nul 2>&1)
  10. GoTo :EoF
  11.  
  12. :Begin
  13.  
  14. ::Setting variable %_nx0% derived from %~nx0 with several characters escaped for copying file to %Temp% folder
  15.  
  16. Set "_nx0=%~nx0"
  17. Set "_nx0=%_nx0:(=^(%"
  18. Set "_nx0=%_nx0:)=^)%"
  19. Set "_nx0=%_nx0:,=^,%"
  20. Set "_nx0=%_nx0:;=^;%"
  21. Set "_nx0=%_nx0:&=^&%"
  22. Set "_nx0=%_nx0:@=^@%"
  23. Set "_nx0=%_nx0: =^ %"
  24.  
  25. ::Setting variable %_dpnx0% derived from %~dpnx0 with several characters escaped for passing file name to PowerShell
  26.  
  27. Set "_dpnx0=%~dpnx0"
  28. Set "_dpnx0=%_dpnx0:)=^)%"
  29. Set "_dpnx0=%_dpnx0:,=^,%"
  30. Set "_dpnx0=%_dpnx0:;=^;%"
  31. ::How to Escape & and @ for Passing to PowerShell's Start-Process in a For Loop?
  32. Set "_dpnx0=%_dpnx0:&=^^^&%"
  33. Set "_dpnx0=%_dpnx0:@=^^^@%"
  34. Set "_dpnx0=%_dpnx0: =^ %"
  35.  
  36. ::Just a test line to debug new variables
  37. ::echo %_nx0%&echo %_dpnx0%&TimeOut 3 >nul
  38.  
  39. ::Commands to confirm whether file is in %Temp% folder and to copy it there if it isn't
  40.  
  41. If NOT "%~dp0" == "%Temp%\" (Copy /y "%~dpnx0" "%Temp%" >nul 2>&1&&Start cmd /c "%Temp%\%_nx0%"&&exit /b)
  42.  
  43. ::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
  44. ::a "For" loop is used to set variable %Elev% to pass elevation confirmation from PowerShell's "$?" command back to cmd as "True" or "False"
  45. ::If file is run with admin privileges run :Main part below and end script with :End self-destruction command above
  46. ::If File is run without admin privileges end script via ":EoF" command
  47.  
  48. net file >nul 2>&1
  49. If %ErrorLevel%==0 (Call :Main&GoTo End) Else (
  50.    For /f "Tokens=* UseBackQ" %%f in (`PowerShell -NoLogo -Command "& { Start-Process \"%_dpnx0%\" -Verb RunAs;$? }"`) Do (Set "Elev=%%f"))
  51. If %Elev%==False (GoTo End) else (GoTo :EoF)
  52. :Main
  53.  
  54. ::Main part of script after header
  55.  
  56. echo Testing the batch header . . . [39
  57. TimeOut /t 5 >nul
Advertisement
Add Comment
Please, Sign In to add comment