Batch file get admin rights

If you want to execute a batch file, but prompt the user to run it as administrator rather than making them run it as administrator upfront:

@echo off

:: BatchGotAdmin
:-------------------------------------
REM  --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"

REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
    echo Requesting administrative privileges...
    goto UACPrompt
) else ( goto gotAdmin )

:UACPrompt
    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
    set params = %*:"=""
    echo UAC.ShellExecute "cmd.exe", "/c %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"

    "%temp%\getadmin.vbs"
    del "%temp%\getadmin.vbs"
    exit /B

:gotAdmin
    pushd "%CD%"
    CD /D "%~dp0"
:--------------------------------------

<YOUR BATCH SCRIPT HERE>

Batch file create date parameter

Often I use a batch file to format folders or files based on the current time. This will set a variable that is the current date/time formatted as YYYYMMDD-HHMMSS:

@echo off

rem --------------------------------------------
rem ------- SET UP DATETIME STAMP VARIABLE -----
rem --------------------------------------------
SET HOUR=%time:~0,2%
SET dtStamp9=%date:~-4%%date:~4,2%%date:~7,2%-0%time:~1,1%%time:~3,2%%time:~6,2% 
SET dtStamp24=%date:~-4%%date:~4,2%%date:~7,2%-%time:~0,2%%time:~3,2%%time:~6,2%

if "%HOUR:~0,1%" == " " (SET dtStamp=%dtStamp9%) else (SET dtStamp=%dtStamp24%)

rem ECHO %dtStamp%
rem --------------------------------------------

Batch file Pop-Up

Description

If you’re working in a batch file, and need some kind of user interaction in a popup window, here is a really messy and ugly way of doing it:

<!-- :: Batch section
@echo off
setlocal

echo Are you sure? 
for /F "delims=" %%a in ('mshta.exe "%~F0"') do set "HTAreply=%%a"

if "%HTAreply%" == "y" goto :dowork
if "%HTAreply%" == "Y" goto :dowork

goto :eof

:dowork

copy /y c:\temp\input.txt c:\temp\output.txt

goto :eof

-->

<!DOCTYPE html>
<html>
  <head>
    <hta:application border="none" scroll="no">
    <style type="text/css">
      body { width: 300px; height: 300px; margin-left: 10%; margin-right: 10%; color: white; background-color: #009; }
    </style>
    <script>
       function closeHTA(reply) {
         var fso = new ActiveXObject("Scripting.FileSystemObject");
         fso.GetStandardStream(1).WriteLine(reply);
         window.close();         
       }
    </script>
  </head>
  <body>
    <p>Are you sure?</p>
    <input type="button" onclick="closeHTA('Y');" value="Yes">
    <input type="button" onclick="closeHTA('N');" value="No">
  </body>
</html>

Batch file basics

Disable echo:

@echo off
Loops over fixed values:
for %%a in (a b c) do (
    echo %%a
)

Variables:

set somevar=myvalue
echo %somevar%

echo %temp%

echo first param=%1
echo second param=%2

Check file existence:

if exist somefile.ext del somefile.ext
if not exist somefile.ext echo no file

Check variable existence:

if defined somevar echo somevar=%somevar%

Numeric comparison

if 24 gtr 30 (echo GREATER) else echo LESSEQ
rem EQU equal to
rem NEQ not equal to
rem LSS less than
rem LEQ less than or equal to
rem GTR greater than
rem GEQ greater than or equal to

Prompt:

set /p name=What is your name? 
echo Your name is %name%

Labels:

goto :mylabel

echo blah blah

:mylabel
echo Just skipped blah blah

Numeric expression in variable:

set /a num=0
:numbers
set /a num=%num%+1
if %num% EQU 10 (goto :next) else (echo %num%)
goto :numbers

:next
echo done
Pause:
pause

Pass all arguments to another executable:

set args=%1
shift
:start
if [%1] == [] goto done
set args=%args% %1
shift
goto start

:done

.\SomeExecutable.exe %args%

String comparison:

@echo off

set /p ans=Are you sure? 

if "%ans%" == "y" goto :dowork
if "%ans%" == "Y" goto :dowork

goto :eof

:dowork

echo Work is being done...