LEARN COMMAND LINE AND
BATCH SCRIPT FAST
D Armstrong
1
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.
BCDEDIT --- Sets properties in boot database to control boot loading.
CACLS --- Displays or modifies access control lists (ACLs) of files.
CALL --- Calls one batch program from another.
CD --- Displays the name of or changes the current directory.
CHCP --- Displays or sets the active code page number.
CHDIR --- Displays the name of or changes the current directory.
CHKDSK --- Checks a disk and displays a status report.
CHKNTFS --- Displays or modifies the checking of disk at boot time.
CLS --- Clears the screen.
CMD --- Starts a new instance of the Windows command interpreter.
COLOR --- Sets the default console foreground and background colors.
COMP --- Compares the contents of two files or sets of files.
COMPACT --- Displays or alters the compression of files on NTFS partitions.
CONVERT --- Converts FAT volumes to NTFS. You cannot convert the current drive.
COPY --- Copies one or more files to another location.
DATE --- Displays or sets the date.
DEL --- Deletes one or more files.
DIR --- Displays a list of files and subdirectories in a directory.
DISKPART --- Displays or configures Disk Partition properties.
DOSKEY --- Edits command lines, recalls Windows commands, and creates macros.
DRIVERQUERY --- Displays current device driver status and properties.
ECHO --- Displays messages, or turns command echoing on or off.
ENDLOCAL --- Ends localization of environment changes in a batch file.
ERASE --- Deletes one or more files.
EXIT --- Quits the [Link] program (command interpreter).
FC --- Compares two files or sets of files, and displays the differences between them.
FIND --- Searches for a text string in a file or files.
FINDSTR --- Searches for strings in files.
FOR --- Runs a specified command for each file in a set of files.
FORMAT --- Formats a disk for use with Windows.
FSUTIL --- Displays or configures the file system properties.
FTYPE --- Displays or modifies file types used in file extension associations.
GOTO --- Directs the Windows command interpreter to a labeled line in a batch program.
GPRESULT --- Displays Group Policy information for machine or user.
GRAFTABL --- Enables Windows to display an extended character set in graphics mode.
HELP --- Provides Help information for Windows commands.
ICACLS --- Display, modify, backup, or restore ACLs for files and directories.
IF --- Performs conditional processing in batch programs.
LABEL --- Creates, changes, or deletes the volume label of a disk.
2
MD --- Creates a directory.
MKDIR --- Creates a directory.
MKLINK --- Creates Symbolic Links and Hard Links
MODE --- Configures a system device.
MORE --- Displays output one screen at a time.
MOVE --- Moves one or more files from one directory to another directory.
OPENFILES --- Displays files opened by remote users for a file share.
PATH --- Displays or sets a search path for executable files.
PAUSE --- Suspends processing of a batch file and displays a message.
POPD --- Restores the previous value of the current directory saved by PUSHD.
PRINT --- Prints a text file.
PROMPT --- Changes the Windows command prompt.
PUSHD --- Saves the current directory then changes it.
RD --- Removes a directory.
RECOVER --- Recovers readable information from a bad or defective disk.
REM --- Records comments (remarks) in batch files or [Link].
REN --- Renames a file or files.
RENAME --- Renames a file or files.
REPLACE --- Replaces files.
RMDIR --- Removes a directory.
ROBOCOPY --- Advanced utility to copy files and directory trees
SET --- Displays, sets, or removes Windows environment variables.
SETLOCAL --- Begins localization of environment changes in a batch file.
SC --- Displays or configures services (background processes).
SCHTASKS --- Schedules commands and programs to run on a computer.
SHIFT --- Shifts the position of replaceable parameters in batch files.
SHUTDOWN --- Allows proper local or remote shutdown of machine.
SORT --- Sorts input.
START --- Starts a separate window to run a specified program or command.
SUBST --- Associates a path with a drive letter.
SYSTEMINFO --- Displays machine specific properties and configuration.
TASKLIST --- Displays all currently running tasks including services.
TASKKILL --- Kill or stop a running process or application.
TIME --- Displays or sets the system time.
TITLE --- Sets the window title for a [Link] session.
TREE --- Graphically displays the directory structure of a drive or path.
TYPE --- Displays the contents of a text file.
VER --- Displays the Windows version.
VERIFY --- Tells Windows whether to verify that your files are written correctly to a disk.
VOL --- Displays a disk volume label and serial number.
XCOPY --- Copies files and directory trees.
WMIC --- Displays WMI information inside interactive command shell.
3
VOLUME 1
[Link]
PROMPT $V$G$S
[Link]
COLOR 17
[Link]
COLOR 0A
TITLE Command Testing
CLS
[Link]
CD "E:\3. Mine\Batch Script\Volume_1"
CLS
[Link]
@echo off
ECHO. & ECHO.
[Link]
@echo off
ECHO. > [Link]
[Link]
@echo off
SYSTEMINFO > [Link]
[Link]
@echo off
VOL >> [Link]
VER >> [Link]
HOSTNAME >> [Link]
[Link]
@echo off
TYPE %[Link] > %[Link]
[Link]
@echo off
SET /p input= Enter text to add to log file:
ECHO At %time% on %date%, %username% logged: %input% >> [Link]
4
ECHO log updated
[Link]
@echo off
SET /p input= Enter command to log results for:
ECHO The command "%input%" returned: >> [Link]
ECHO. >> [Link]
%input% >> [Link]
ECHO. >> [Link]
ECHO log updated
PAUSE
[Link]
@echo off
SET /p input= Enter command to log errors for:
REM run command and save its error text in a temporary file
%input% 2> [Link]
IF %errorlevel% == 0 (ECHO No error logged) ELSE (
REM copy the command and its result to a permanent log file
ECHO At %time%, the command "%input%" return: >> [Link]
TYPE [Link] >> [Link]
REM add blank lines to separate entires
ECHO. >> [Link]
ECHO error log updated
)
REM delete the temparary file's contents
ECHO. > [Link]
PAUSE
[Link]
@echo off
SET /A output = %1 %% %2
ECHO %output%
5
[Link]
@echo off
SET /A output = %1 / %2
ECHO %output%
[Link]
@echo off
SET /A output = %1 * %1
ECHO %output%
[Link]
@echo off
REM get device number
SET /P num= enter device number:
REM calculate 3rd IP number
SET /A IP3 = num / 256
REM calculale 4th IP number
SET /A IP4 = num %% 256
REM output complete IP address
ECHO 1.2.%IP3%.%IP4%
[Link]
@echo off
SET string=%1
ECHO %string:~0,10%
[Link]
@echo off
SET string=%1
ECHO %string:~-10%
[Link]
@echo off
SET string=%1
ECHO %string:~2,1%
6
[Link]
@echo off
SET string=%1
ECHO %string: = %
[Link]
@echo off
SET /P string= < [Link]
SET string=%string:colour=color%
SET string=%string:flat=apartment%
ECHO %string% > [Link]
PAUSE
[Link]
@echo off
COLOR 0D
:start
SET /P msg= Enter your message:
ECHO %username% says: %msg% >> [Link]
GOTO start
[Link]
@echo off
:start
TYPE [Link]
TIMEOUT 3 > nul
GOTO start
[Link]
@echo off
ECHO. > [Link]
[Link]
@echo off
COLOR 0D
7
:start
SET /P msg= Enter your message:
ECHO %username% says: %msg:&=^&% >> [Link]
GOTO start
[Link]
@echo off
IF EXIST %1 (ECHO file found) ELSE ECHO file not found
[Link]
@echo off
IF %2 == 0 (
SET output=Infinity
) ELSE (
SET /A output = %1 / %2
)
ECHO %output%
[Link]
@echo off
SET /A output= %1 - %2
IF %output% LSS 0 SET /A output*= -1
ECHO %output%
[Link]
@echo off
IF %1 GTR %2 (
SET /A output= %1 - %2
) ELSE (
SET /A output= %2 - %1
)
ECHO %output%
[Link]
@echo off
ECHO The Information Interface
ECHO.
ECHO Options:
8
ECHO 1 - Display version of Windows
ECHO 2 - Display volume details
ECHO 3 - Display name of host device
ECHO.
SET /P input= Enter a number from 1 to 3 and press enter:
IF %input% == 1 VER
IF %input% == 2 VOL
IF %input% == 3 HOSTNAME
PAUSE
[Link]
@echo off
ECHO The Information Interface
ECHO.
ECHO Options:
ECHO 1 - Display version of Windows
ECHO 2 - Display volume details
ECHO 3 - Display name of host device
ECHO.
:choose
SET /P input= Enter a number from 1 to 3 and press enter:
IF NOT "%input:~1%" == "" ECHO Too many characters entered! & GOTO
choose
IF %input% GTR 3 ECHO Too high! & GOTO choose
IF %input% LSS 1 ECHO Too low! & GOTO choose
IF %input% == 1 VER
IF %input% == 2 VOL
IF %input% == 3 HOSTNAME
PAUSE
9
VOLUME 2
[Link]
@echo off
MORE logs\log*.txt
PAUSE
[Link]
@echo off
SORT %[Link] /O %[Link]
[Link]
@echo off
SORT %[Link] /R /O %[Link]
[Link]
@echo off
FIND /I /V /C "%1" %2
[Link]
@echo off
SYSTEMINFO | FIND /I "boot time"
[Link]
@echo off
SYSTEMINFO | FINDSTR /B OS
[Link]
@echo off
FINDSTR "1,*000" [Link]
PAUSE
[Link]
@echo off
FINDSTR "alert.*error" [Link]
PAUSE
[Link]
@echo off
10
FINDSTR "1$" [Link]
PAUSE
[Link]
@echo off
FINDSTR "^2" [Link]
PAUSE
[Link]
@echo off
FINDSTR "[0-9]$" [Link]
PAUSE
[Link]
@echo off
FINDSTR "[^0-9][0-9]$" [Link]
PAUSE
[Link]
@echo off
FINDSTR "^[0-9][0-9][0-9][0-9][0-9]$" [Link]
PAUSE
[Link]
@echo off
::this is a comment, a label is on the line below
:label1
REM this is a comment too, a label is on the line below
:label2
FINDSTR "^:[^:]" [Link]
PAUSE
[Link]
@echo off
11
FINDSTR "\.csv" [Link]
PAUSE
[Link]
@echo off
FINDSTR /R /C:" .$" [Link]
PAUSE
17checkInput_NOTOK.bat
@echo off
:begin
SET /P input= Enter a variable:
IF NOT DEFINED input GOTO begin
ECHO You entered %input%
PAUSE
[Link]
@echo off
REM Present user with options
:begin
ECHO Dummy file generator
ECHO.
ECHO Choose a file type. Enter:
ECHO 1 for .txt
ECHO 2 for .csv
ECHO 3 for .html
SET /P fileType= Enter a type?
REM Make time variable 10:02:31.69 --> 100231.69 --> 10023169
SET timeMade=%time::=%
SET timeMade=%timeMade:.=%
REM Choose dummy file type, and make it
GOTO case%fileType%
:case1
ECHO. > file%timeMade%.txt
GOTO begin
12
:case2
ECHO. > file%timeMade%.csv
GOTO begin
:case3
ECHO. > file%timeMade%.html
GOTO begin
==========
@echo off
Set "_var=first"
Set "_var=second" & Echo %_var% !_var!
--> first !_var!
@echo off
SETLOCAL EnableDelayedExpansion
Set "_var=first"
Set "_var=second" & Echo %_var% !_var!
--> first second
@echo off
SETLOCAL EnableDelayedExpansion
Set "_var=Old"
For /L %%G in (1,1,3) Do (
Set "_var=New"
Echo [%_var%] is now [!_var!]
)
-->
[Old] is now [New]
[Old] is now [New]
[Old] is now [New]
==========
[Link]
@echo off
SET string=%3
SETLOCAL ENABLEDELAYEDEXPANSION
ECHO !string:%1=%2!
[Link]
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
REM Define file type variables
SET fileType1=txt
13
SET fileType2=csv
SET fileType3=html
REM Present options to user
:begin
ECHO Dummy file generator
ECHO.
ECHO Choose a file type. Enter:
ECHO 1 for %fileType1%
ECHO 2 for %fileType2%
ECHO 3 for %fileType3%
SET /P num= Enter a type?
REM generate time variable
SET timeMade=%time::=%
SET timeMade=%timeMade:.=%
ECHO. > file%timeMade%.!fileType%num%!
GOTO begin
[Link]
@echo off
DEL %[Link]
REN %[Link] %[Link]
[Link]
@echo off
IF EXIST %[Link] (DEL %[Link]) ELSE EXIT /B 1
REN %[Link] %[Link]
[Link]
@echo off
ECHO Attemting to restore file %[Link]
CALL 22safeRestore %1
GOTO Case%errorlevel%
:Case0
ECHO File restored
EXIT /B
14
:Case1
ECHO There is no backup file to restore from
[Link]
@echo off
SET /P setup=Press 'S' to create side-by-side, or 'W' to create
within each other:
GOTO Case%setup%
:CaseS
MKDIR %1
MKDIR %2
MKDIR %3
EXIT /B
:CaseW
MKDIR %1\%2\%3
[Link]
@echo off
RMDIR /S /Q %1
MOVE %1_bak %1
[Link]
ATTRIB -r -h /D /S
[Link]
COPY [Link] folder\[Link]
[Link]
@echo off
COPY folder\*txt folder\*.bak
[Link]
@echo off
DEL *.txt
REN *.bak *.txt
[Link]
@echo off
COPY /Y *.bak *.txt
15
DEL *.bak
[Link]
@echo off
XCOPY /s /e /i %1 %1_bak
16
VOLUME 3
[Link]
@echo off
SUBST Y: "\\[Link]\E$\3. Mine\Batch
Script\Volume_3\mapFolder"
REM IP address checked with 'IPCONFIG'
REM Folder path copied in from Windows Explorer
REM ':' after drive letter replaced with '$'
COPY [Link] Y:
SUBST /D Y:
[Link]
@echo off
CALL 3logMe %0
REM If this was not just a test script...
REM ... the rest of the script would go here.
ECHO Script completed!
PAUSE
[Link]
ECHO %1 ran at %time% on %date% >> [Link]
[Link]
@echo off
REM ~x is file type, "1" is file of 1st parameter, "0" is file
running
ECHO %~x1
REM ~z is file size, "1" is file of 1st parameter, "0" is file
running
ECHO %~z1
PAUSE
[Link]
@echo off
17
TITLE %0
START 6getData
TIMEOUT /T 3
ECHO finished > [Link]
DIR
PAUSE
[Link]
TITLE %0
HOSTNAME
DIR
PAUSE
[Link]
@echo off
TITLE %0
ECHO finished > [Link]
DIR
PAUSE
[Link]
TITLE %0
HOSTNAME
DIR
START 7doProcess2
PAUSE
[Link]
@echo off
TITLE %0
DEL [Link]
DEL [Link]
18
START 10getData3
:startLoop
TIMEOUT /T 3
IF NOT EXIST [Link] GOTO startLoop
ECHO finished > [Link]
DIR
PAUSE
[Link]
TITLE %0
HOSTNAME
DIR
PAUSE
REM The 'pause' above is only for testing. You would delete it
after that.
ECHO.>[Link]
PAUSE
[Link]
@echo off
REM eq (==), ne (!=), gt (>), lt (<), ge (>=), le (<=)
TASKLIST /FI "SessionName eq Services"
PAUSE
[Link]
@echo off
TASKLIST /FO csv | sort > [Link]
ECHO Open a program then press enter
PAUSE > nul
TASKLIST /FO csv | sort > [Link]
ECHO Delete the other columns in both files, then press enter.
19
PAUSE > nul
ECHO The difference is:
FC [Link] [Link]
PAUSE
[Link]
@echo off
TASKKILL /IM [Link]
PAUSE
[Link]
FOR %%i IN (*.txt *.csv) DO ECHO %%i >> %%i
[Link]
FOR %%i IN (*) DO ECHO %%i >> [Link]
[Link]
FOR %%i IN (*) DO ECHO %%i >> [Link] & ECHO %%i >> [Link]
[Link]
@echo off
FOR /L %%i IN (101, 1, 105) DO SET IP%%i = 1.2.3.%%i
ECHO IP101 = %IP101%
ECHO IP102 = %IP102%
ECHO IP103 = %IP103%
ECHO IP104 = %IP104%
ECHO IP105 = %IP105%
PAUSE
[Link]
@echo off
SET count = 0
SETLOCAL ENABLEDELAYEDEXPANSION
FOR %%i IN (*) DO (
20
SET /A count = !count! + 1
ECHO File!count!: %%i
)
PAUSE
[Link]
@echo off
SET count = 0
SETLOCAL ENABLEDELAYEDEXPANSION
FOR %%i IN (*) DO (
SET /A count = !count! + 1
ECHO File!count!: %%i
IF !count! == 3 EXIT /B
)
PAUSE
[Link]
@echo off
FOR /F "tokens=3 delims=," %%i IN ([Link]) DO ECHO %%i
PAUSE
[Link]
@echo off
REM "skip=1" means skip the first 1 line, "goto next" means get
2nd line and exit from for loop
FOR /F "tokens=* skip=1" %%i IN ([Link]) DO ECHO %%i & GOTO
next
:next
PAUSE
[Link]
@echo off
REM Calculate number of lines to skip
SET /A skipVal = %1 - 1
REM Skip that many lines, display the next line
21
FOR /F "tokens=* skip=%skipVal%" %%i IN ([Link]) DO ECHO %%i &
GOTO next
:next
PAUSE
[Link]
@echo off
REM If line 1 is requested, display it (don't skip any lines)
IF %1==1 FOR /F "tokens=*" %%i IN ([Link]) DO ECHO %%i & GOTO
next
REM Calculate number of lines to skip
SET /A skipVal = %1 - 1
REM Skip that many lines, display the next line
FOR /F "tokens=* skip=%skipVal%" %%i IN ([Link]) DO ECHO %%i &
GOTO next
:next
PAUSE
[Link]
@echo off
REM If a value in line 1 is requested, display it (don't skip any
lines)
IF %1==1 FOR /F "tokens=%2 delims=," %%i IN ([Link]) DO ECHO
%%i & GOTO next
REM Calculate number of lines to skip
SET /A skipVal = %1 - 1
REM Skip that many lines, display the selected value from the next
line
FOR /F "tokens=%2 skip=%skipVal% delims=," %%i IN ([Link]) DO
ECHO %%i & GOTO next
:next
PAUSE
22
[Link]
@echo off
FOR /F "tokens=1-3 delims=," %%i IN ([Link]) DO ECHO
%%k,%%j,%%i
PAUSE
[Link]
FOR /F "tokens=1-3 delims=," %%i IN ([Link]) DO ECHO
%%k,%%j,%%i >> [Link]
TYPE [Link] > [Link]
DEL [Link]
[Link]
@echo off
fOR /F "tokens=*" %%I IN ([Link]) DO ECHO %%I | CALL
29lineReplace1
PAUSE
[Link]
@echo off
SET /P string=%*
ECHO %string%
[Link]
@echo off
SET /P string=%*
ECHO %string:Microsoft=Apple%
[Link]
@echo off
fOR /F "tokens=*" %%I IN ([Link]) DO ECHO %%I | CALL
31lineReplace2
TYPE [Link] > [Link]
DEL [Link]
[Link]
@echo off
23
SET /P string=%*
ECHO %string:Microsoft=Apple% >> [Link]
[Link]
ECHO Parameter zero is: > %0
--> After running, now the content of the file is "Parameter zero
is: "
[Link]
@echo off
ECHO Parameter zero is: ^> %0
REM '^' escapes '>', preventing the batch file redirecting text
and overwriting itself
PAUSE
[Link]
@echo off
SET var8 = %8
SHIFT /8
SET var9 = %8
SHIFT /8
SET var10 = %8
ECHO The first 7 parameters are: %1 %2 %3 %4 %5 %6 %7
ECHO var8 = %var8%
ECHO var9 = %var9%
ECHO var10 = %var10%
PAUSE
[Link]
@echo off
SET num= 8
:startLoop
SET var%num% = %8
24
SHIFT /8
SET /A num += 1
IF NOT "%8" == "" GOTO startLoop
ECHO The first 7 parameters are: %1 %2 %3 %4 %5 %6 %7
SET /A num -= 1
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /L %%I IN (8, 1, %num%) DO ECHO var%%I is !var%%I!
PAUSE
[Link]
@echo off
ECHO %0 %* >> [Link]
25