Usage of the For command in Windows

Source: Internet
Author: User
Tags comparison table

In Windows, we often need to perform the same job in batches. Many users often search For third-party software online. In fact, in Windows, you can easily complete a lot of batch work by cleverly using the For command. To better understand our next practice, let's take a look at the usage of the For command. The specific format of the For command is as follows:

For % variable IN (set) do command [command-parameters],

% Variable -- specify a single letter replaceable Parameter

(Set) -- specifies one or more files. Wildcards can be used.

Command -- specify the command to be executed on each file

Command-parameters -- specify a parameter or command line switch for a specific command

When using the For command in a batch file, use "% variable" instead of "% variable" to specify the variable ". Variable names are case sensitive, so "% I" is different from "% I ". Because the usage and parameters of the For command are too many and the changes are complex, we will not list them one by one For space reasons. You can enter "/? "To get help. Next we will go directly to the actual situation and introduce the specific usage of the For command in combination with the actual situation.

1. Add funds to text files in batches

In many cases, we edit and process the document, should add a file including "mailing address, QQ, E-MAIL and other information", such as to the editorial department of the manuscript. If there is only one article, just enter it. But what if you have many such files? Do you need to manually enter the same article with one word? NO! There is no need to worry about this. We can add money to all files at once. If we want to add money to all text files in the "ABCDE" directory. Create a text file named address.txt, enter the communication address, QQ, E-mail, and other information, and save it to the root directory of the E drive.

Create a new text file and enter the following content:

Md e: TEMPabcde

For/r E: ABCDE % I in (*. txt) do copy % I + E: address.txt E: empABCDE

For/r E: empABCDE % I in (*. txt) do copy % I E: ABCDE

Echo y | del E: TEMPabcde

Echo y | rd E: TEMPabcde

After the input is complete, check and confirm the correctness, save it as "add pay. bat to the file", and save it to the root directory of the E disk. Now, you only need to double-click the batch processing file to add the payment for all the TXT files in the E: ABCDE folder.

Morning wind prompt: The For command specifies the parameter "% I" from all text files in the "E: ABCDE" folder; "/r" indicates that the parameter includes subdirectories.

2. Obtain the file list in batches

If you want to create a file list under a directory, such as a file list of a movie file, you can use the For command to easily complete it. Let's take a list Of all RM format movie files on drive F as an example:

First, create a text file named "column file list. bat" under the root directory of drive F, then enter the following content and save:

For/r F: % I in (*. rm) DO @ echo % ~ Fi> F: RMList.txt

Double-click this batch file to quickly list all movie files with the RM extension on drive F.

Morning wind prompt: The For command specifies the parameter "% I" to take the value from all files on the drive with the RM extension; "@ echo % fi" indicates that all RM files and their full paths are displayed. "> F \ RMList.txt" indicates that the output results are redirected to the F \ RMList.txt file, open the file to view the Detailed Directory List. "f" indicates the complete path. You can also use "% n" (file name), "% x" (File Extension), "% d" (drive), "% p" (PATH), "% s" (full path), "% t" (File date), "% z" (file size) and other parameters.

3. quickly delete empty folders on the disk

Suppose we want to clear the empty folder on disk D, first create a file named "kill. bat" in notepad and enter the following content in the file:

Dir d:/ad/B/s | sort/r> d: kill.txt

For/f % I in (d: kill.txt) DO rd % I

Echo y | del d: kill.txt

Search for and delete all the "Thumbs. db" files, and double-click the files. All the empty folders on disk D will soon disappear.

Morning wind prompt: "dir d:" indicates that all the directories on the column d disk include the list of subdirectories. The parameter "/ad" indicates all the directories in the column; the parameter "/B" indicates that only the folder name is listed, and the title information or abstract is not displayed. The parameter "/s" indicates searching, that is, searching for all folders and subfolders in drive D; "|" is the pipe symbol. The result of the Dir command is passed to the "sort" command; "sort" command indicates sorting; "/r" indicates sorting of reverse columns. The reason why the system needs to sort the empty folders in reverse order is that, after sorting the columns in reverse order, all subfolders on disk D will always be in front of the parent folder. In this way, when you use the For command to delete empty folders, empty subfolders are deleted first, so the empty parent folder can be deleted. Otherwise, you cannot delete any subfolders in the empty parent folder. "> D: kill.txt" is used to re-locate the screen to the "d: kill.txt" file.

In the For command, the parameter "/f" is used to allow the For command to read data from the specified file as a variable. For example, the result file "d: the kill.txt "file reads data, and each time a row is read, it is passed to the RD command. The RD command is used to delete every folder passed by the For command, because the RD command cannot delete folders with content, after the For command loop, all empty folders are deleted, and folders with content are retained intact.

4. Obtain the correspondence between all IP addresses and host names in the LAN in batches

If you are a network administrator, you must obtain a list of all IP addresses and host names in this segment. If you do not want to use third-party software, try the For command.

Enter the following command in the Command Prompt window:

For/L % I in (254, 192) DO ping-a-n 1 168. .. 0.% I | Find/I "reply"> E: IPAddress.txt

Now, after the command is executed, open the "E: IPAddress.txt" file. A complete comparison table between the IP address and the host name has been initially formed. You only need to edit and modify the table to print the output.

Morning wind prompt: The For command specifies that the parameter "% I" starts from 0 and increases by 1 each time until 254, that is, all the addresses of a Class C network segment. If the IP address is defined by the user's Lan, it should be set according to the actual situation, for example, "172.16.0"; "-a" indicates obtaining the host name; "-n-1" indicates that the number of ECHO times is 1, and the default value is 4. Find/I "reply" indicates to Find the responded host, that is, the host that has been pinged; "E: IPAddress.txt" indicates entering the screen echo content into the ": IPAddress.txt" file.

Related Article

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.