2024年6月30日 星期日

Batch Script - Commands

 

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.

Microsoft Windows [Version 6.3.9600]
 
 
 
 
 

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
 
 
 
 
 

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:\
 
 
 
 

Example

@echo off Cls

Output

The command prompt screen will be cleared.

 

 

 

 

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.

 

 

 

 

 

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.

 

 

 

 

 

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.

 

 

 

 

 

Example

@echo off 
echo %DATE%

Output

The current date will be displayed in the command prompt. For example,

Mon 12/28/2015 
 
 
 
 
 

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 

 

 

 

 

Example

@echo off 
echo "Hello World" 
exit

Output

The batch file will terminate and the command prompt window will close.

 

 

 

 

 

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 
 
 
 
 
 
 

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 

 

 

 

 

 

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.

 

 

 

 

 

Example

@echo off 
Echo %PATH%

Output

The value of the path variable will be displayed in the command prompt.

 

 

 

 

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.

 

 

 

 

 

Example

@echo off 
prompt myprompt$G

The $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>

 

 

 

 

 

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.

 

 

 

 

 

Example

@echo off 
ren C:\lists.txt C:\newlists.txt

Output

The file lists.txt will be renamed to newlists.txt.

 

 

 

 

 

Example

@echo off 
REM This is a batch file

 :: This is comment





Example

@echo off
start notepad.exe

Output

When the batch file is executed, a new notepad windows will start.

 

 

 

 

 

Example

@echo off 
echo %TIME%

Output

The current system time will be displayed. For example,

22:06:52.87 
 
 
 
 
 

Example

@echo off TYPE C:\tp\lists.txt

Output

The contents of the file lists.txt will be displayed to the command prompt.

 

 

 

 

 

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 
 
 
 
 
 

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,

A C:\tp\assoclst.txt A C:\tp\List.cmd A C:\tp\lists.txt A C:\tp\listsA.txt A C:\tp\lists.txt A R C:\tp\lists.txt R C:\tp\lists.txt 

 

 

 

 

 

Example

@echo off 
chkdsk

Output

The above command starts checking the current disk for any errors.

 

 

 

 

 

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]? 
 
 
 
 

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.

 

 

 

 

Example

@echo off 
FIND "Application" C:\tp\lists.txt

Output

If the word “Application” resides in the file lists.txt, the line containing the string will be displayed in the command prompt.

 

 

 

 

Example

@echo off 
FC lists.txt listsA.txt

Output

The above command will display the differences in the contents of the files (lists.txt and listsA.txt ) if any.

 

 

 

 

Example

@echo off 
Title New Windows Title

Output

The above command will change the title of the window to “New Windows Title”.

 

 

 

 

Example

@echo off 
set

Output

The above command displays the list of environment variables on the current system.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

沒有留言: