Batch Script CMD syntax with example
Batch Script – VER
This batch command shows the version of MS-DOS you are using.Syntax
ver
Example
@echo off ver
Output
The output of the above command is as follows. The version number will depend upon the operating system you are working on.Batch Script – ASSOC
This is a batch command that associates an extension with a file type (FTYPE), displays existing associations, or deletes an association.Syntax
assoc – Displays all the file extensions assoc | find “.ext” – Displays only those file extensions which have the extension ext.
Example
@echo off assoc > C:\lists.txt assoc | find “.doc” > C:\listsdoc.txt
Output
The list of file associations will be routed to the file lists.txt. The following output shows what is there in the listsdoc.txt file after the above batch file is run..doc=Word.Document.8 .dochtml=wordhtmlfile .docm=Word.DocumentMacroEnabled.12 .docmhtml=wordmhtmlfile .docx=Word.Document.12 .docxml=wordxmlfile ---------------------------------------------------------------------------
Batch Script – CD
This batch command helps in making changes to a different directory, or displays the current directory.Syntax
cd
Example
The following example shows how the cd command can be used in a variety of ways.@echo off Rem The cd without any parameters is used to display the current working directory cd Rem Changing the path to Program Files cd\Program Files cd Rem Changing the path to Program Files cd %USERPROFILE% cd Rem Changing to the parent directory cd.. cd Rem Changing to the parent directory two levels up cd..\.. cd
Output
The above command will display the following output after changing to the various folder locations.C:\Users\Administrator C:\Program Files C:\Users\Administrator C:\Users C:\ ---------------------------------------------------------------------------
Batch Script – CLS
This batch command clears the screen.Syntax
cls
Example
@echo off Cls
Output
The command prompt screen will be cleared.Batch Script – COPY
This batch command is used for copying files from one location to the other.Syntax
Copy [source] [destination]The files will be copied from source to destination location.
Example
The following example shows the different variants of the copy command.@echo off cd Rem Copies lists.txt to the present working directory. If there is no destination identified , it defaults to the present working directory. copy c:\lists.txt Rem The file lists.txt will be copied from C:\ to C:\tp location copy C:\lists.txt c:\tp Rem Quotation marks are required if the file name contains spaces copy “C:\My File.txt” Rem Copies all the files in F drive which have the txt file extension to the current working directory copy F:\*.txt Rem Copies all files from dirA to dirB. Note that directories nested in dirA will not be copied copy C:\dirA dirB
Output
All actions are performed as per the remarks in the batch file.Batch Script – DEL
]This batch command deletes files and not directories.
Syntax
del [filename]
Example
The following example shows the different variants of the del command.@echo off Rem Deletes the file lists.txt in C:\ del C:\lists.txt Rem Deletes all files recursively in all nested directories del /s *.txt Rem Deletes all files recursively in all nested directories , but asks for the confirmation from the user first Del /p /s *.txt
Output
All actions are performed as per the remarks in the batch file.Batch Script – DIR
This batch command lists the contents of a directory.Syntax
dir
Example
The following example shows the different variants of the dir command.@echo off Rem All the directory listings from C:\ will be routed to the file lists.txt dir C:\>C:\lists.txt Rem Lists all directories and subdirectories recursively dir /s Rem Lists the contents of the directory and all subdirectories recursively, one file per line, displaying complete path for each listed file or directory. dir /s /b Rem Lists all files with .txt extension. dir *.txt Rem Includes hidden files and system files in the listing. dir /a Rem Lists hidden files only. dir /ah
Output
All actions are performed as per the remarks in the batch file.Batch Script – DATE
This batch command help to find the system date.Syntax
DATE
Example
@echo off echo %DATE%
Output
The current date will be displayed in the command prompt. For example,Batch Script – ECHO
ECHO
This batch command displays messages, or turns command echoing on or off.Syntax
ECHO “string”
Example
The following example shows the different variants of the dir command.Rem Turns the echo on so that each command will be shown as executed echo on echo "Hello World" Rem Turns the echo off so that each command will not be shown when executed @echo off echo "Hello World" Rem Displays the contents of the PATH variable echo %PATH%
Output
The following output will be displayed in the command prompt.C:\>Rem Turns the echo on so that each command will be shown as executed C:\>echo on C:\>echo "Hello World" "Hello World" C:\>Rem Turns the echo off so that each command will not be shown when executed "Hello World" C:\Users\ADMINI~1\AppData\Local\Temp
Batch Script – EXIT
This batch command exits the DOS console.Syntax
Exit
Example
@echo off echo "Hello World" exit
Output
The batch file will terminate and the command prompt window will close.Batch Script – MD
This batch command creates a new directory in the current location.Syntax
md [new directory name]
Example
@echo off md newdir cd newdir cd Rem “Goes back to the parent directory and create 2 directories” cd.. md newdir1 newdir1 cd newdir1 cd cd.. cd newdir2 cd
Output
The above command produces the following output.C:\newdir C:\newdir1 C:\newdir2 ---------------------------------------------------------------------------
Batch Script – MOVE
his batch command moves files or directories between directories.Syntax
move [source] [destination]The files will be copied from source to destination location.
Example
The following example shows the different variants of the move command.@echo off Rem Moves the file list.txt to the directory c:\tp move C:\lists.txt c:\tp Rem Renames directory Dir1 to Dir2, assuming Dir1 is a directory and Dir2 does not exist. move Dir1 Dir2 Rem Moves the file lists.txt to the current directory. move C:\lists.txt
Output
All actions are performed as per the remarks in the batch file.Batch Script – REM
This batch command is used for remarks in batch files, preventing the content of the remark from being executed.Syntax
REM remark description
Example
@echo off REM This is a batch file
Output
None ---------------------------------------------------------------------------
Batch Script – REN
Syntax
ren [oldfile/dirname] [newfile/dirname]Renames the file name from the old file/dir name to the new one.
Example
@echo off ren C:\lists.txt C:\newlists.txt
Output
The file lists.txt will be renamed to newlists.txt.Batch Script – RD
This batch command removes directories, but the directories need to be empty before they can be removed.Syntax
rd [directoryname]
Example
The following example shows the different variants of the rd command.@echo off Rem removes the directory called newdir rd C:\newdir Rem removes 2 directories rd Dir1 Dir2 Rem Removes directory with spaces rd "Application A" Rem Removes the directory Dir1 including all the files and subdirectories in it rd /s Dir1 Rem Removes the directory Dir1 including all the files and subdirectories in it but asks for a user confirmation first. rd /q /s Dir1
Output
All actions are performed as per the remarks in the batch file.Batch Script – PROMPT
This batch command can be used to change or reset the cmd.exe prompt.Syntax
PROMPT [newpromptname]
Example
@echo off prompt myprompt$GThe $G is the greater than sign which is added at the end of the prompt.
Output
The prompt shown to the user will now be myprompt>Batch Script – PAUSE
This batch command prompts the user and waits for a line of input to be entered.Syntax
Pause
Example
@echo off pause
Output
The command prompt will show the message “Press any key to continue….” to the user and wait for the user’s input.Batch Script – PATH
This batch command displays or sets the path variable.Syntax
PATH
Example
@echo off Echo %PATH%
Output
The value of the path variable will be displayed in the command prompt.Batch Script – START
This batch command starts a program in new window, or opens a document.Syntax
START “programname”
Example
@echo off start notepad.exe
Output
When the batch file is executed, a new notepad windows will start.Batch Script – TIME
This batch command sets or displays the time.Syntax
TIME
Example
@echo off echo %TIME%
Output
The current system time will be displayed. For example,Batch Script – TYPE
This batch command prints the content of a file or files to the output.Syntax
TYPE [filename]Where filename is the file whose contents need to be displayed.
Example
@echo off TYPE C:\tp\lists.txt
Output
The contents of the file lists.txt will be displayed to the command prompt.Batch Script – VOL
This batch command displays the volume labels.Syntax
VOL
Example
@echo off VOL
Output
The output will display the current volume label. For example,Volume in drive C is Windows8_OS Volume Serial Number is E41C-6F43 ------------------------------------------------------------------------
Batch Script – ATTRIB
Displays or sets the attributes of the files in the current directorySyntax
attrib
Example
The following example shows the different variants of the attrib command.@echo off Rem Displays the attribites of the file in the current directory Attrib Rem Displays the attributes of the file lists.txt attrib C:\tp\lists.txt Rem Adds the "Read-only" attribute to the file. attrib +r C:\tp\lists.txt Attrib C:\tp\lists.txt Rem Removes the "Archived" attribute from the file attrib -a C:\tp\lists.txt Attrib C:\tp\lists.txt
Output
For example,-----------------------------------------------------------------------------------------------------------------------------------------
Batch Script – CHKDSK
This batch command checks the disk for any problems.Syntax
chkdsk
Example
@echo off chkdsk
Output
The above command starts checking the current disk for any errors.Batch Script – CHOICE
This batch command provides a list of options to the user.Syntax
CHOICE /c [Options] /m [Message]Where Options is the list of options to provide to the user and Message is the string message which needs to be displayed.
Example
@echo off echo "What is the file size you what" echo "A:10MB" echo "B:20MB" echo "C:30MB" choice /c ABC /m "What is your option A , B or C"
Output
The above program produces the following output."What is the file size you what" "A:10MB" "B:20MB" "C:30MB" What is your option A , B or C [A,B,C]? ---------------------------------------------------------------------------
Batch Script – CMD
This batch command invokes another instance of command prompt.
Syntax
cmd
Example
@echo off cmd
Output
Another instance of command prompt will be invoked.Batch Script – COMP
This batch command compares 2 files based on the file size.Syntax
COMP [sourceA] [sourceB]Wherein sourceA and sourceB are the files which need to be compared.
Example
@echo off COMP C:\tp\lists.txt C:\tp\listsA.txt
Output
The above command will compare the files lists.txt and listsA.txt and find out if the two file sizes are different.Batch Script – CONVERT
This batch command converts a volume from FAT16 or FAT32 file system to NTFS file system.Syntax
CONVERT [drive]
Example
@echo off CONVERT C:\
Output
The above command will convert the file system of C drive.Batch Script – DRIVERQUERY
This batch command shows all installed device drivers and their properties.Syntax
driverquery
Example
@echo off driverquery
Output
The above command will display the information of all the device drivers installed on the current system. Following is an example of a subset of the information displayed.WacomPen Wacom Serial Pen HID D Kernel 8/22/2013 4:39:15 AM Wanarp Remote Access IP ARP D Kernel 8/22/2013 4:35:45 AM Wanarpv6 Remote Access IPv6 ARP Kernel 8/22/2013 4:35:45 AM Wdf01000 Kernel Mode Driver Fra Kernel 8/22/2013 4:38:56 AM WFPLWFS Microsoft Windows Filt Kernel 11/9/2014 6:57:28 PM WIMMount WIMMount File System 8/22/2013 4:39:34 AM WinMad WinMad Service Kernel 5/9/2013 9:14:27 AM WinNat Windows NAT Driver Kernel 1/22/2014 1:10:49 AM WinUsb WinUsb Driver Kernel 8/22/2013 4:37:55 AM WinVerbs WinVerbs Service Kernel 5/9/2013 9:14:30 AM WmiAcpi Microsoft Windows Mana Kernel 8/22/2013 4:40:04 AM WpdUpFltr WPD Upper Class Filter Kernel 8/22/2013 4:38:45 AM ws2ifsl Windows Socket 2.0 Non Kernel 8/22/2013 4:40:03 AM wtlmdrv Microsoft iSCSI Target Kernel 8/22/2013 4:39:19 AM WudfPf User Mode Driver Frame Kernel 8/22/2013 4:37:21 AM WUDFWpdFs WUDFWpdFs Kernel 8/22/2013 4:36:50 AM WUDFWpdMtp WUDFWpdMtp Kernel 8/22/2013 4:36:50 AM --------------------------------------------------------------------------- ----------------------------------------------------------------------------
Batch Script – EXPAND
This batch command extracts files from compressed .cab cabinet files.Syntax
EXPAND [cabinetfilename]
Example
@echo off EXPAND excel.cab
Output
The above command will extract the contents of the file excel.cab in the current location.-----------------------------------------------------------------------------------------------------------------------------------------
0 comments:
Post a Comment
For Any Tech Updates, Hacking News, Internet, Computer, Technology and related to IT Field Articles Follow Our Blog.