0% found this document useful (0 votes)
3 views

CommandLine_BatchScript

This document provides a comprehensive guide to command line and batch scripting commands, detailing their functions and usage. It includes a list of commands such as ASSOC, CD, COPY, and many others, along with examples of batch scripts for various tasks. The document serves as a resource for users looking to learn and utilize command line and batch scripting efficiently.

Uploaded by

trido2512
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

CommandLine_BatchScript

This document provides a comprehensive guide to command line and batch scripting commands, detailing their functions and usage. It includes a list of commands such as ASSOC, CD, COPY, and many others, along with examples of batch scripts for various tasks. The document serves as a resource for users looking to learn and utilize command line and batch scripting efficiently.

Uploaded by

trido2512
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

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 CMD.EXE 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 CONFIG.SYS.
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 CMD.EXE 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

1YourVersion.bat
PROMPT $V$G$S

2ColorCode.bat
COLOR 17

3custom.bat
COLOR 0A
TITLE Command Testing
CLS

4custom2.bat
CD "E:\3. Mine\Batch Script\Volume_1"
CLS

5twoLines.bat
@echo off

ECHO. & ECHO.

6clearFile.bat
@echo off

ECHO. > file0.txt

7sysInfo.bat
@echo off

SYSTEMINFO > sysInfo.txt

8gather.bat
@echo off

VOL >> gather.txt


VER >> gather.txt
HOSTNAME >> gather.txt

9backup.bat
@echo off

TYPE %1.txt > %1.bak

10logIn.bat
@echo off

SET /p input= Enter text to add to log file:

ECHO At %time% on %date%, %username% logged: %input% >> log.txt

4
ECHO log updated

11logOut.bat
@echo off

SET /p input= Enter command to log results for:

ECHO The command "%input%" returned: >> logOut.txt

ECHO. >> logOut.txt

%input% >> logOut.txt

ECHO. >> logOut.txt

ECHO log updated

PAUSE

12logErr.bat
@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> tempErrLog.txt

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: >> logErr.txt

TYPE tempErrLog.txt >> logErr.txt

REM add blank lines to separate entires


ECHO. >> logErr.txt
ECHO error log updated
)

REM delete the temparary file's contents


ECHO. > tempErrlog.txt

PAUSE

13mod.bat
@echo off

SET /A output = %1 %% %2

ECHO %output%

5
14divideA.bat
@echo off

SET /A output = %1 / %2

ECHO %output%

15square.bat
@echo off

SET /A output = %1 * %1

ECHO %output%

16getIP.bat
@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%

17left10.bat
@echo off

SET string=%1

ECHO %string:~0,10%

18right10.bat
@echo off

SET string=%1

ECHO %string:~-10%

19char3.bat
@echo off

SET string=%1

ECHO %string:~2,1%

6
20trim.bat
@echo off

SET string=%1

ECHO %string: = %

21UKtoUS.bat
@echo off

SET /P string= < ebook.txt

SET string=%string:colour=color%
SET string=%string:flat=apartment%

ECHO %string% > ebook1.txt

PAUSE

22writer.bat
@echo off

COLOR 0D

:start

SET /P msg= Enter your message:


ECHO %username% says: %msg% >> chat.txt

GOTO start

23reader.bat
@echo off

:start

TYPE chat.txt
TIMEOUT 3 > nul

GOTO start

24flush.bat
@echo off

ECHO. > chat.txt

25writer2.bat
@echo off

COLOR 0D

7
:start

SET /P msg= Enter your message:


ECHO %username% says: %msg:&=^&% >> chat.txt

GOTO start

26isHere.bat
@echo off

IF EXIST %1 (ECHO file found) ELSE ECHO file not found

27divideB.bat
@echo off

IF %2 == 0 (
SET output=Infinity
) ELSE (
SET /A output = %1 / %2
)

ECHO %output%

28absA.bat
@echo off

SET /A output= %1 - %2

IF %output% LSS 0 SET /A output*= -1

ECHO %output%

29absB.bat
@echo off

IF %1 GTR %2 (
SET /A output= %1 - %2
) ELSE (
SET /A output= %2 - %1
)

ECHO %output%

30info.bat
@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

31info2.bat
@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

1scanLogs.bat
@echo off

MORE logs\log*.txt

PAUSE

2sortFile.bat
@echo off

SORT %1.txt /O %1.txt

3sortFileR.bat
@echo off

SORT %1.txt /R /O %1.txt

4mismatchTotal.bat
@echo off

FIND /I /V /C "%1" %2

5bootTime.bat
@echo off

SYSTEMINFO | FIND /I "boot time"

6justOS.bat
@echo off

SYSTEMINFO | FINDSTR /B OS

7commas.bat
@echo off

FINDSTR "1,*000" test.txt

PAUSE

8twoWords.bat
@echo off

FINDSTR "alert.*error" test.txt

PAUSE

9ending.bat
@echo off

10
FINDSTR "1$" test.txt

PAUSE

10beginning.bat
@echo off

FINDSTR "^2" test.txt

PAUSE

11endInDigit.bat
@echo off

FINDSTR "[0-9]$" test.txt

PAUSE

12endIn1Digit.bat
@echo off

FINDSTR "[^0-9][0-9]$" test.txt

PAUSE

13zipCode.bat
@echo off

FINDSTR "^[0-9][0-9][0-9][0-9][0-9]$" test.txt

PAUSE

14labelsOnly.bat
@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 "^:[^:]" 14labelsOnly.bat

PAUSE

15getCSVs.bat
@echo off

11
FINDSTR "\.csv" test.txt

PAUSE

16trickyEnd.bat
@echo off

FINDSTR /R /C:" .$" test.txt

PAUSE

17checkInput_NOTOK.bat
@echo off

:begin

SET /P input= Enter a variable:

IF NOT DEFINED input GOTO begin

ECHO You entered %input%

PAUSE

18makeDummy.bat
@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]
==========

19stringReplace.bat
@echo off

SET string=%3

SETLOCAL ENABLEDELAYEDEXPANSION

ECHO !string:%1=%2!

20makeDummy2.bat
@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

21restore.bat
@echo off

DEL %1.txt

REN %1.bak %1.txt

22safeRestore.bat
@echo off

IF EXIST %1.bak (DEL %1.txt) ELSE EXIT /B 1

REN %1.bak %1.txt

23parent.bat
@echo off

ECHO Attemting to restore file %1.txt

CALL 22safeRestore %1

GOTO Case%errorlevel%

:Case0
ECHO File restored
EXIT /B

14
:Case1
ECHO There is no backup file to restore from

24mkdirs.bat
@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

25restoreDir.bat
@echo off

RMDIR /S /Q %1

MOVE %1_bak %1

26clearAll.bat
ATTRIB -r -h /D /S

27myCopy.bat
COPY test.txt folder\NewName.txt

28backup.bat
@echo off

COPY folder\*txt folder\*.bak

29restoreAll.bat
@echo off

DEL *.txt

REN *.bak *.txt

30safeRestoreAll.bat
@echo off

COPY /Y *.bak *.txt

15
DEL *.bak

31backupDir.bat
@echo off

XCOPY /s /e /i %1 %1_bak

16
VOLUME 3

1copyToMap.bat
@echo off

SUBST Y: "\\10.84.30.64\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 file1.txt Y:

SUBST /D Y:

2callLogMe.bat
@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

3logMe.bat
ECHO %1 ran at %time% on %date% >> log.txt

4fileInfo.bat
@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

5doProcess.bat
@echo off

17
TITLE %0

START 6getData

TIMEOUT /T 3

ECHO finished > processDone.txt

DIR

PAUSE

6getData.bat
TITLE %0

HOSTNAME

DIR

PAUSE

7doProcess2.bat
@echo off

TITLE %0

ECHO finished > processDone.txt

DIR

PAUSE

8getData2.bat
TITLE %0

HOSTNAME

DIR

START 7doProcess2

PAUSE

9doProcess3.bat
@echo off

TITLE %0

DEL processDone.txt

DEL goAhead.txt

18
START 10getData3

:startLoop

TIMEOUT /T 3

IF NOT EXIST goAhead.txt GOTO startLoop

ECHO finished > processDone.txt

DIR

PAUSE

10getData3.bat
TITLE %0

HOSTNAME

DIR

PAUSE

REM The 'pause' above is only for testing. You would delete it
after that.

ECHO.>goAhead.txt

PAUSE

11listServices.bat
@echo off

REM eq (==), ne (!=), gt (>), lt (<), ge (>=), le (<=)


TASKLIST /FI "SessionName eq Services"

PAUSE

12getImageName.bat
@echo off

TASKLIST /FO csv | sort > list1.csv

ECHO Open a program then press enter

PAUSE > nul

TASKLIST /FO csv | sort > list2.csv

ECHO Delete the other columns in both files, then press enter.

19
PAUSE > nul

ECHO The difference is:

FC list1.csv list2.csv

PAUSE

13closeExcel.bat
@echo off

TASKKILL /IM excel.exe

PAUSE

14raw.bat
FOR %%i IN (*.txt *.csv) DO ECHO %%i >> %%i

15list.bat
FOR %%i IN (*) DO ECHO %%i >> list.csv

16list2.bat
FOR %%i IN (*) DO ECHO %%i >> list.csv & ECHO %%i >> list.txt

17makeIPs.bat
@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

18countFiles.bat
@echo off

SET count = 0

SETLOCAL ENABLEDELAYEDEXPANSION

FOR %%i IN (*) DO (

20
SET /A count = !count! + 1
ECHO File!count!: %%i
)

PAUSE

19top3.bat
@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

20getColumn3.bat
@echo off

FOR /F "tokens=3 delims=," %%i IN (list2.csv) DO ECHO %%i

PAUSE

21get1Line.bat
@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 (list2.csv) DO ECHO %%i & GOTO


next

:next

PAUSE

22getLineNum.bat
@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 (list2.csv) DO ECHO %%i &
GOTO next

:next

PAUSE

23getAnyLineNum.bat
@echo off

REM If line 1 is requested, display it (don't skip any lines)

IF %1==1 FOR /F "tokens=*" %%i IN (list2.csv) 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 (list2.csv) DO ECHO %%i &


GOTO next

:next

PAUSE

24getValue.bat
@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 (list2.csv) 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 (list2.csv) DO


ECHO %%i & GOTO next

:next

PAUSE

22
25reverse.bat
@echo off

FOR /F "tokens=1-3 delims=," %%i IN (list2.csv) DO ECHO


%%k,%%j,%%i

PAUSE

26columnSwap.bat
FOR /F "tokens=1-3 delims=," %%i IN (list2.csv) DO ECHO
%%k,%%j,%%i >> list3.csv

TYPE list3.csv > list2.csv

DEL list3.csv

27mainReplace.bat
@echo off

fOR /F "tokens=*" %%I IN (sysInfo.txt) DO ECHO %%I | CALL


29lineReplace1

PAUSE

28lineReplace.bat
@echo off

SET /P string=%*

ECHO %string%

29lineReplace1.bat
@echo off

SET /P string=%*

ECHO %string:Microsoft=Apple%

30mainReplace2.bat
@echo off

fOR /F "tokens=*" %%I IN (sysInfo.txt) DO ECHO %%I | CALL


31lineReplace2

TYPE temp.txt > sysInfo.txt

DEL temp.txt

31lineReplace2.bat
@echo off

23
SET /P string=%*

ECHO %string:Microsoft=Apple% >> temp.txt

32get.bat
ECHO Parameter zero is: > %0

--> After running, now the content of the file is "Parameter zero
is: "

33get0.bat
@echo off

ECHO Parameter zero is: ^> %0

REM '^' escapes '>', preventing the batch file redirecting text
and overwriting itself

PAUSE

34easyShifter.bat
@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

35Shifter.bat
@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

36asterisk.bat
@echo off

ECHO %0 %* >> log.txt

25

You might also like