Joshua's Cheatsheets
Light
help

Batchfile / bat / Windows CMD - Cheatsheet

Word of advice: When PATH doesn't seem to be working (recent updates not showing) there is no harm in restarting, and it often fixes the issue.

Resources

What Type Link
SS64 CMD Reference Docs A-Z CMD Index

How to comment?

REM # My Comment

Using variables

SET MYPATH="C:\Users\Joshua"
cd %MYPATH%

Joining string vars

Same as bash, you can just put them together:

SET strA=Hello
SET strB=World
SET final=%strA% %strB%
echo %final%

Hello World

How to get system and user vars

  • Get path

    • PATH
    • More readable version

  • Getting user variables

    • can be accessed just by using %varName% syntax
    • echo %JAVA_HOME%

Tasklist

  • Find process by exe name

    • tasklist /FI "IMAGENAME eq Greenshot.exe"

Output text to file

  • Redirect operator

    • ipconfig /all > output.txt
  • Double redirect for append mode

    • ipconfig /all >> existingOutput.txt

File Management

Copy files

There are a bunch of built-in tools. In order of online recommendations:

List all files by size (recursive subdir too)

(@For /F "Delims=" %A in ('dir /B/S/A-D') Do @Echo %~fA %~zA) >filelist.txt

List file tree

  • For both files and folders

    • tree /F
  • Pipe to file - use /A for ascii output else file will be garbled

    • tree /F /A > filestructure.txt

Delete files of a certain type within current folder and all subfolders

Batch:

DEL /S *.docx

REMINDER - even though there is no wildcard at the ends, it still acts like one for some reason. So DEL /S *.qml will delete all .qml files, but also all .qmlc files - see https://stackoverflow.com/a/32399927

Powershell:

Remove-Item * -Include *.qml -Recurse | Remove-Item

NOTE - Powershell does not suffer from the above issue - .qml will match .qml and not .qmlc

Logic

IF / Else

The basics:

If (condition) (do_something) ELSE (do_something_else)

Make sure logic switches are on same line - https://ss64.com/nt/else.html

This Won't work:

IF EXIST %MyFile% (
    echo "found"
)
ELSE (
    echo "not found"
)

This should work:

IF EXIST %MyFile% (
    echo "found"
) ELSE (
    echo "not found"
)

ELSE IF

Else If can be problematic with .bat batch scripts. Might work in CMD, but for BAT, seems most people recommend nested IFs as alternative, or GOTOs. Worked fine for me though, with Win 8

Using the current directory you are in as a variable

  • Simple!

    • echo %cd%
  • Works great for opening current folder with various programs

    • "C:\Program Files\Microsoft VS Code\Code.exe" %cd%
    • Explorer.exe %cd%

Keeping file open after execution

Add this to very end of file:

PAUSE

Input

Asking for input

You can pause your script and ask for input by using this:

set /P {variableName}="Prompt String"
REM Access by using %{variableName}%

Example:

set /P name="What is your name? "
echo Good day, %name%!
PAUSE

File permissions

You can use TAKEOWN to take ownership of files.

Example:

REM # Folder contents is foo.txt and subfolder ./pics.
REM # We need to take ownership of ./pics
takeown /r /f pics

Symlinking

On Win, you can use MKLink (ss64) to create a symlink to a directory or file.

  • Link to a file:

    • MKlink {symbolicFileName} {realTargetFile}
  • Link to a directory / folder

    • MKlink /D {symbolicDirName} {realTargetDir}

Creating a symlink requires an elevated (with admin rights) console. (Unless using dev mode on Win 10+)

In general, it is best / easiest to always use absolute paths for the targets. You might want to wrap paths in quotes if you are having issues.

Evaluating symlinks

If you want to check what a symlink maps to (or just see if there are any in the current dir), you can use:

dir /a, or for just links, dir /aL

Markdown Source Last Updated:
Sat Nov 16 2019 01:58:06 GMT+0000 (Coordinated Universal Time)
Markdown Source Created:
Mon Aug 19 2019 17:06:24 GMT+0000 (Coordinated Universal Time)