Batch Process starter Manual batch processing Common DOS command posts (Echo, REM, CD, dir) _dos/bat

Source: Internet
Author: User
Tags current time manual echo command goto valid


1.1 Learning the Echo rem CD dir command

Learning points:
1. Display information: Echo
2. Comment Statement: REM
3. Directory Switching: CD
4. Column File name: dir

One, display information: Echo

Before we learn the echo command, we'll build a new folder bat under C disk and build an empty bat file. For instance, I'm mybat.bat.
We began the first chapter of section 1.1 of the Getting Started handbook on batch processing.

1. Output hint information
Format: echo the information you want to output

Example 1

echo Hello,world
echo I'll come back
pause

Perform the display:
C:\bat>echo Hello,world
Hello,world
C:\bat>echo I'll come back
I'll come back
C:\bat>pause
Please press any key to continue ...

We write the batch process, can it not show us what exactly is the command we wrote? Ok! So let's learn the second usage of Echo!

2. Turn off the echo of this command
Format: @ Put in front of command

Example 2

@echo Hello,world
@echo I'll come back
pause

Perform the display:
Hello,world
I'll come back
C:\bat>pause
Please press any key to continue ...


3. Turn on echo or turn off echo feature
Format: Echo [{On|off}]
Explain that if you add the echo off before the batch command, the command echoing in the batch will be closed. Of course, if you don't want to show echo off,
Command itself, you can add @ to the front of echo off.

Example 3

@echo off
Echo Hello,world
echo I'll come back
pause

Perform the display:
Hello,world
I'll come back
Please press any key to continue ...

Before the batch is preceded by @echo off, it causes all commands to no longer display the command itself, including, of course, the echo off command itself does not echo
Up.

4. Output blank line, which is equivalent to entering a carriage return
Format: Echo.
Notice the "." In the command. To be followed by the Echo, there should be no space in the middle, otherwise "." will be output to the screen as a cue message. Another "." OK
Use,:; "/]+\ any of these symbols instead.

Example 4

@echo off
Echo I'm writing a batch tutorial Oh!
Echo.
Echo, do you see a blank line up there?
Pause

Perform the display:
I'm writing a batch tutorial.

Do you see a blank line up there?
Please press any key to continue ...

Command echo. The output of the carriage return, through the DOS pipe steering can be used as the input of other commands, such as echo.| Time is equivalent to giving the time command after execution
Out a carriage return. Therefore, the system automatically returns to the DOS prompt after the current time is displayed.

5. Answer a question in a command
Format: ECHO Reply |-command expression
Function: Through Pipeline Command | Transmit "reply" as input to the following "command expression" and as input to the "command expression". (Note
Input method for Pipeline command |, SHIFT key \)

Example 5

@echo off
rd/s c:\abc
Pause

Perform the display:
C:\ABC, do you confirm (y/n)?

Explain, here ABC is a not empty folder, with the RD command to delete when it will ask you y or N, then you need to manually input y/n.
OK, we'll change the batch, and the system will automatically help us to confirm the Y input.

Example 6

@echo off
Echo y|rd/s c:\abc
Pause

6. Create a new file or increase the contents of a file
Format: Echo file content > FileName

Example 7

@echo off
echo 123>myfile.txt
Pause

This allows us to create a myfile.txt file in the current directory with a file content of 123. If you want to build an empty new file, then you
You can try this.

Example 8

@echo off
echo >myfile.txt 
Pause

Note: The pause in all of the above examples is the pause command, which is mainly used to pause the demo for everyone to see, haha!!

Second, note statement: REM

REM is an annotation command that is generally used to annotate the program, and the content after the command is not executed but can be returned.

Example 1

@echo off
Echo This is example 1 Oh!
The Echo statement above REM is used to display hints.
Pause

In addition:: Also can play the annotation function of REM, and more concise and effective; But there are two points to pay attention to!

First, any word rows that begins with a colon is treated as a label in a batch and ignores all of the contents immediately thereafter.
Valid label: The colon is followed by a string that begins with an alphanumeric character, which can be identified by a goto statement.

Invalid label: After a colon followed by a non-alphanumeric special symbol, goto unrecognized label, can play a note
function, so:: Often used as an annotation symbol, in fact: can also play a role in annotation.

Example 2

@echo off
Echo This is Example 2 Oh! :
: The echo statement above is used to display hints.
Pause

Second, unlike REM,:: The following character rows does not echo when executed, regardless of whether the command line echoing state is opened with Echo on
Because the command interpreter does not consider him to be a valid command line, for this point, rem in some cases
will be better than:: more applicable; In addition, REM can be used in Config.sys files.

Third, directory switching: CD

1. Switching to the same partition
Directory Toggle Format: Format: CD [disk] [path]

If the current directory is C:\Documents and Settings\mzybar, we want to switch to a different directory under the same partition, see the example below. (
Note: the > in the following example represents the current directory in front of the prompt at the command line,>. )

Example 1
Switch to C:\WINDOWS directory

C:\Documents and settings\mzybar> 
cd C:\WINDOWS
c:\windows>

Example 2
Switch to C:\WINDOWS\system32 directory

C:\Documents and settings\mzybar>        
cd C:\WINDOWS\system32
c:\windows\system32>

If the current directory is C:\WINDOWS\system32, we want to return to the previous level directory, that is, when we return to C:\WINDOWS, we can:

Example 3

C:\windows\system32>
cd ...
C:\windows>

Here, CD. Is it a lot simpler? Oh! Of course, you can also CD C:\WINDOWS.


If the current directory is C:\WINDOWS\system32, we want to return to the root directory, that is, when you return to C:\, you can try this:

Example 4

c:\windows\system32>
cd\
c:\ >

When we return the root directory here, we use the cd\ command, and of course you can also CD C:\

In fact, in DOS commands. 、.. and \ Both represent relative paths,. Is the current directory, ... Is the previous level directory, and \ represents the root directory. Let's try it.
Cd.

Example 5

C:\windows\system32>
CD.
C:\windows\system32>

See, when typing a CD. After the original position, because. is the current directory, switching to the current directory certainly does not change ROM.

2. Switching from different partitions
Format: Format: cd/d [disk] [path]
If the current directory is C:\Documents and Settings\mzybar, we need to switch to another partition directory, see:

Example 1
Switch to D:\123\ABC

C:\Documents and settings\mzybar>
cd/d d:\123\abc
d:\123\abc>

If we want to switch to the root of the D disk, there is a simpler way, see:

Example 2
C:\Documents and Settings\mzybar>
D:
D:\>

Here we can switch to its root directory by simply entering the drive letter colon.

3. Another use of the CD: Displays the current full path, which is typically referenced by%cd%.

Example 1

@echo off
Echo current path is%cd%
pause

The execution shows the current full path.

Four, column file name: dir

Displays a list of files and subdirectories in the directory.
DIR [Drive:][path][filename] [/a[[:]attributes]] [/b] [/C] [/d] [/l] [/n]
[/o[[:]sortorder]] [/P] [/Q] [/S] [/t[[:]timefield]] [/w] [/x] [/4]

[Drive:] [Path] [FileName] Specifies the drives, directories, and/or files to be listed.

/A displays the file with the specified attributes.
Attributes D Directory R read-only file
H hidden file a file to be archived
S System file-prefix indicating "no"
/b uses an empty format (no header information or summary).
/C Displays the thousand separator in the file size. This is the default value. Use/-c.
Disable separator display.
/d is the same as wide, but the files are listed by column category.
/l in lowercase.
/n The New Long column table format, where the filename is on the far right.
/O lists files in sorted order.
SortOrder N by name (alphabetic order) S by size (from small to large)
E by extension (alphabetic order) D by date/time (from)
G Group Directory Precedence-prefix of reverse order
/p pauses after each information screen.
/q Displays the file owner.
/S Displays the files in the specified directory and all subdirectories.
/T controls the time character fields that are displayed or used to classify.
Timefield C creation Time
A Last access time
W Last Write Time
/w with a wide list format.
/x Displays a short name resulting from a 8dot3 filename. Format is a format of/n.
The short name is inserted in front of the long name. If there is no short name, the position is
Displays white space.
/4 display year with four digits

About dir command more, see above a lot of parameters head dizzy, hehe ~ Here we learn some commonly used on the OK ~

1. List the directories and files under C:\Windows
Format: DIR [Drive:][path][filename]

Example 1
Dir c:\windows
There are obvious drawbacks to this approach--when there are too many directories and files in the list that can't be fully displayed, we'll solve the problem with the/p parameter.


/p pauses after each information screen.
Example 2
dir/p C:\Windows
At the end of the command, we add a parameter of "/P" (that is, the abbreviation for page), and the column file uses the paging display, which lists the screen
After recording and file, the prompt "Press any key to continue ...", press any key to display the next screen. This way you can view it screen by layer!


/b uses an empty format (no header information or summary).
Example 3
Dir/b C:\
When we look at it, we find that the listed files have date, time, creator and so on, we can add the/b parameter after the command, so that it only lists the text
The name of the piece.

2. Lists the files for the specified properties under C:\
Format: dir/a: Attributes (here: The number can be omitted)
Attributes D Directory R read-only file
H hidden file a file to be archived
S System file-prefix indicating "no"

Example 1
dir/a C:\
Lists all the files under C:\.

Example 2
Dir/ah C:\
Lists the hidden files (including files and folders).

Example 3
Dir/ad C:\
Lists all the folders under C:\.

Example 4
Dir/ad-h C:\
Lists all the folders under C:\, but does not contain hidden folders.

3. List the files under C:\ by category
Format: dir/o: SortOrder (here: The number can be omitted)
SortOrder N by name (alphabetic order) S by size (from small to large)
E by extension (alphabetic order) D by date/time (from)
G Group Directory Precedence-prefix of reverse order

Example 1
Dir/on C:\
/o: The Sort by parameter specifies how the dir command will be sorted when the command result is displayed, and the sort set to "n" that represents the alphabetical order of the file name
(from A~z, etc.).

Example 2
Dir/ogn C:\
The folders are prioritized, and the folders and files are listed in alphabetical order.

★ "/O" and "/a" can omit the following colon ":", but there is no space between. such as "/o:n" after the omission of the shape
The type should be "/on".

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.