Batch basics

From wikinotes

comments

REM lines beginning with 'REM' are comments
REM (they cannot be indented)

echo

echo is the batch's print statement. It does not require quotes.

echo

echo with a newline is technically possible using !LF!^, but don't do it. just don't.

echo your message

@echo off

By default, every command in a batch script is printed in addition to it's output. you can disable this using @echo off

@echo off

%cd%

%cd% is the equivalent to pwd in unix. (the current working directory).

escape character

The \ character is used for paths in dos, so it uses the caret character in it's place.

REM escape non-% characters
^"

REM escape a %
%%

pause

REM 'pause' waits for a keypress before continuing
pause

goto

goto statements have over time become recognized as a bad idea. But they are the primary way of controlling the flow of code in batch. Use them as a last resort.

:Label1
echo "things" && echo "things"
goto Label1

filepath expansion

Technically, this is documented under the for loop's documentation. (see for /?).


set I=C:/path/to/some/file

%~I         - expands %I removing any surrounding quotes (")
%~fI        - expands %I to a fully qualified path name
%~dI        - expands %I to a drive letter only
%~pI        - expands %I to a path only
%~nI        - expands %I to a file name only
%~xI        - expands %I to a file extension only
%~sI        - expanded path contains short names only
%~aI        - expands %I to file attributes of file
%~tI        - expands %I to date/time of file
%~zI        - expands %I to size of file
%~$PATH:I   - searches the directories listed in the PATH
               environment variable and expands %I to the
               fully qualified name of the first one found.
               If the environment variable name is not
               defined or the file is not found by the
               search, then this modifier expands to the
               empty string

https://stackoverflow.com/questions/5034076/what-does-dp0-mean-and-how-does-it-work

clipboard

copy output to clipboard

echo "some text" | clip

prompt geometry

get size of cmd prompt.

mode con