Command Prompt
lastmod 2021-05-21created 2014-12-0427 edits
Command Prompt (executable name: cmd.exe) is the command-line interpreter on OS/2, Windows CE, and Windows NT-based systems (Windows 2000/XP/2003/Vista and later). It is the analog of COMMAND.COM on MS-DOS and Windows 9x systems, or of the shells used on Unix systems.
Categories
Example
- Boost:WindowsBuildScript: How to build Boost on Windows.
Default
If you want some command to run automatically as soon as Command Prompt starts, modify the registry entries below (if they don't exist, add a 'String Value').
-
HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\AutoRun -
HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun
The above can be checked with the following command.
Detailed Command Prompt commands can be listed with the following command.
Terms
- Wildcard character
- A wildcard character is a symbol used to specify several files at once when issuing a particular command on a computer. The asterisk (
*), question mark (?), and others are used.
Syntax
Describes the syntax.
FOR /F Options
- Explanation of FOR /F options (en)
- Correct use of the FOR statement in DOS batch files
- Explanation of the for statement in batch files 1
-
tokens= - The FOR statement parses each line, and tokens specifies which string of each line to pass. The default delimiter between strings is whitespace.
-
delims= - Tokens are separated by whitespace by default, but if you specify delimiter characters after delims, they are used instead.
-
skip= - Determines how many lines from the top to ignore. If the token you need is on the third line, you can ignore the first two lines. In that case, enter skip=2.
-
usebackq - This option is used when putting double quotes around a file path that contains spaces.
Example
-
chcp 65001 - Outputs UTF-8 in the console window. For reference, it stands for change code page.
-
for %%i in (D:\temp\*.png) do move /y %%i %%~di%%~pi%%~ni@2x%%~xi - Renames files in bulk.
-
for /r %%i in (*.zip) do md5sum %%~ni%%~xi > %%~ni.md5 - Saves the md5 hash of every zip file.
Environment variables containing spaces
Can be used as follows.
Split command
To split a long command, use the following.
Arithmetic
For integer arithmetic, add the /A flag to the SET command.
Get STDOUT into a variable
- Stackoverflow: Get STDOUT into a variable
- Stackoverflow: Windows batch assign output of a program to a variable
The way to put stdout output into a variable is as follows.
for /f "tokens=*" %%a in ('[sendmail command line]') do (
set VAR=%%a
)
:: OR
application arg0 arg1 > temp.txt
set /p VAR=<temp.txt
:: OR
for /f %%i in ('application arg0 arg1') do set VAR=%%i
Copy Clipboard
DATE and TIME
To add the current date and time to a file name, do the following.
set RN_DATE=%date:~0,4%%date:~5,2%%date:~8,2%
set /A RN_TIME_HOUR=%time:~0,2%
if %RN_TIME_HOUR%==0 set RN_TIME_HOUR=00
set RN_TIME=%RN_TIME_HOUR%%time:~3,2%%time:~6,2%%time:~9,2%
set RN_NOW=%RN_DATE%-%RN_TIME%
echo %RN_NOW%
Change CMD CodePage
How to change the default cmd code page to UTF-8: UTF-8 CodePage: 65001
Command Prompt:
PowerShell:
Cygwin:
Tips
- The
%ComSpec%environment variable holds the full file path ofcmd.exe. - To print a blank line, use
ECHO.. - Putting @ in front of a command suppresses the ECHO of that command.
- For reference, to do
echo offin sh or [[bash], usestty -echo. (Forecho on, usestty echo)
- For reference, to do
doskey ls=dir gives a similar effect.- Enter
doskey ls=diras a string (REG_SZ) at the registry pathHKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor\AutoRun.
%* for all passed arguments, and access each argument with %1, %2, ... and so on. -f) of Bash, use something like cat filename.txt -Wait -Tail 1000 in PowerShell. Sample Download
Lists Windows command prompt (CMD) scripts that can be referenced and reused.
- Cmd-geosoft-korail-ftp-update.zip
- Automatically finds and downloads the latest update data from FTP.
- Cmd_file_and_rename.zip
- Converts
&toANDwhen a file name contains it.
- Cmd-blackhole-bat-sample.zip
- Sample batch file used in the Blackhole project.
See also
- DOSBox
- COMMAND.COM
- Shell
- Bash
- PowerShell
Favorite site
- Wikipedia (en) cmd.exe
- Microsoft Windows XP CMD
- MSDN: Command Prompt: frequently asked questions
- DOS - String Manipulation
- Tips for writing Windows BATCH files
- [Recommended] Rob van der Woude's Scripting Pages 2
Sample site
- Writing text into a file with a batch file
- Compressing each of several files in a folder with a batch file
- Making a manual choice in a batch file
- Solving special characters in html batch files
- Batch file commands
- Automatically connecting network drives
- Automatic IP change script
- Simple repetitive IP changes using a Bat file
- Windows console commands
- Compressing each of several files in a folder with a batch file
- Folder branching
- Batch file for getting the date and time
- Creating a folder named with today's date in a batch file
- Batch file: automatic processing with bat files 1