For Loop in Windows bat script

Source: Internet
Author: User
Tags eol uppercase letter
Delete a file in a certain format in the directory] For/r f: \ study % I in (*. o) do del % I
DOS loop: One of the bat/batch processing for commands IN the format of "FOR [parameter] % variable name IN (related file or command)" at a.m: execute specific commands on each object in one or more files, strings, or command results to achieve the desired results. Note: when using the FOR command in the batch processing file, use % variable instead of % variable to specify the variable. Variable names are case sensitive, so % I is different from % I. about: The for command can contain or without parameters. The following parameters are supported:/d/l/r/f: --- FOR % variable IN (set) DO command [command-parameters] % variable specifies a single letter replaceable parameter. (Set) specifies one or more files. Wildcard characters can be used. Command specifies the command executed on each file. Command-parameters specifies a parameter or command line switch for a specific command. TTT example: for % I in (t *. *) do echo % I -- display the current directory and t *. * matched file (only the file name is displayed, but not the path) for % I in (d: \ mydocuments \\*. doc) do @ echo % I -- display the Directory d: \ mydocuments \ and *. file matching doc ===1. Parameter/d (the parameter can only display the directory name under the current directory) --- format: FOR/D % variable IN (set) DO command [command-parameters] this parameter is mainly used for Directory Search and does not search for files. The/D parameter can only display the directory name under the current directory. (TTT Special Note: Only the directories under the specified directory will be searched, and no more directories at the next level will be searched .) TTT example: for/d % I in (c :\\*) do echo % I -- display all directories under the c root directory for/d % I in (???) Do echo % I -- display the directory with only 1-3 letters in the current directory ===2. Parameter/R (search for all files that match the set in the specified path and all subdirectories) --- format: FOR/R [[drive:] path] % variable IN (set) DO command [command-parameters] This command searches for the specified path and all files that match the set in all subdirectories. Note that the path and all subdirectories are specified. 1. If the file name in set contains wildcards (? Or *), lists the directory specified by the/R parameter and all the files that match the set in the subdirectories below it, and the directories that do not match the file are not listed. 2. If set is a specific file name without wildcards, enumerate the directory tree (that is, list the Directory and all its subdirectories) (and add the specific file name to the end ), regardless of whether the specified file in the set exists. Example: for/r c :\\% % I in (*. exe) do echo % I -- list the C root directory and all the EXE files under each directory !!!! TTT example: for/r c: \ % I in (boot. ini) do echo % I -- enumerative all directories of drive C for/r d: \ backup % I in (1) do echo % I -- enumerative d \ backup Directory for/r c :\\ % I in (boot. ini) do if exist % I echo % I -- a good search command to list boot. ini directory ===3. Parameter/L (this set represents a sequence of numbers starting from the beginning to the end in incremental form. Negative Step can be used. --- format: FOR/L % variable IN (start, step, end) DO command [command-parameters] This set represents a sequence of numbers from start to end in incremental form. Example of negative Step TTT: for/l % I in (, 5) do @ echo % I -- output 1 2 3 4 5 for/l % I in (, 10) do @ echo % I -- output, 9 for/l % I in (100,-20, 1) do @ echo % I -- output, 80, 60, 40, 20 for/l % I in (, 5) do start cmd -- open five CMD windows for/l % I in (, 5) do md % I -- create from 1 ~ 5 five folders in total for/l % I in (, 5) do rd/q % I -- delete from 1 ~ 5. A total of five folders DOS loops: One of the details of bat/batch processing for commands (although detailed summary and descriptions in history ~~) Conversion from: role [parameter] % variable name IN (related file or command) DO command: for one or more files, each object in the string or command result executes a specific command to achieve the desired result. Note: when using the FOR command in the batch processing file, use % variable instead of % variable to specify the variable. Variable names are case sensitive, so % I is different from % I. about: The for command can contain or without parameters. The following parameters are supported:/d/l/r/f: --- FOR % variable IN (set) DO command [command-parameters] % variable specifies a single letter replaceable parameter. (Set) specifies one or more files. Wildcard characters can be used. Command specifies the command executed on each file. Command-parameters specifies a parameter or command line switch for a specific command. TTT example: for % I in (t *. *) do echo % I -- display the current directory and t *. * matched file (only the file name is displayed, but not the path) for % I in (d: \ mydocuments \\*. doc) do @ echo % I -- display the Directory d: \ mydocuments \ and *. file matching doc ===1. Parameter/d (the parameter can only display the directory name under the current directory) --- format: FOR/D % variable IN (set) DO command [command-parameters] this parameter is mainly used for Directory Search and does not search for files. The/D parameter can only display the directory name under the current directory. (TTT Special Note: Only the directories under the specified directory will be searched, and no more directories at the next level will be searched .) TTT example: for/d % I in (c :\\*) do echo % I -- display all directories under the c root directory for/d % I in (???) Do echo % I -- display the directory with only 1-3 letters in the current directory ===2. Parameter/R (search for all files that match the set in the specified path and all subdirectories) --- format: FOR/R [[drive:] path] % variable IN (set) DO command [command-parameters] This command searches for the specified path and all files that match the set in all subdirectories. Note that the path and all subdirectories are specified. 1. If the file name in set contains wildcards (? Or *), lists the directory specified by the/R parameter and all the files that match the set in the subdirectories below it, and the directories that do not match the file are not listed. 2. If set is a specific file name without wildcards, enumerate the directory tree (that is, list the Directory and all its subdirectories) (and add the specific file name to the end ), regardless of whether the specified file in the set exists. Example: for/r c :\\% % I in (*. exe) do echo % I -- list the C root directory and all the EXE files under each directory !!!! TTT example: for/r c: \ % I in (boot. ini) do echo % I -- enumerative all directories of drive C for/r d: \ backup % I in (1) do echo % I -- enumerative d \ backup Directory for/r c :\\ % I in (boot. ini) do if exist % I echo % I -- a good search command to list boot. ini directory ===3. Parameter/L (this set represents a sequence of numbers starting from the beginning to the end in incremental form. Negative Step can be used. --- format: FOR/L % variable IN (start, step, end) DO command [command-parameters] This set represents a sequence of numbers from start to end in incremental form. Negative StepTTT example: for/l % I in (, 5) do @ echo % I -- output 1 2 3 4 5for/l % I in (, 10) do @ echo % I -- output, 9 for/l % I in (100,-20, 1) do @ echo % I -- output, 80, 60, 40, 20for/l % I in (, 5) do start cmd -- open five CMD windows for/l % I in (, 5) do md % I -- create from 1 ~ 5 five folders in total for/l % I in (, 5) do rd/q % I -- delete from 1 ~ 5. Five folders in total. 4. parameters/F (Use File Parsing to process command output, strings, and file content .) --- This parameter is the most difficult and has many parameters. The for command can be used to analyze the file content, string content, or the output result of a command, set the option to get the expected result. The following is an explanation from a professional who feels a little too professional and thinks it is not easy to understand: [iteration and file parsing-Use File Parsing to process command output, strings, and file content. Use iteration variables to define the content or string to be checked, and use various options to further modify the resolution method. Use the options token option to specify which tokens should be passed as iteration variables. Note: When the token option is not used,/F checks only the first token. The file parsing process includes reading output, string, or file content, dividing it into independent text lines, and parsing each line into zero or more tokens. Then, call the for loop by setting the variable value as the token. By default,/F passes the first blank separator number of each line of each file. Skip empty rows.] ++ Format: FOR/F [\ "options \"] % variable IN (file-set) DO command [command-parameters] FOR/F [\ "options \"] % variable IN (\ "string \") DO command [command-parameters] FOR/F [\ "options \"] % variable IN (\ 'COMMAND \ ') DO command [command-parameters] or, if usebackq option is available: FOR/F [\ "options \"] % variable IN (file-set) DO command [command-parameters] FOR/F [\ "options \"] % variable IN (\ "string \") DO command [command- Parameters] FOR/F [\ "options \"] % variable IN (\ 'COMMAND \ ') DO command [command-parameters] TTT Description: The above is the help content IN WinXP, you can note that the two are identical, which is actually a system error. The second section "If usebackq option is available:" should be the following: FOR/F [\ "options \"] % variable IN (\ "file-set \") DO command [command-parameters] FOR/F [\ "options \"] % variable IN (\ 'string \') DO command [command-parameters] FOR/F [\ "options \"] % variable IN ('command') DO command [command-parameters] -- (The quotation mark in 'command' is a back quotation mark, which is the key on the left side of number 1 on the keyboard) ++ (TTT Description: The following is a detailed explanation, most of the Help content in the system and some errors (no wonder the for command is so difficult to learn) have been corrected by me .) 1) OPTION keyword explanation: eol = c: refers to the end of a line comment character (just one ). For example, eol =; -- ignore the rows whose names start with a semicolon; skip = n: the number of rows ignored at the beginning of the file. For example, skip = 2 -- ignore 2 rows; delims = xxx: indicates the delimiter set. This replaces the default delimiter set of spaces and the hop key. For example, [delims =,] -- specifies to use commas and spaces to separate strings. Tokens = x, y, m-n: indicates which symbol of each row is passed to the for itself of each iteration. This leads to the allocation of additional variable names. The m-n format is a range. Use the nth symbol to specify mth. If the last character in the symbol string is an asterisk, the extra variable will be allocated and accept the reserved Text of the row after the last symbol is parsed. For example: tokens = 2, 3 * -- pass the second and third symbols in each line to the for program body; tokens = 2, 3 *... I % -- the second string obtained is assigned to I %, the third string is assigned to j %, and the remaining string is assigned to k %. For usebackq, different versions of the system prompt different help, but it is helpful to understand, so the excerpt is as follows: (1), usebackq: use quotation marks (the key on the left side of number 1 on the keyboard '). If the usebackq parameter is not used, file-set indicates the file. quotation marks are not allowed, so space is not allowed. Double quotation marks are used to indicate the string, that is, \ "string \". single quotation marks are used to execute the command, that is, \ 'COMMAND \'. When usebackq is used: Both file-set and \ "file-set \" indicate files. When there is space in the file path or name, double quotation marks can be used to enclose the file. single quotation marks indicate strings, that is, \ 'string \ '; the quotation marks indicate command execution, that is, 'command '. (This is the help in WinXP) (2) usebackq: specifies that the new syntax is used in the following situations: a string that is enclosed in quotation marks when executed as a command; A single quotation mark is a text string command, and the file name can be expanded using double quotation marks in filenameset. The preceding two parameters can be used in combination. I can explain that the purpose of this parameter is to process file names with spaces. If the file name and path you want to process contain spaces, if you use them directly, you will be prompted that the file cannot be found. If you use double quotation marks to enclose the file name and path. At this time, it will be processed as a string instead of a file. This "usebackq" parameter is added to cope with this situation. If this parameter is used, the set of double quotation marks enclosed in brackets can be considered as a file. The real string must be enclosed in single quotation marks. The command must be enclosed in reverse quotation marks. 2) file-set is one or more file names. Before proceeding to the next file in file-set, each file has been opened, read, and processed. Processing includes reading files, dividing them into lines of text, and parsing each line into zero or more symbols. Then, call the For loop with the variable value of the string that has been found. By default,/F uses the first blank symbol separated by each line of each file. Skip blank lines. You can specify the optional \ "options \" parameter to replace the default resolution operation. The string with quotation marks contains one or more keywords that specify different resolution options. 3) % I: It is specified in the for statement. % j and % k are specifically described through the tokens = option. You can use tokens = to specify up to 26 characters in a row. If you do not try to illustrate a variable higher than the letter \ 'z \ 'or \ 'z. Remember that the FOR variable is a single letter, case sensitive, and global. At the same time, no more than 52 FOR variables are in use. (TTT supplement: Generally, only the first parameter is specified after tokens, for example, % I or % a. The second and more parameters are used later, automatically sort down in order. If % a is specified, % B is used to represent the second result, and % c represents the third result... After tokens is tested, multiple variable names are specified. If the test fails, it is not possible. Therefore, after token, you can only use the first variable name to use. If the variable name used exceeds % z or % Z, it cannot be used. I thought it would loop over: for example, % z can be used after % a or % A, but this is not allowed after testing. For example: for/f \ "tokens = 1, 2, 3 * delims =-, \" % y in (\ "aa bb, cc-dd ee \") do echo % y % z % A % a -- only the first two strings are output, and the other two variables are invalid .) ++ The following is an example provided by the system: FOR/F \ "eol =; tokens = 2, 3 * delims =, \" % I in (myfile.txt) do @ echo % I % j % k -- Note: each row in myfile.txt is analyzed, eol =; -- ignore the rows headers with semicolons; tokens = 2, 3 * -- pass the second and third symbols in each line to the for program body; delims =, -- Use commas and/or spaces to define the delimiter. % I -- The for program body statement references % I to obtain the first string (the second symbol in this example ), reference % j to get the second string (the third symbol in this example) Reference % k to get all the remaining symbols after the third symbol. (TTT Description: an obvious error in the above examples and descriptions. % I should be replaced with % I (The help has a clear description: Use % variable to specify a variable, instead of using % variable, misleading) ++ TTT: The following lists the examples I have made: 1. Example of the analysis file FOR/F \ "eol =; tokens = 1, 2 * delims =,-\ "% I in (d: \ test.txt) do echo % I % j % k2, an example of string analysis: for/f \ "tokens = 1, 2, 3 * delims =-, \" % I in (\ "aa bb, cc-dd ee \") do echo % I % j % k % l3. Analyze the command output example: FOR/F \ "tokens = 1 * delims = \" % I IN (\ 'set \') DO @ echo [% I ---- % j] If the usebackq parameter is used, the command is as follows and the result is exactly the same as the preceding one. 1. Example of the analysis file: FOR/F \ "usebackq eol =; tokens = 1, 2 * delims =,-\" % I in (\ "d: \ test.txt \") do echo % I % j % k2, analysis string example: for/f \ "usebackq tokens = 1, 2, 3 * delims = -, \ "% I in (\ 'aa bb, cc-dd ee \ ') do echo % I % j % k % l3, analysis command output example: (The names and values of environment variables in the current environment are enumerated .) FOR/F \ "usebackq tokens = 1 * delims = \" % I IN ('set ') DO @ echo [% I ---- % j] results can be easily understood. In the FOR command, the replacement of the FOR variable reference has been enhanced. You can use the following syntax :~ I-delete any quotation marks (\ ") and expand % I % ~ FI-extended % I to a fully qualified PATH % ~ DI-only expand % I to one drive letter % ~ PI-only expand % I to one path % ~ NI-only expand % I to one file name % ~ XI-only expand % I to one file extension % ~ The sI-extended path only contains the short name % ~ AI-extended % I to file attributes % ~ TI-extended % I to the file date/time % ~ ZI-expand % I to the file size % ~ $ PATH: I-find the directory of the environment variable in the path (TTT prompt: it is the directory of the PATH environment variable), and expand % I to the first fully qualified name found. If the environment variable name is not defined or the file is not found, the key combination is expanded to an empty string. In addition, you can combine modifiers to get multiple results: % ~ DpI-only expand % I to one drive letter and PATH % ~ NxI-only expand % I to one file name and extension % ~ FsI-only expand % I to a complete path name with a short name % ~ Dp $ PATH: I-searches the directory of the environment variable in the PATH and expands % I to the first drive letter and PATH found. % ~ FtzaI-extend % I to DIR similar to the output line. In the preceding example, % I and PATH can be replaced by other valid values. % ~ Syntax ends with a valid FOR variable name. Selecting an uppercase variable name similar to % I is easier to read and avoids confusion with case-insensitive key combinations. (The above is the help of the system) We can see that each line has an uppercase letter \ "I \", which is actually the variable we bring in FOR, FOR example: FOR/F \ "usebackq eol =; tokens = 1, 2 * delims =,-\" % x in (\ "d :\\ test.txt \") do echo % x % y % z here we need to change the x, y, z to % ~ Fx, % ~ Fy, % ~ Fz. ++ TTT exception: The following is a comprehensive example based on the preceding instructions. You can copy the example directly to notepad and save it as bat (any directory in disk C, you can see the expanded results intuitively. @ Echo offecho --- display \ "dir c: \ boot. ini/B/ah \ "for/f \" delims = \ "% I in (\ 'dir c: \ boot. ini/B/ah \ ') do echo does not extend the variable % ifor/f \ "delims = \" % I in (\ 'dir c: \ boot. ini/B/ah \ ') do echo extends the variable ~ FI % ~ Fi -- extended to a fully qualified path name for/f \ "delims = \" % I in (\ 'dir c: \ boot. ini/B/ah \ ') do echo extends the variable ~ DI % ~ Di -- only extend the variable to one drive letter for/f \ "delims = \" % I in (\ 'dir c: \ boot. ini/B/ah \ ') do echo extends the variable ~ PI % ~ Pi -- only extend the variable to one path: for/f \ "delims = \" % I in (\ 'dir c: \ boot. ini/B/ah \ ') do echo extends the variable ~ NI % ~ Ni -- only extend the variable to one file name. for/f \ "delims = \" % I in (\ 'dir c: \ boot. ini/B/ah \ ') do echo extends the variable ~ XI % ~ Xi -- only extend the variable to one file extension. for/f \ "delims = \" % I in (\ 'dir c: \ boot. ini/B/ah \ ') do echo extends the variable ~ SI % ~ Si -- the expanded path only contains the short name for/f \ "delims = \" % I in (\ 'dir c: \ boot. ini/B/ah \ ') do echo extends the variable ~ AI % ~ Ai -- extend the variable to the file attributes for/f \ "delims = \" % I in (\ 'dir c: \ boot. ini/B/ah \ ') do echo extends the variable ~ TI % ~ Ti -- extend the variable to the file date/time for/f \ "delims = \" % I in (\ 'dir c: \ boot. ini/B/ah \ ') do echo extends the variable ~ ZI % ~ Zi -- extend the variable to the file size for/f \ "delims = \" % I in (\ 'dir c: \ boot. ini/B/ah \ ') do echo extends the variable ~ $ PATH: I % ~ $ PATH: I -- find the directory of the environment variable in the PATH and extend the variable to the first fully qualified name echo --- the following display composite modifier to get multiple results ---: for/f \ "delims ==\" % I in (\ 'dir c: \ boot. ini/B/ah \ ') do echo extends the variable ~ DpI % ~ Dpi -- only extend the variable to one drive letter and path for/f \ "delims = \" % I in (\ 'dir c: \ boot. ini/B/ah \ ') do echo extends the variable ~ NxI % ~ Nxi -- only extend the variable to a file name and extension for/f \ "delims = \" % I in (\ 'dir c: \ boot. ini/B/ah \ ') do echo extends the variable ~ FsI % ~ FsI -- only extend the variable to a complete pathname with a short name for/f \ "delims = \" % I in (\ 'dir c: \ boot. ini/B/ah \ ') do echo extends the variable ~ Dp $ PATH: I % ~ Dp $ PATH: I -- find the directory of the Environment Variable listed in the PATH, extend the variable to the first drive letter and path found for/f \ "delims = \" % I in (\ 'dir c: \ boot. ini/B/ah \ ') do echo extends the variable ~ FtzaI % ~ Ftzai -- extend the variable to a DIRecho similar to the output line. echo --- display \ "dir C: \ WINDOWS \ system32 \ notepad.exe/B \ "for/f \" delims = \ "% I in (\ 'dir C: \ WINDOWS \ system32 \ notepad.exe/B \') do echo does not extend the variable % ifor/f \ "delims = \" % I in (\ 'dir C: \ WINDOWS \ system32 \ notepad.exe/B \ ') do echo extends the variable ~ FI % ~ Fi -- extend to a fully qualified path name for/f \ "delims = \" % I in (\ 'dir C: \ WINDOWS \ system32 \ notepad.exe/B \ ') do echo extends the variable ~ DI % ~ Di -- only extend the variable to one drive letter for/f \ "delims = \" % I in (\ 'dir C: \ WINDOWS \ system32 \ notepad.exe/B \ ') do echo extends the variable ~ PI % ~ Pi -- only extend the variable to one path for/f \ "delims = \" % I in (\ 'dir C: \ WINDOWS \ system32 \ notepad.exe/B \ ') do echo extends the variable ~ NI % ~ Ni -- only extend the variable to one file name. for/f \ "delims = \" % I in (\ 'dir C: \ WINDOWS \ system32 \ notepad.exe/B \ ') do echo extends the variable ~ XI % ~ Xi -- only extend the variable to one file extension: for/f \ "delims = \" % I in (\ 'dir C: \ WINDOWS \ system32 \ notepad.exe/B \ ') do echo extends the variable ~ SI % ~ Si -- the expanded path only contains the short name for/f \ "delims = \" % I in (\ 'dir C: \ WINDOWS \ system32 \ notepad.exe/B \ ') do echo extends the variable ~ AI % ~ Ai -- extend the variable to the file attribute for/f \ "delims = \" % I in (\ 'dir C: \ WINDOWS \ system32 \ notepad.exe/B \ ') do echo extends the variable ~ TI % ~ Ti -- extend the variable to the file date/time for/f \ "delims = \" % I in (\ 'dir C: \ WINDOWS \ system32 \ notepad.exe/B \ ') do echo extends the variable ~ ZI % ~ Zi -- extend the variable to the file size for/f \ "delims = \" % I in (\ 'dir C: \ WINDOWS \ system32 \ notepad.exe/B \ ') do echo extends the variable ~ $ PATH: I % ~ $ PATH: I -- find the directory of the environment variable in the PATH and extend the variable to the first fully qualified name echo --- the following display composite modifier to get multiple results ---: for/f \ "delims = \" % I in (\ 'dir C: \ WINDOWS \ system32 \ notepad.exe/B \ ') do echo extends the variable ~ DpI % ~ Dpi -- only extend the variable to one drive letter and path for/f \ "delims = \" % I in (\ 'dir C: \ WINDOWS \ system32 \ notepad.exe/B \ ') do echo extends the variable ~ NxI % ~ Nxi -- only extend the variable to a file name and extension for/f \ "delims = \" % I in (\ 'dir C: \ WINDOWS \ system32 \ notepad.exe/B \ ') do echo extends the variable ~ FsI % ~ FsI -- only extend the variable to a complete path name with a short name for/f \ "delims = \" % I in (\ 'dir C: \ WINDOWS \ system32 \ notepad.exe/B \ ') do echo extends the variable ~ Dp $ PATH: I % ~ Dp $ PATH: I -- find the directory of the Environment Variable listed in the PATH, extend the variable to the first drive letter and path found for/f \ "delims = \" % I in (\ 'dir C: \ WINDOWS \ system32 \ notepad.exe/B \ ') do echo extends the variable ~ FtzaI % ~ Ftzai -- extend the variable to DIRPauseTTT similar to the output line. Note: 1. In the preceding command, % ~ FsI cannot be displayed. It is estimated that it is a system error because % ~ FI is extended to a fully qualified path name, % ~ SI only contains short file names, which are in conflict with each other, so an error occurs. I do not know whether it is a system error or test us ~~ 2. If the above command is saved in another drive, the correct drive and path cannot be displayed. 3. If you want to % ~ Dp $ PATH: I is displayed normally. Make sure that the path of the environment variable is C: \ WINDOWS \ system32. The following describes in sequence: ++ 1 and ,~ I-delete any quotation marks (\ ") and extend % I --- the function of this variable is as described in his description. delete quotation marks! The rules for deleting quotation marks are as follows (BAT brother added !): 1. If both the start and end of a string have quotation marks, the quotation marks at the beginning and end are deleted. 2. If the end of a string does not exist, the quotation marks at the beginning of the string are deleted. 3. If there is quotation marks in the middle of the string, or, if there are quotation marks at the end, do not delete them. Add a tornado: Do not delete headers or even tails. Let's take a look at this example. First, create the "temp.txt" file. The content is as follows \ "1111 \" 2222 \ "3333 \" 4444 \ "44 \" 55 \ "55 \" 55. You can also create a BAT File Code as follows: @ echo offecho ^ \ "1111> temp.txt echo \" 2222 \ "> temp.txt echo 3333 ^ \"> temp.txt echo \ "4444 \" 44> temp.txt echo ^ \" 55 \ "55 \" 55> create a temporary file on temp.txt rem, note that escape characters must be added to unpaired quotation marks ^. DO not leave spaces FOR/F \ "delims = \" % I IN (temp.txt) DO echo % ~ After ipausedel temp.txt is executed, the echo of CMD is as follows: 1111 # The quotation marks before the string are deleted. 2222 # The quotation marks at the beginning and end of the string are deleted. 3333 \ "# No quotation marks before the string, the quotation marks at the end of the string are still 4444 \ "44 # The quotation marks at the front of the string are deleted, while the quotation marks at the middle are still 55 \" 55 \ "55 # The quotation marks at the front of the string are deleted, the quotation marks in the middle are retained. Press any key to continue .. .compare the content in the result with the content in temp.txt, we will find that the quotation marks for the rows 1st, 2, and 5 have all disappeared. This is to delete the quotation marks ~ I's done! ++ II. % ~ FI-extend % I to an example of a fully qualified path name: Save the Code wherever you are, and I will put it on the desktop. FOR/F \ "delims = \" % I IN (\ 'dir/B \ ') DO @ echo % ~ After fipause is executed, the following content is displayed: C: \ Documents and Settings \ Administrator \ Desktop \ test. batC: \ Documents and Settings \ Administrator \ Desktop \ test. vbs when I put % ~ In the code ~ Change fi to % iFOR/F \ "delims = \" % I IN (\ 'dir/B \') DO @ echo % ipause: test. battest. by comparing vbs, we can easily see that there is no path, which is the role of \ "extending % I to a fully qualified path name, that is, if the content of the % I variable is a file name, it will print the absolute path of the file, not just a file name, you can find the manual experiment by yourself! ++ 3.% ~ DI-only extend % I to a drive letter. For example, the Code is as follows. I still want to run it on the desktop! FOR/F \ "delims = \" % I IN (\ 'dir/B \ ') DO @ echo % ~ After executing the dipause command, the following C: C is displayed in CMD: my desktop has two files: test. bat, test. vbs, % ~ Di indicates that if the content of the variable % I is a file or directory name, it prints out the disk symbol of the file or directory! ++ 4.% ~ PI-only extend % I to one path. This usage is the same as above, it only prints the path and does not print the file name FOR/F \ "delims = \" % I IN (\ 'dir/B \ ') DO @ echo % ~ Pipause I won't hit the results. Let's copy the code and check the results. The following code is used to show the results! +++ V. % ~ NI-only extend % I to a file name. Only the file name FOR/F \ "delims = \" % I IN (\ 'dir/B \ ') is printed \') DO @ echo % ~ Nipause ++ 6.% ~ XI-only extend % I to one file extension. Only the file extension FOR/F \ "delims = \" % I IN (\ 'dir/B \ ') is printed \') DO @ echo % ~ Xipause ++ 7.% ~ The sI-extension path only contains short names. Print the absolute short file name FOR/F \ "delims = \" % I IN (\ 'dir/B \') DO @ echo % ~ Sipause ++ 8.% ~ AI-extend % I to the file attributes of the file. Print the attributes of the file. FOR/F \ "delims = \" % I IN (\ 'dir/B \') DO @ echo % ~ Aipause ++ 9.% ~ TI-extend % I to the file date/time to print the file creation date FOR/F \ "delims = \" % I IN (\ 'dir/B \') DO @ echo % ~ Tipause ++ 10, % ~ ZI-extend % I to the file size. Print the file size FOR/F \ "delims = \" % I IN (\ 'dir/B \') DO @ echo % ~ Zipause tornado supplement: In the above example, \ "delims = \" can be changed to \ "delims = \", that is, do not use separators +++ 11, % ~ $ PATH: I-find the directory of the environment variable in the PATH and extend % I to the first fully qualified name found. If the environment variable name is not defined or the file is not found, the key combination will be extended to the Null String. This is the last one. It is different from those above. Let me talk about it separately! Save the code as a batch and put it on the desktop. @ Echo offFOR/F \ "delims = \" % I IN ({notepad.exe ") DO echo % ~ $ PATH: ipause tornado supplement: the above Code shows that the result is C: \ WINDOWS \ system32 \ drivers, which prints out the absolute PATH of the attacker. If not, an error is printed! (TTT Description: Save it to the desktop. The running result is: the system cannot find the file named notepad.exe ". Check that this path exists in the environment variable path. The reason is unknown! Later I found out why it was originally a Chinese quotation mark. The above command should be written as: FOR/F \ "delims = \" % I IN (\ "notepad.exe \") DO echo % ~ $ PATH: I)

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.