Batch scripts traverse files in a specified folder

Source: Internet
Author: User
Batch Processing Script 1. traverse the file in the specified folder 1.1 command explanation

Command:For [parameter] % variable name in (matching character) Do (executed command)

Note: Each command must be separated by a space. There is a space between in and do. Otherwise, the command cannot be executed successfully.

PS: This section only describes how to use a batch script to execute the for command to traverse folders. Therefore, the following section does not describe the meaning of each command.

[]: Indicates that this command is optional.

[Parameters]: There are four parameter values:/d, /r, /l, /fWith no parameters, there are five scenarios in total

  • No parameter: traverses the files in the folder of the current path, but you can also(Matching character)Path specified in
  • /D: traverse the folders in the current path, but you can also(Matching character)Path specified in
  • /R [path]: traverses all files in the specified path in depth, and files in the subdirectory are also traversed. If no path is specified, the current path is used by default.
  • /L: When the parameter/L is used(Matching character)In this case()The rules for using parentheses are as follows:(start, step, end)At this time, the for command is equivalent to the for statement in Java.
  • /F: Used to parse the content in the file, which is not described in this section.

% Variable name: Similarfor(int i = 0; ; )Inint i = 0The difference is that when the for loop of batch processing traverses each subitem,% Variable name<=> Each subitem, that is& Variable nameIt already points to a specific subitem.

(Matching character): The content in the brackets after the in command () can be expressed as a wildcard, used to filter files in the folder according to the specified rules, such (*. TXT) indicates to traverse all the objects in the folder. file ending with txt

(Executed command): The preceding command can be used to obtain each specified sub-item in the folder. The next step is to write how to operate each sub-item here, functions of braces {} after the for command in Java

1.2 example

The following example is based on the file path and the batch processing script is test. bat.

  • No parameter:for %%i in (*) do ( echo %%i )

Result: All files in the current directory are traversed.

  • No parameter specified path:for %%i in (c:\softwares\VisualBat\*.url) do ( echo %%i )

Effect:(Matching character)File ending with. URL in the specified path

  • With parameters/D:for /d %%i in (*) do ( echo %%i )

Result: All folders in the current directory are traversed.

  • Path with parameter/D:for /d %%i in (c:\softwares\VisualBat\*) do ( echo %%i )

Effect: traverse all folders in the specified directory. % I points to the absolute path of each subfolders.

  • With parameters/R:for /r %%i in (*.url) do ( echo %%i )

Effect: The system traverses all objects ending with. URL in the current path in depth. % I points to the absolute path of the object because the object is traversed in depth.

  • Path with parameter/R:for /r c:\softwares\VisualBat\ %%i in (*.url) do ( echo %%i )

    Effect: Same

  • With parameter/L:for /l %%i in (1, 1, 5) do ( echo %%i )

Effect: equivalentfor (int i = 1; i <= 5; i++)Statement, start value, increment or decrease, and end value can be set by yourself

  • With parameters/D/R:for /d /r %%i in (*) do ( echo %%i )

Effect: The parameters can be used in combination./d Indicates traversing folders, And/R indicates in-depth traversing. Therefore, the preceding command is used to traverse all folders in the current directory in depth, including folders in subdirectories.

2. Basic usage of the Temporary Variable 2.1

Command:set key=value

Remember:key=valueThere must be no space between the three, and it cannot be added to the Java style to comply with the rules.int a = 1Variable declaration is different. Remember

Variable usage: % key %

Example:

@echo offset name=dasuAndroidTvecho %name%

Effect: name can be used as a temporary variable. You need to use % to enclose the variable name.

Limitations: Variables cannot be declared and used directly in the for command like in the previous step, as follows:

@echo offfor /l %%i in (0,1,5) do (    set name=dasuAndroidTv    echo %name%)

Effect: When declaring a temporary variable in the for command and using the % key % method directly, the following error occurs: ECHO is disabled, but ifset key=valueThe declaration of temporary variables is placed outside the for command. If the for command is only used internally, it is possible, as shown below:

@echo offset name=dasuAndroidTvfor /l %%i in (0,1,5) do (    echo %name%)

Effect: temporary variables are declared outside the for command. The for command is only used internally. This method is allowed.

2.2 usage of variables in the for command

Q: What should I do if I want to declare a temporary variable in the for command and use it?

Use of temporary variables in the for command:

  1. To enable the variable delay function, run the following command:setlocal enabledelayedexpansion
  2. Use temporary variables in the for command!key!Replace the form enclosed by the Han number%key%
  3. Reason: I am not clear about Google's solutions. I am interested in self-searching for in-depth research.

Example:

@echo offsetlocal enabledelayedexpansionset name=dasufor /l %%i in (0, 1, 5) do (    set name=dasuAndroidTv     echo !name!    echo %name%)

