TABLE OF CONTENTS (HIDE)

Windows Basics

CMD Shell & Others

For beginners, read "Programmer's Survival Guide for Windows - File System and CMD Shell".

Frequently-used Commands

General Notes: The commands are NOT case-sensitive (because the legacy DOS is not case-sensitive). In general, Windows/DOS Operating System is not case-sensitive, but Unixes are. Most of the programming languages such as C/C++/Java, with origin in Unix, are case-sensitive.

Help

Help: To list all the available commands, for example,

Prompt> help
For more information on a specific command, type HELP command-name
ASSOC          Displays or modifies file extension associations.
ATTRIB         Displays or changes file attributes.
BREAK          Sets or clears extended CTRL+C checking.
......

To get more information on a particular command, enter "help command-name", for example,

Prompt> help cd
Displays the name of or changes the current directory.
.......

Directory- and File-related Commands

DirectoryStructure.png

Windows Operating System organizes files in drive and directory. A drive is identified by a letter followed by a colon, e,g,. "C:", "D:" and "I:". (Unix does not have the concept of drive.) Directory exhibits a tree-like structure starting from the so-called root directory, denoted by a leading back slash '\'. Each directory may contain sub-directories or files. Sub-directories may contains sub-sub-directories or files. The sub-directories are separated by a back slash '\' (Unix uses forward slash '/' as the directory separator). Directory is also called a folder or a path. An example is illustrated below:

In the above example, the full-path filename for "Hello.java" is "D:\java\beginner\Hello.java", which begins with the drive letter "D:", followed by root directory "\", sub-directories (separated with '\') and filename. The filename composes of two parts, name and extension, separated by a dot '.'. The file extension specifies the type of the file (e.g., ".txt" for text file, ".doc" for Word document, ".java" for Java source file), which can be associated with a processing program. (Unix does not have the concept of file extension.)

Current Drive and Current Working Directory (CWD) refer to the drive and the directory that you are currently working on. They are shown as part of the command prompt preceding ">". For example,

D:\java\beginner>

In the above example, the current drive is "D:", and the current working directory is "\java\beginner".

To set or change the current drive, enter the drive letter followed by a colon, for examples,

C:\Users\john> D:
D:\> C:
C:\Users\john> D:
D:\> J:
The system cannot find the drive specified.
Change current drive to D:
Change current drive to C:
Change current drive to D:
Change current drive to J:
[error message]

To set or change the current working directory, use the cd (change directory) command. You can use "\" to refer to the root directory of the current drive, ".." to refer to the parent directory, and "." to refer to the current directory. You can use absolute path which begins with the root directory (with a leading back slash), or relative path (without the leading back slash) which is relative to the current working directory. For examples,

C:\Users\john> D:
D:\> cd java
D:\java> cd beginner
D:\java\beginner> cd \java\advanced
D:\java\advanced> cd ..
D:\java> cd \
D:\> C:
C:\Users\john>
Change current drive to D:
Change directory to "java", relative to root "\"
Change directory to "beginner", relative to "\java"
Change directory to "\java\advanced" (absolute) 
Change directory to parent directory (relative)
Change directory to root (absolute)
Change current drive to C:
[at the current working directory of drive C:]

The command cd (without parameter) display the current working directory.

Take note that you have to set the current drive and current working directory in two separate operations.

You can use dir (directory) command to list the contents of the current working directory. For examples,

D:\java\beginner> dir
 Volume in drive D is Work
 Volume Serial Number is xxxx-xxxx
  
 Directory of D:\java\beginner
  
xx-xxx-xx  xx:xx xx    <DIR>          .
xx-xxx-xx  xx:xx xx    <DIR>          ..
xx-xxx-xx  xx:xx xx               417 Hello.class
xx-xxx-xx  xx:xx xx               118 Hello.java
               2 File(s)            535 bytes
               2 Dir(s)  xx,xxx,xxx,xxx bytes free

The output lists all the sub-directories and files contained in the current working directory. It also contains two special entries: ".." for the parent directory, and "." for the current directory.

You can use special wildcard characters for filename filtering or matching. The wildcard character '*' matches zero or more (any) characters. The wildcard character '?' matches exactly one (any) character. For examples,

Prompt> dir *.java
Prompt> dir H*
Prompt> dir Hello?.java
List files ending with .java
List files beginning with "H"
List files beginning with Hello, followed by any (one) character, ending with .java

Other frequently-used directory- and file-related commands include:

  • del filenames: Deletes files.
  • ren current-filename new-filename: Renames a file.
  • mkdir directory-name: Creates (or make) a sub-directory under the current working directory.
  • rmdir directory-name: Removes or deletes the sub-directory.
  • copy filename new-filename: Copies file.
  • xcopy|robocopy: Copies (or Robust copies) files and directory trees.

