Skip to content

Instantly share code, notes, and snippets.

@Skhmt
Last active April 1, 2024 19:41
Show Gist options
  • Save Skhmt/3988a697658364f53fe10ec9d85e484b to your computer and use it in GitHub Desktop.
Save Skhmt/3988a697658364f53fe10ec9d85e484b to your computer and use it in GitHub Desktop.
Reference for writing .bat/.cmd files in windows

Windows batch (.bat/.cmd) reference

Some other resources:

Flow control examples

SET foo="bar"
if "%foo%"=="bar" (goto isbar) else (goto isnotbar)
echo.

:isbar
echo foo is bar
goto endbar

:isnotbar
echo foo is not bar

:endbar
:choicestart
SET /p choice="Enter 1: "
if NOT %choice%==1 (goto choicestart)

Variable reference

  • Current working directory: %cd%
  • Path to the batch file: %~dp0
  • Command line parameters of the batch file: %1 - %9

%cd% and %~dp0 start the same when not running the script as administrator. If running the script as administrator, %cd% gives the System32 diretory while %~dp0 always gives the location of the batch file.

Change directory to the one containing the script

pushd "%~dp0"

Use pushd because it accepts network paths (UNC) while cd does not.

This command is necessary in general because when .bat files are run as admin, the path is set to C:\windows\system32\.

%0 accesses the first parameter which is the full path of the bath file, ~ is for parameter extensions, d is the drive letter only, and p is the path only, so combining them into %~dp0 gives the drive and path but not the file/script name.

See: https://ss64.com/nt/syntax-args.html

Zip/unzip

Win 10 1903 (Jan 2018) or later

Unzip: tar -xf "myfile.zip"

Zip: tar -caf "myfile.zip" a.txt b.txt

Create a zip of all .txt files: tar -caf "myfile.zip" *.txt

Backup via running powershell in cmd

Unzip: powershell Expand-Archive myfile.zip output/ -Force

Zip: powershell Compress-Archive -path mypath -destinationpath -mydestpath

Download a file

Win 10 1903 or later: curl "https://.../file.zip" -o myfile.zip

Download (can be run in a cmd): powershell Invoke-WebRequest -Uri "https://file-examples-com.github.io/uploads/2017/02/file_example_JSON_1kb.json" -OutFile c:\test.json

Copy all files from one location to another (in this example, up one directory)

robocopy .\currentFolder\ .\ /E /MOVE /NFL /NDL /NJH /NJS /NC /NS /NP

Restart the batch file (maybe something in the PATH changed)

start "" "%~f0"

Check if something is in the PATH

for %%X in (wt.exe) do (set FOUNDWT=%%~$PATH:X)
if defined FOUNDWT (goto found) else (goto notfound)

Open a website in default browser

start https://www.google.com

Run a commandline command in this window

start "" /wait /b ping www.google.com

Get file hash

certutil -hashfile myfile.zip MD5

MD4, MD5, SHA1, SHA256, and SHA512 are allowed

To get only the hash returned: certutil -hashfile myfile.zip MD5 | findstr /V ":" as the only line without a colon is the hash

If certutil is not available: powershell Get-FileHash ./myfile.zip -Algorithm MD5

MD5, SHA1, SHA256, SHA384, and SHA512 are allowed

Rename all files to a different file type

ren *.rar *.cbr

Take control of all files in directory (after reinstalling Windows for example)

takeown /f %1 /r /d y
icacls %1 /grant administrators:F /t

Put every .mp3 file in the folder and all sub folders into a .m3u playlist

for /R %%X in (*.mp3) do (echo %%X >> playlist.m3u)

Turn off echoing every command

@echo off

The @ makes the current command also silent; echo off turns off all subsequent echoes, but not itself

Write an empty line to a text file

echo. >> out.txt

Note the period after echo - echo.

Write the contents of a text file to another text file

append to the end or create if it doesn't exist: type "filename.txt" >> out.txt

overwrite or create if it doesn't exist: type "filename.txt" > out.txt

Write comments

:: this is a comment

Alternatively, rem this is a comment

Wait/sleep

For 9 seconds

Vista or later: timeout /t 9 /nobreak

XP: ping 127.0.0.1 -n 9 > NUL

For 500ms

ping 127.0.0.1 -n 1 -w 500 > NUL

This is the lowest amount of time you can wait without a third party application

Wait for user to press any key before closing (useful when intended to be run by double-clicking)

pause

Redirecting stdout and stderr

[command] >NUL - pipe stdout to null

[command] 2>&1 - pipe stderr to stdout

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment