Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use batch for commands

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to use batch for commands", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "how to use batch for commands"!

The FOR command is basically used to process text, but there are other useful features!

Take a look at his basic format (here I am referring to the format in the batch, which only requires a% sign directly on the command line)

FOR parameter%% variable name IN (related file or command) command executed by DO

Parameters: FOR has 4 parameters / d / l / r / f their role I will explain with examples below

%% variable name: this variable name can be lowercase Amurz or uppercase Amurz. They are case-sensitive, and FOR will give him each read value.

IN: just write the format of the command.

(related files or commands): what FOR wants to read and assign to variables, see the following example

Do: just write the format of the command!

Executed command: write here what you want to do with the value of each variable.

You can enter for /? in CMD. Look at the help provided by the system! Make a comparison.

FOR% variable IN (set) DO command [command-parameters]

% variable specifies a parameter that can be replaced by a single letter.

(set) specify a file or group of files. You can use wildcards.

Command specifies the commands to be executed on each file.

Command-parameters specifies parameters or command line switches for specific commands. Now let's talk about the meaning of each parameter.

/ d is a directory only

If Set (that is, the "related file or command" I wrote above) contains wildcards (* and?), each item that matches the Set will be matched

Record (instead of a filegroup in the specified directory) executes the specified Command.

Format of system help: FOR / D% variable IN (set) DO command

It is mainly used for directory search, but does not search for files. Look at this example.

@ echo off for / d% I in (*) do @ echo% I pause

If you save it in the root directory of C disk, all the directory names under the C disk directory will be printed out, and none of the file names will be displayed!

Another one, for example, we need to type out the name of the folder under the current path with only 1-3 letters.

@ echo off for / d% I in Do @ echo% I pause

In this way, if you have a directory name with only 1-3 letters in the current directory, it will be displayed, but if not, it will not be displayed.

Topic to consider:

@ echo off for / d% I in (window?) Do @ echo% I pause

If you save it to disk C, what will be displayed? See for yourself!

The / D parameter can only display the name of the directory under the current directory, which we should pay attention to!

/ R recursion

Go to the root tree [Drive:] Path and execute the for statement in each directory of the tree. If no directory is specified after / R, it is considered the current directory. If the Set is just a period (.), only the directory tree is enumerated.

Format of system help: FOR / R [[drive:] path]% variable IN (set) DO command

As we know above, / D can only display the directory name under the current path, so now this / R is also related to the directory, what can he do? Don't worry, he is much more powerful than / D.

It can read all the file names under the current or your specified path, note that it is the file name, what is the use to see the example!

@ echo off for / r c:% I in (* .exe) do @ echo% I pause

Let's save this BAT to D disk anywhere and execute it, and I will see that he lists all the EXE files under the root directory of C disk and the subdirectory of each directory. Here c: is the directory.

One more

@ echo off for / r% I in (* .exe) do @ echo% I pause

Parameters are not the same, this command does not add that C: that is, the search path, so that he will use the current directory as the search path, such as your BAT you put him under the disaster prevention d: est directory, then he will list all the EXE files in the D: est directory and his subdirectories!

/ L iterative numerical range

