For in DOS commands

Source: Internet
Author: User
Tags eol

The first few days to do the installation package, you need to get the name and path of all the folders in a directory. Originally wanted to write a paragraph in C + + code, and then think of DOS may meet the demand, so study for the command, unexpectedly very easy to achieve the purpose, it seems to underestimate the dos this doorway things.

Next to my needs, get all the DLL files in the subdirectory of the specified directory, the same name as the directory where the DLL is located.

Put my code on first.

 1  ::d ir directory full path  2  "  dir  %%a in  (.) do   ( 3  if  exist  %%~dpa%%~na\%%~na.dll  Span style= "color: #800000;" > "  ( 4  echo  " %%~dpa%%~na\%%~na.dll  "  5  )  6 ) 

A few words will come true, simple ~!!!

The for command also has a lot of powerful features, I still don't understand, look at the official help

Executes a specific command for each file in a set of files.  For%variable in (set) does command [command-parameters]%variable specifies a single-letter replaceable parameter. (set) specifies one or a set of files.  Wildcard characters can be used.  command specifies the commands to execute on each file. Command-parameters specify parameters or command-line switches for specific commands. When using the for command in a batch program, specify the variable using%%variable instead of%variable. Variable names are case-sensitive, so%i differs from%i. If you enable command extensions, other formats for the following for commands are supported: FOR/D%variable in (set) do command [Command-parameters] If the set contains a wildcard Matches the directory name instead of the file name.    FOR/R [[Drive:]path]%variable in (set) do command [command-parameters] checks the tree with [Drive:]path Root], pointing to the for statement in each directory. If the directory specification is not specified after/R, the current directory is used. If the set is only a single point (.) character, the directory tree is enumerated. FOR/L%variable in (start,step,end) does command [Command-parameters] The set represents a sequence of numbers in increments from start to finish. Therefore, (1,1,5) will generate sequence 1 2 3 4 5, (5,-1,1) will generate sequence (5 4 3 2 1) for/f ["Options"]%variable in (file-set) do command [Command-parame  ters]for/f ["Options"]%variable in ("string") does command [command-parameters]for/f ["Options]%variable in (' command ') Do command [command-parameters] or, if there is usebackq option: 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 ') does command [Command-parameters] fileset to one or more filenames. Before continuing to the next file in Fileset, each file is opened, read, and processed. Processing involves reading a file, dividing it into lines of text, and then parsing each line into 0 or more symbols.    The For loop is then called with the found symbol string variable value. By default,/F separates the first blank symbol from each line in each file.    Skips a blank line. You can override the default resolution action by specifying an optional options parameter. This quoted string includes one or more keywords that specify different parsing options.        These keywords are: eol=c-refers to the end of a line comment character (just one) skip=n-refers to the number of rows ignored at the beginning of the file. Delims=xxx-refers to the delimiter set.        This replaces the space and the default delimiter set for the jump bar. Tokens=x,y,m-n-Indicates which symbol per line is passed to the for itself for each iteration. This causes the allocation of additional variable names. The m-n format is a range. Specify MTH with the nth symbol. If the last character in the symbol string is asterisk, then the extra variable is assigned and accepted after the last symbol parsing.        Keep the text.  USEBACKQ-Specifies that the new syntax is used in the following class case: A string that executes a quotation mark as a command and a single quote character is a literal string command and allows the     File-set                     Use double quotation marks to expand the file name. Some examples may help: for/f "eol=; tokens=2,3* delims=, "%i in (myfile.txt) does @echo%i%j%k parse each row in myfile.txt, ignoring those lines that begin with a semicolon, passing the second and third symbols in each row to the for function body, Separate the symbols with commas and/or spaces. Note that the statement for the body of this for function references%i to get the second symbol, reference%j to get the third symbol, reference%k to get all the remaining symbols after the third symbol.
For filenames with spaces, you need to enclose the file name in double quotation marks. In order to use double quotes in this way, the USEBACKQ option is also required, otherwise the double quotes will be interpreted as defining a string to parse. %i is explicitly declared in the For statement, and%j and%k are implicitly declared through the tokens= option. You can specify up to 26 symbols by tokens= a line, as long as you do not attempt to declare a variable that is higher than the letter "z" or "Z". Keep in mind that the for variable is a single-letter, case-and global-variable, and that you cannot use more than 52 at a time. You can also use for/f parsing logic on adjacent strings by enclosing the file-set between parentheses in single quotes. In this way, the string is parsed as a single input line in a file. Finally, you can use the for/f command to parse the output of the command. The method is to change the file-set between parentheses into an enclosing string. The string is passed to a sub-CMD as a command line. EXE, its output is captured in memory and is parsed as a file. As shown in the following example: for/f "Usebackq delims=="%i in (' Set ') does @echo%i enumerates the environment variable names in the current environment. In addition, the substitution of the for variable reference has been enhanced. You can now use the following option syntax:%~i-Remove any quotation marks ("), extend%I%~fi-will%I Extend to a fully qualified pathname%~di-will only%I Extend to a drive letter%~pi-%I only Extend to a path%~ni-%I only Extend to a filename%~xi-%I only Extend to a file name extension%~si-The extended path contains only the short name%~ai-will%I File attributes extended to file%~ti-will%I Date/Time extended to file%~zi-will%I Scale to File size%~ $PATH: I-Find the directory that is listed in the PATH environment variable and%I Expands to the first fully qualified name found. If the environment variable name is not defined or the file is not found, this groupA key extension to an empty string can combine modifiers to get multiple results:%~dpi-only%I Extend to a drive letter and path%~nxi-%I only Extend to a file name and extension%~FSI-only%I Extend to a full pathname with a short name%~dp$path:i-search for the directory in the PATH environment variable and will%I Expands to the first drive letter and path found. %~ftzai-Will%I Extended to a similar output line dir in the example above,%I And PATH can be replaced by other valid values. The%~ syntax is terminated with a valid for variable name. Choose similar%I Uppercase variable names are easier to read and avoid being confused with key combinations that are not case-sensitive.

Finally say the idea: believe in yourself, there must be a simpler way to achieve your needs

For in DOS commands

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.