Shell

Basics

@rem Suppresses the display of each command on the screen as it is executed
@echo off

@rem Clear screen
@cls

@rem Print a message
@echo This is a message

@rem Block until the user press a key
pause

Arguments and Variables

@echo Argument one is: %1
@echo Argument two is: %2
...
rem Conrol
if .%1.==.. goto usage
...
:usage
...

@set variable=World
@echo Hello %variable%

rem Add a path to the environmental variable PATH
PATH %PATH%;mypath

Built-in variables

Variable Description
%CD% Expands to the current directory.
%DATE% Expands to the same output as typing DATE.
%TIME% Expands to the same output as typing TIME.
%RANDOM% Generates a random integer in the range of 0 - 32767.
%ERRORLEVEL% Expands to the current ERRORLEVEL.
%CMDCMDLINE% Expands to the original command line that invoked the Command Processor.

Frequently Asked Questions Regarding The Windows 2000 Command Processor

Control Flow

if condition dosomething
...
if not condition dosomething
...
rem Check equality
if %a% == %b% dosomething
...
goto label
...
:label
...
rem For loop
for %%f in (*.java) do javac %%f
...
for %%d in (dir_a,dir_c,dirb) do dir %%d:
...
rem Call a second script 
call batfile

Background Execution and Error Level

Handling of the return code from a program

program_name
if errorlevel 1 goto error 
...
goto end
:error
echo Program error
:end

To run a program in the background (& equivalent):

start program_name

Files and Directories

rem Check for existence
@if exist filename dosomething

rem Change Directory
cd directory

rem Make Directory
mkdir

rem Copy source to target
copy source rtarget

rem Remove filename
del filename

windows/shell.txt · Last modified: 2010/08/10 (external edit)
CC Attribution-Noncommercial-Share Alike 3.0 Unported
Valid CSS Driven by DokuWiki Recent changes RSS feed Valid XHTML 1.0