You could issue "help command-name" to check the syntax and options of the above commands.

Alternatively, you can use Windows Explorer (My Computer) to perform the above tasks, graphically.

Environment and Local Variables

Environment variables are global system variables accessible by all the processes running under the Operating System. Environment variables are useful to store system-wide values such as the directories to search for the executable programs, the OS version, and the location of Windows binaries. (Windows registry was later created to store the ever-growing data needed for the applications in a more efficient manner.)

A process could access all the environment variables, it could also maintain its own local variables, which is available to itself only.

READ "How to set or change a variable".

PATH Environment Variable

READ "PATH (For Windows Users)"

Miscellaneous Commands

  • cls (clear screen): Clears the current screen and remove all the messages.
  • type filename: Types out (or displays) the content of the file.

Redirection Operators and Filtering Commands

By default, the output of a command goes to the screen (called STDOUT), and the input of a command comes from the keyboard (called STDIN). You can use a redirection operator to redirect input and output from/to a file or another command:

  • > (output redirection): Writes the output to a file (or a device such as printer), instead of the screen (STDOUT).
  • >> (output append redirection): Appends the output to a file, instead of the screen.
  • < (input redirection): Reads the input from a file or a device, instead of the keyboard (STDIN).
  • | (pipe): Pipes the output of one command into the input of another command.

An output redirector '>' involves a program and a sink (destination). An input redirector '<' involves a program and a source. A pipe '|' involves two programs.

These filtering commands work with redirection operators for specifying the input and output:

  • find string: Searches for the specified string.
  • sort: Sort the lines alphabetically.
  • more: Displays one screen at a time and wait for a key-press to display the next screen. (It is useful in the old days when screen output is not buffered, but I find it annoying nowadays.)

For examples,

Prompt> more < abc.txt
Prompt> find "Hello" < abc.txt
Prompt> find "Hello" < abc.txt > out.txt
Prompt> find "Hello" < abc.txt | more
Prompt> sort < xyz.txt
Prompt> find "John" < xyz.txt | sort > out.txt
Display the given file one screen at a time.
Find the specified string from the given file.
Find the specified string and write the results to another file.
Display the result one screen at a time.
Sort the lines alphabetically, output to screen.
Find the string from file, sort the output, write result to file.

Notes: Pipe '|' is used to pipe into another command (or process); output redirection '>' is used to send the output to a file (or device such as printer) instead of STDOUT.

More on Windows' File System

Permissions

[TODO]

Symbolic Link (SymLink)

In Windows Vista/7/8, you can create a symlink to a file or a directory (similar to Unix), via command "mklink". Symlink could provide great convenience in many situations.

You need the "Create Symbolic Link" privilege to run "mklink" command, which only the administrators possessed by default. Start a CMD shell with administrator right ("run as administrator") to run the "mklink" command..

> help mklink
 
// Make a directory symlink called mysql to mysql-5.0.28-win32
> mklink /D mysql mysql-5.0.28-win32
symbolic link created for mysql <<===>> mysql-5.0.28-win32
 
> dir
10/14/2012  12:45 AM    <SYMLINKD>     mysql [mysql-5.0.28-win32]

Symlink is different from shortcut, which was available in the earlier Windows.

CMD Shell Language and Batch Files

A batch file (also called a batch program or shell script) is a executable program that contains a sequence of commands. A batch file has a file extension of ".bat". Batch files, which employ a legacy scripting language, possesses very simple, primitive and clumsy syntax . For examples,

  • The names and identifiers are not case-sensitive.
  • All variables belong to the type string. There is no numeric type, and no arithmetic operations.
  • Only simple and primitive flow control constructs (if-else, goto, and call) are supported. Non-structured goto's are used extensively, which make the script hard to read and understand. (A for construct is available to process each file from a set of files.)

The primary purpose of batch files is to automate a series of commands, instead of general programming. Use a general-purpose programming language for programming. There are also many better and more powerful scripting languages, such as Perl, Python, Java Script, Java FX Script, VB Script, among other.

Batch Commands

The frequently-used commands in the batch files are:

  • rem message: A programming comment (or remark) to provide explanation, non-executable.
  • pause: Suspends execution of the batch program and displays the message "Press any key to continue . . ."; resumes execution after a key is pressed.
  • echo message: Displays the specified message. To display a blank line, use "echo.".
  • echo on|off: When echo is on, all batch commands are echoed on the screen. When echo is off, only echo commands are displayed. @echo off disables display for this echo command and subsequent batch commands. Echo is usually turned on during program testing and development, and turned off in production.
  • echo %variable-name%: Displays the value of an environment variable.
  • ctrl+c or ctrl-break: to stop the batch file.
  • ctrl-s or pause-key: to suspend the batch file.

