Skip to content
Search

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

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.

help cmd

Detailed Command Prompt commands can be listed with the following command.

help

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

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.

set "VAR_NAME=<String With Spaces>"

Split command

To split a long command, use the following.

copy.exe file1.txt ^
    file2.txt

Arithmetic

For integer arithmetic, add the /A flag to the SET command.

set a=123
set b=456
set /a c=a+b
echo %c%

Get STDOUT into 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

dir | clip

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

[HKEY_CURRENT_USER\Console\%SystemRoot%_system32_cmd.exe]
"CodePage"=dword:fde9

Command Prompt:

REG ADD HKCU\Console /v CodePage /t REG_DWORD /d 0xfde9

PowerShell:

sp -t d HKCU:\Console CodePage 0xfde9

Cygwin:

regtool set /user/Console/CodePage 0xfde9

Tips

  • The %ComSpec% environment variable holds the full file path of cmd.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 off in sh or [[bash], use stty -echo. (For echo on, use stty echo)
  • If you want to use the ls command, doskey ls=dir gives a similar effect.
    • Enter doskey ls=dir as a string (REG_SZ) at the registry path HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor\AutoRun.
  • Use %* for all passed arguments, and access each argument with %1, %2, ... and so on.
  • As a replacement for the tail command (-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 & to AND when a file name contains it.
Cmd-blackhole-bat-sample.zip
Sample batch file used in the Blackhole project.

See also

Favorite site

Sample site

References


  1. Batchfile_for-blog_daum_net_idrlee_16101024.zip ↩

  2. Www_robvanderwoude_com.zip ↩

  3. Www_robvanderwoude_com.zip ↩

author Zer0 sitemap