Convert WAV to MP3

Assuming you have Lame for Audacity installed:

"C:\Program Files (x86)\Lame For Audacity\lame.exe"
  --preset standard 
  --ta "THE_ARTIST" 
  --tt "THE_TITLE" 
  "INPUT_FILE.wav" 
  "OUTPUT_FILE.mp3"

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...

Shrink image with ImageMagick

@ECHO OFF
SETLOCAL
FOR /R %%G IN (*.jpg) DO CALL :process "%%G"
GOTO :end

:process
    SET _inname=%1
    SET _outname=%_inname:~0,-5%_1024.jpg"

    identify -format %%w %_inname% >width.txt
    identify -format %%h %_inname% >height.txt

    set /p width=<width.txt
    set /p height=<height.txt
    DEL width.txt
    DEL height.txt
    ECHO Processing %_inname% ...

    if %width% gtr %height% call :landscape %_inname% %_outname%
    if %height% geq %width% call :portrait %_inname% %_outname%
    rem convert %_inname% %_inname%
    EXIT /B

:landscape
    convert %_inname% -resize 1024x -quality 90 %_outname%
    EXIT /B

:portrait
    convert %_inname% -resize x1024 -quality 90 %_outname%
    EXIT /B

:end

Add timestamp to photo using ImageMagick

In a Windows batch file:

    rem dptnt.com/2009/04/how-to-add-date-time-stamp-to-jpeg-photos-using-free-software/

    @ECHO OFF
    SETLOCAL
    FOR /R %%G IN (*.jpg) DO CALL :process "%%G"
    GOTO :end

    :process
     SET _inname=%1
     identify -format %%w %_inname% >dttmpfile
     set /p width=<dttmpfile
     Set /a pointsize=%width%/50
    rem  echo ZZ >> dttempfile
     DEL dttmpfile
     ECHO Processing %_inname% ...
     convert %_inname% -gravity SouthEast -font Arial -pointsize %pointsize% -fill orange -annotate +%pointsize%+%pointsize% "%%[exif:DateTimeOriginal]" %_inname%
     EXIT /B
    :end