title: Batch file - Run as administrator, if not delete batch file?
body: <p>Hi I have made a batch file which I need to run as administrator. For that purpose I use this script, which I took from <a href="https://stackoverflow.com/a/12264592/1016343">here</a> (StackOverflow).</p>
  
  <p>But what I want, is if the user choose not to run as administrator (click NO to UAC), then the program will exit, and the batch will delete it selves automatically.</p>
  
  <p>The command for the batch file to delete it selves is "del %0", but I need help as to where in this script, I can put this command.
  I tried to put it with "@exit /B", but then the batch file will delete if you press either YES or NO to UAC, and then the rest of the batch file cannot execute   </p>
  
  <p>Anybody can help figure out how to only run the command "del %0", when a user press "NO" to UAC?</p>
  
  <hr>
  
  <pre><code>@echo off
  :checkPrivileges 
  NET FILE 1&gt;NUL 2&gt;NUL
  if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges ) 
  
  :getPrivileges 
  if '%1'=='ELEV' (shift &amp; goto gotPrivileges) 
  
  setlocal DisableDelayedExpansion
  set "batchPath=%~0"
  setlocal EnableDelayedExpansion
  ECHO Set UAC = CreateObject^("Shell.Application"^) &gt; "%temp%\OEgetPrivileges.vbs" 
  ECHO UAC.ShellExecute "!batchPath!", "ELEV", "", "runas", 1 &gt;&gt; "%temp%\OEgetPrivileges.vbs" 
  "%temp%\OEgetPrivileges.vbs" 
  @exit /B 
  
  :gotPrivileges 
  </code></pre>
  
  <hr>
  
  <p>Thanks guys
  Rune</p>