Variables

All the variables belong to the type string. The names and commands are not case-sensitive.

In a batch file, you can reference an environment variable using the syntax %variable-name%. For example, echo %USERNAME%.

The command-line arguments can be referenced via variables %0 to %9 from right-to-left, where %0 is the name of the batch file. For example,

Prompt> process -o abc.out abc.in

The above command invoke a batch file Process.bat with command-line arguments. The command-line arguments can be referenced inside the batch file as follows: %0=Process, %1=-o, %2=abc.out, and %3=abc.in. To reference more argument, you need to use the command shift, which shifts the value of %1 into %0, %2 into %1, ..., %9 into %8, and the next argument into %9.

setlocal and endlocal: The command setlocal begins localization of environment changes in a batch file. That is, changes to environment variables made after setlocal are local to this batch file. The command endlocal restores the previous environment settings. An endlocal is issued implicitly at the end of a batch file.

Program Control Constructs

if [not] exist filename command
if [not] "string1"=="string2" command
if [not] errorlevel n command
 
if [not] ... (command1) else (command2)
Execute command if [if not] the filename exists
Execute command if [if not] string1 equals to string2
Execute command if [if not] error-level is equal or greater than n
(Error code is often set to 0 if no error occurs, 1 and higher otherwise.)
if exist a.exe (echo yes) else (echo no)
goto label-name
:label-name
Jump to the command at the specified label-name
Define a label as target for goto
for variable in (set) do command
Examples:
for %f in (*.doc *.txt) do type %f
Runs a specified command for each file in a set of files
 
type out all .doc and .txt
call filename arguments
start program-name
program-file
prompt message
Call another batch file or program with arguments, and returns to this batch file
Start a separate cmd session to run a specified program or batch file.
Terminate this batch file, and start the specified program (or batch file)
Put up the prompting message.

The command call can be used to call another batch file or program. The control returns to the next statement following the call command, after the called program is completed (similar to calling a subroutine in programming languages). For example, suppose "process.bat" is to be used in a batch file. "call process" executes the "process.bat", and returns control to the next statement. On the other hand, "process" alone (without call) terminates the current batch file and executes the "process.bat".

Examples

Below is a sample batch file extracted from "startup.bat" for starting the Tomcat server. The script checks if environment variable CATALINA_HOME is defined, else set it to the current directory or the parent directory, which contains "bin\catalina.bat". It then packs all the command-line arguments into variable CMD_LINE_ARGS, and call "catalina.bat start CMD_LINE_ARGS".

@echo off
if "%OS%" == "Windows_NT" setlocal
 
rem Start script for the CATALINA Server
 
rem Guess CATALINA_HOME if not defined
set CURRENT_DIR=%cd%
if not "%CATALINA_HOME%" == "" goto gotHome
set CATALINA_HOME=%CURRENT_DIR%
if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome
cd ..
set CATALINA_HOME=%cd%
cd %CURRENT_DIR%
 
:gotHome
if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome
echo CATALINA_HOME environment variable is not defined correctly
goto end
 
:okHome
set EXECUTABLE=%CATALINA_HOME%\bin\catalina.bat
if exist "%EXECUTABLE%" goto okExec
echo Cannot find %EXECUTABLE%
goto end
 
:okExec
rem Get remaining unshifted command line arguments
set CMD_LINE_ARGS=
:setArgs
if ""%1""=="""" goto doneSetArgs
set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1
shift
goto setArgs
 
:doneSetArgs
call "%EXECUTABLE%" start %CMD_LINE_ARGS%
 
:end
turn off message display
localized environment
 
 
 
 
%cd% gives the current directory
if CATALINA_HOME defined, goto gotHome
else set to current directory
if startup batch file found, goto okHome
else set CATALINA_HOME to the parent directory
 
 
 
check CATALINA_HOME 
 
 
 
 
CATALINA_HOME defined
 
 
 
 
 
"catalina.bat" found
 
 
 
 
append next argument behind
 
 
 
ready to start the server
"catalina start cmd_line_args" 
 
 

REFERENCES & RESOURCES

  1. Microsoft MS-DOS User's Guide and Reference.
  2. "Microsoft XP Professional Product Documentation - Command-line reference", [online].
  3. "Microsoft XP Professional Product Documentation - Using batch file", [online].
  4. "Command-line Reference for IT Pros - Technical Reference", available under Windows "Help".