Effect: To put it bluntly, use the Temporary Variable % name % in the for command, the value of the temporary variable named name will always be the value assigned outside the for command. It will not take effect even if the variable is re-assigned by the set command in the for command.

If you want to use the temporary variable assigned by the set command in the for command immediately, you need two steps:setlocal enabledelayedexpansionIn the for command!name!To use temporary variables.

3. string processing 3.1 Truncation

Command:%key:~[start,num]%

Explanation: When%key%The:~To intercept the string pointed to by the key. The intercept operation supports the following forms:

  • InterceptN strings starting at the specified position:%key:~0,4%It indicates that the last four characters starting with subscript 0 are intercepted.
  • InterceptThe entire string starting at the specified position:%key:~4%To intercept the entire string starting with subscript 4.
  • InterceptSpecify the entire string at the start position in the reciprocal Mode:%key:~-2%To intercept the entire string starting from the last 2nd characters
  • InterceptSpecify the N strings after the start position in the reciprocal Mode:%key:-4,2%, Which is two characters starting from the last 4th characters
  • Combination of positive and negative values:%key:~2,-2%To intercept the string from subscript 2 to the last 2nd

Example:

@ Echo offrem (REM indicates the content followed by comments, similar to Java //) original string set name = dasuandroidtvrem: It indicates intercepting the last four characters starting from the subscript 0, output dasuecho % name :~ 0, 4% REM comment content: Indicates intercepting the entire string starting with subscript 4 and outputting androidtvecho % name :~ 4% REM comment content: captures the entire string starting from the last 2nd characters and outputs tvecho % name :~ -2% REM comments: 2 characters starting from the last 4th characters are intercepted, and androidecho % name: ~ is output :~ 4,-2% REM comment content: indicates to intercept the string from subscript 2 to the last 2nd, and output idecho % name :~ -4,2%

3.2 stitching

Command:%key1%%key2%

Explanation: The string to be spliced is directly followed by the concatenated string without any Concatenation Operators.

Example:

@ Echo offset name1 = dasuset name2 = androidtvecho % name1 % name2 % REM here is the comment: Output dasuandroidtv

3.3 Replace

Command:% Key: replaced string = replaced string %

Explanation: I don't want to explain it. Let's look at the example and it's easy to understand.

Example:

@ Echo offset name = whoandroidtvecho % name: WHO = dasu % REM here is the comment: Output dasuandroid

3.4 Special File Operations

If the file in a folder is traversed in the for command, you can use some special commands to obtain various information about the file. For example:

@echo offfor %%i in (*.txt) do (   echo %%i   echo %%~fi   echo %%~di   echo %%~pi   echo %%~ni   echo %%~xi      echo %%~ti   echo %%~zi)

Explanation: when you use the for command to traverse a file, % I varies depending on the for usage. For details, see section 1st. In this usage, % I points to each file name in the current directory, the complete file name.

In this case, you can use some special commands to obtain information about the file, such:

  • % ~ Fi: Obtains the absolute path information of the file.
  • % ~ Di: Obtains the drive letter of the file.
  • % ~ Pi: Obtains the path of the file, excluding the drive letter information.
  • % ~ Ni: Get the file name, excluding the extension information
  • % ~ XI: Get the file extension
  • % ~ Ti: Indicates the last modification time of the file.
  • % ~ Zi: Obtains the file size.
4. Complete example

Finally, let's take a specific scenario and use the knowledge learned in this article to consolidate it.

Scenario: traverse all APK files in the specified path directory and use a sign. JAR file, execute Java commands for each APK file to sign the file, sign. jar receives two parameters. One is the APK to be signed, and the other is the output APK. The signed APK must be named by replacing the unsign in the original file name with Google, and output in the same directory as APK.

APK path: C: \ Users \ suxq \ Desktop \ outputs

Sign. Jar path: C: \ Users \ suxq \ Desktop

Java SIGNATURE command example (requires that both the sign. jar and APK files are in the same path, you can use the following command ):

java -jar sign.jar meizi_1_3_0_debug_unsign.apk meizi_1_3_0_debug_google.apk

Batch Processing script code:

@ Echo offsetlocal enabledelayedexpansionset Sign = c: \ Users \ suxq \ Desktop \ sign. jarset apkpath = c: \ Users \ suxq \ Desktop \ outputsfor % I in (% apkpath % *. APK) Do (set oldapk = % ~ Nxi set outapk =! Oldapk: unsign = Google! Echo Java-jar % sign %! Oldapk! ! Outapk! Rem here is the comment content: Because the APK file and sign. jar files are all virtual, so an error will be reported during actual execution. Here, we only output the Java full-sentence command to check whether it will be correctly executed from the full-sentence command, if all these files are true. The real script should remove echo)

 

From: https://www.cnblogs.com/dasusu/p/9058554.html

 

Batch scripts traverse files in a specified folder

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.