Use the iteration variable to set the start value (Start#), and then step through a range of values until the value exceeds the set end value (End#). / L will execute iterative variables by comparing Start# with End#. This command is executed if Start# is less than End#,.

If the iteration variable exceeds End#, the command interpreter exits the loop. You can also use a negative Step# to incrementally execute values in this range. For example, (1) generate sequences 1 2 3 4 5), and (5) generate sequences (5 4 3 2 1).

The syntax is:

Format of system help: for / L% Variable in (Start#,Step#,End#) do Command

For example:

@ echo off for / l% I in (1meme 1me 5) do @ echo% I pause

Save the execution to see the effect, he will print 5 numbers from 1 2 3 4 5

This parameter means to add 1 at a time from 1 to 5!

Look at this example again.

@ echo off for / l% I in (1penny 1je 5) do start cmd pause

After the implementation is not startled, why more than 5 CMD windows, ha ha! What will happen if you change that (1mem1pyrrine 5) to (1mem1pence65535)?

Let me first tell you that 65535 CMD windows will be opened. You are strong if you don't die!

Of course, we can also change that start cmd to md% I so that we can set up a specified directory! The name is 1-65535.

After looking at this parameter that I have given the nature of destruction, let's look at the last parameter.

/ f

Detailed description of for with / F

For with / F is very useful, most commonly used in batches, and is used as follows:

Format:

FOR / F ["options"]% I IN (file) DO commandFOR / F ["options"]% I IN ("string") DO commandFOR / F ["options"]% I IN (command) DO command

This is probably the most commonly used and most powerful command, mainly used to deal with files and the output of some commands.

File represents one or more files

String stands for string

Command stands for command

["options"] optional

For FOR / F% I IN (file) DO command

File is the file name, according to the official saying, for will open the files in file in turn, and read each file into memory before moving on to the next file, divide each line into one element according to each line, ignore blank lines, see an example.

If the file a.txt contains the following:

Row 1 row 1 column 1 row 2 column 1 row 3 column

Row 2 row 1 column 2 row 2 column 2 row 3 column

Row 3 row 1 column 3 row 2 column 3 row 3 column

What command will you use if you want to display the contents of a.txt? Type,type a.txt, of course.

For can also do the same command:

For / f% I in (a.txt) do echo% I

Or first execute from parentheses, because it contains the parameter / f, so for will first open the a.txt, and then read out all the contents of the a.txt, treat it as a collection, and take each line as an element, so there will be such a collection.

{"Row 1, column 1, column 1, column 2, row 1, column 3", / / first element

"Row 2, column 1, column 2, row 2, column 3", / / second element

"Row 3, column 1, row 3, column 2, row 3, column 3" / / third element

There are only three elements in the collection, and each element is also replaced by% I in turn, and then execute the commands that follow do.

Specific process:

Replace "row 1, column 1, row 2, column 2, row 3" with% I, execute echo% I after do, showing "row 1, column 1, row 1, column 2, column 1, column 3"

Replace "2nd row, 1st column, 2nd column" with% I, execute echo% I, showing "2nd row, 1st column, 2nd column, 2nd row, 3rd column"

In turn, until each element has been replaced.

To enhance your understanding of the role of / f, please execute two commands to make a comparison:

For / f% I in (a.txt) do echo% I / / this will display the contents of the a.txt, because of the function of / f, it will read the contents of the a.txt.

For% I in (a.txt) do echo% I / / and this will only show the name a.txt and will not read its contents.

From the above study, we found that for / f defaults to each line as an element, but what if we want to break down each line into smaller contents? Don't worry, the for command also gives us more detailed parameters, making it possible for us to break each line into smaller elements.

They are: delims and tokens

Delims is used to tell for what to take on each line as a delimiter. The default delimiters are space and tab.

For example, again with the above file, we execute the following command:

For / f "delims=" I in (a.txt) do echo I

The result displayed is:

Row 1 column 1

Row 2 column 1

Row 3 column 1

Why is this so? Because there is the parameter delims, there is a space after =, which means that each element is divided by a space. By default, only the first element after the division is taken.

The execution process is as follows:

Divide the first element, "row 1, column 1, column 1, row 2, column 1, column 3" into three elements: "row 1, column 1", "row 2, column 1" and "row 3, column 1". By default, it only takes the first, that is, "row 1, column 1", then execute the command after do, and so on.

But there are limits. What if we want the second column of elements in each row?

At this point, tokens jumped out and said, I can do it.

What it does is that when you divide each row into smaller elements through delims, it controls which one or several to take.

Again with the above example, execute the following command:

For / f "tokens=2 delims=" I in (a.txt) do echo I

Execution result:

Row 1 column 2

Row 2 column 2

Row 3, column 2.

If you want to display the third column, change it to tokens=3.

At the same time, tokens supports the wildcard character *, as well as scope.

If you want to display the second and third columns, replace it with tokens=2,3 or tokens=2-3, or if there are more, tokens=2-10 and so on.

The command at this time is:

For / f "tokens=2,3 delims=" I in (a.txt) do echo I j

How to get an extra% j?

This is because you need to take two columns of each row after your tokens, replacing the second column with% I and the third column with% j.

And must be in English alphabetical order,% j cannot be replaced by% k, because I is followed by j

The execution result is:

Row 1, column 2, column 3.

Row 2 column 2 row 3 column

Row 3 column 2 column 3 row 3 column

If you use the wildcard character *, you treat the whole or the rest of the line as an element.

For example:

For / f "tokens=* delims=" I in (a.txt) do echo I

The execution result is:

Row 1 row 1 column 1 row 2 column 1 row 3 column

Row 2 row 1 column 2 row 2 column 2 row 3 column

Row 3 row 1 column 3 row 2 column 3 row 3 column

In fact, the execution result is the same as that of for / f% I in (a.txt) do echo% I.

Another example is:

For / f "tokens=2,* delims=" I in (a.txt) do echo I j

The execution result is:

Row 1, column 2, column 3.

Row 2 column 2 row 3 column

Row 3 column 2 column 3 row 3 column

Replace the second column with% I and all remaining columns with% j

Finally, there are skip and eol, which are simple, skip is to ignore the first line of the file, and eol is used to specify what symbol to ignore when a line begins.

For example:

For / f "skip=2 tokens=*"% I in (a.txt) Thank you for your reading, this is the content of "how to use batch for commands". After the study of this article, I believe you have a deeper understanding of how to use batch for commands, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 298

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report