Parsing of a script file

Source: Internet
Author: User
Tags eol

Features of the script:
(1) separate the current path name where the script is located with "\"
(2) The separated elements into a variable of 10 letters in the A-j interval, if the directory does not have so many levels, the value of the relevant variable is empty
(3) Set relevant markers for future codedir of environment variables
(4) If a folder named codes is found, the full folder name from the drive to this folder is assigned to the variable Codedir, at which point the IF statement ends (subsequent if the value of the Codedir variable is not manipulated)
@ for/F "usebackq tokens=1-10 delims=\ "%%a in ('%~dp0 ') do (set a=%%a) @FOR/F"Usebackq tokens=1-10 delims=\"%%a in ('%~dp0 ') do (set b=%%b) @FOR/F"Usebackq tokens=1-10 delims=\"%%a in ('%~dp0 ') do (set c=%%c) @FOR/F"Usebackq tokens=1-10 delims=\"%%a in ('%~dp0 ') do (set d=%%d) @FOR/F"Usebackq tokens=1-10 delims=\"%%a in ('%~dp0 ') do (set e=%%e) @FOR/F"Usebackq tokens=1-10 delims=\"%%a in ('%~dp0 ') do (set f=%%f) @FOR/F"Usebackq tokens=1-10 delims=\"%%a in ('%~dp0 ') do (set g=%%g) @FOR/F"Usebackq tokens=1-10 delims=\"%%a in ('%~dp0 ') do (set h=%%h) @FOR/F"Usebackq tokens=1-10 delims=\"%%a in ('%~dp0 ') do (set i=%%i) @FOR/F"Usebackq tokens=1-10 delims=\" %%a in ('%~dp0 ') does (set j=%%j) @set dirend=false@set codedir=%a% @set v=%b%sif%v% NEQ s (if%dirend%==false (if%v% NEQ C Odes (@set codedir=%codedir%\%b%) Else @set dirend=true)) @set v=%c%sif%v% NEQ s (if%dirend%==false (if%v% NEQ CODEs (@s ET codedir=%codedir%\%c%) Else @set dirend=true)) @set v=%d%sif%v% NEQ s (if%dirend%==false (if%v% NEQ CODEs (@set CODED ir=%codedir%\%d%) Else @set dirend=true)) @set v=%e%sif%v% NEQ s (if%dirend%==false (if%v% NEQ CODEs (@set Codedir=%code dir%\%e%) Else @set dirend=true)) @set v=%f%sif%v% NEQ s (if%dirend%==false (if%v% NEQ CODEs (@set codedir=%codedir%\%f% else @set dirend=true)) @set v=%g%sif%v% NEQ s (if%dirend%==false (if%v% NEQ CODEs (@set codedir=%codedir%\%g%) Else @ Set dirend=true)) @set v=%h%sif%v% NEQ s (if%dirend%==false (if%v% NEQ CODEs (@set codedir=%codedir%\%h%) Else @set dire nd=true)) @set v=%i%sif%v% NEQ s (if%dirend%==false (if%v% NEQ CODEs (@set codedir=%codedir%\%i%) Else @set dirend=true) ) @set V=%j%sif%v% NEQ s (If%dirend%==false (if%v% NEQ CODEs (@set codedir=%codedir%\%j%) Else @set dirend=true)) @set Codedir=%codedir%\code@rem @echo%codedir% @FOR/F"Usebackq tokens=1-10 delims=\"%%a in ('%~dp0 ') do (set a=%%a) @FOR/F"Usebackq tokens=1-10 delims=\"%%a in ('%~dp0 ') do (set b=%%b) @FOR/F"Usebackq tokens=1-10 delims=\"%%a in ('%~dp0 ') do (set c=%%c) @FOR/F"Usebackq tokens=1-10 delims=\"%%a in ('%~dp0 ') do (set d=%%d) @FOR/F"Usebackq tokens=1-10 delims=\"%%a in ('%~dp0 ') do (set e=%%e) @FOR/F"Usebackq tokens=1-10 delims=\"%%a in ('%~dp0 ') do (set f=%%f) @FOR/F"Usebackq tokens=1-10 delims=\"%%a in ('%~dp0 ') do (set g=%%g) @FOR/F"Usebackq tokens=1-10 delims=\"%%a in ('%~dp0 ') do (set h=%%h) @FOR/F"Usebackq tokens=1-10 delims=\"%%a in ('%~dp0 ') do (set i=%%i) @FOR/F"Usebackq tokens=1-10 delims=\"%%a in ('%~dp0 ') do (set J=%%j) @FOR/F"Usebackq tokens=1-10 delims=\"%%a in ('%~dp0 ') do (set K=%%k) @FOR/F"Usebackq tokens=1-10 delims=\"%%a in ('%~dp0 ') do (set l=%%l) @FOR/F"Usebackq tokens=1-10 delims=\"%%a in ('%~dp0 ') do (set m=%%m) @FOR/F"Usebackq tokens=1-10 delims=\"%%a in ('%~dp0 ') do (set n=%%n) @FOR/F"Usebackq tokens=1-10 delims=\"%%a in ('%~dp0 ') do (set O=%%o) @FOR/F"Usebackq tokens=1-10 delims=\"%%a in ('%~dp0 ') do (set p=%%p) @FOR/F"Usebackq tokens=1-10 delims=\"%%a in ('%~dp0 ') do (set Q=%%q) @FOR/F"Usebackq tokens=1-10 delims=\"%%a in ('%~dp0 ') do (set r=%%r) @FOR/F"Usebackq tokens=1-10 delims=\"%%a in ('%~dp0 ') do (set s=%%s) @FOR/F"Usebackq tokens=1-10 delims=\" %%a in ('%~dp0 ') does (set t=%%t) @set Dirend=false@set v=%b%sif%v% NEQ s (if%dirend%==false (@set parname=%b%)) Else @set Dirend=true@set v=%c%sif%v% NEQ s (if%dirend%==false (@set parname=%c%)) Else @set Dirend=true@set v=%d%sif%v% NEQ s ( If%dirend%==false (@set parname=%d%)) Else @set Dirend=true@set v=%e%sif%v% NEQ s (if%dirend%==false (@set parname=%e%) else @set Dirend=true@set v=%f%sif%v% NEQ s (if%dirend%==false (@set parname=%f%)) Else @set Dirend=true@set V=%g%sif %v% NEQ S (if%dirend%==false (@set parname=%g%)) Else @set Dirend=true@set v=%h%sif%v% NEQ s (if%dirend%==false (@set P arname=%h%) Else @set dirend=true@set v=%i%sif%v% NEQ s (if%dirend%==false (@set parname=%i%)) Else @set Dirend=true@se T v=%j%sif%v% NEQ s (if%dirend%==false (@set parname=%j%)) Else @set Dirend=true@set v=%k%sif%v% NEQ s (if%DIREND%==FA LSE (@set parname=%k%)) Else @set Dirend=true@set v=%l%sif%v% NEQ s (if%dirend%==false (@set parname=%l%)) Else @set dir End=true@set V=%m%sif%v% NEQ S (if%dirend%==false (@set parname=%m%)) Else @set Dirend=true@set v=%n%sif%v% NEQ s (if%dirend%==false (@set PAR name=%n%) Else @set dirend=true@set v=%o%sif%v% NEQ s (if%dirend%==false (@set parname=%o%)) Else @set Dirend=true@set V=%p%sif%v% NEQ S (if%dirend%==false (@set parname=%p%)) Else @set Dirend=true@set v=%q%sif%v% NEQ s (if%dirend%==fals E (@set parname=%q%)) Else @set Dirend=true@set v=%r%sif%v% NEQ s (if%dirend%==false (@set parname=%r%)) Else @set Diren D=true@set v=%s%sif%v% NEQ s (if%dirend%==false (@set parname=%s%)) Else @set dirend=true@set v=%t%sif%v% NEQ S (if%di Rend%==false (@set parname=%t%)) Else @set Dirend=true@rem @echo%parname%

Related Key points analysis:
1, @ command
Indicates that the command after @ is not displayed.

Http://www.cnblogs.com/SunShineYPH/archive/2011/12/13/2285570.html

Example:

echo@echoecho Please wait a minite ... @echo 1please wait a minite ...

2,%~dp0
CD/D%~dp0The meaning isCD/D D:\QQ
%0Represents the batch itself D:\qq\a.bat
~DP is a variable extension
D is both expanded to the partition code D:
P is the extension to the path \qq
DP is the extension to the area code path d:\qq
%0 determins where the batch file is running from. I ' ve created a demo batch file in my Winnt System32 folder called X.cmd. Running This gives the results shown below
Http://www.cnblogs.com/taoxu0903/archive/2010/03/02/1676690.html

3. For

(1).the for format without any parameters is: for%%i in (Set) Do Command%%i A variable set as a file or a set of files is actually a collection. Wildcard characters are available, such as *.txt. Commandis the command to execute. See an example: The current directory has the following files: a. txt B.mp3 c.mp3 c.wma a.rm E.bat you want to show the file with the extension mp3 inside, what command would you use? It's dir, of course.dir*.mp3. Similarly, for can also be implemented, as follows: for%%i in (*.mp3) Do Echo%%i How is this understood? For is executed first from the parentheses, because the parentheses are. mp3, so for will first get all the files in the current directory with the extension mp3, and use them as a collection, and each file name is an element,
Like this {b.mp3,c.MP3}, then use%%i instead of each element, then do the command after do. Of course,()It is not limited to a file or wildcard character, there can be multiple, such as: for%%i in (*.mp3,*.wma) Do Echo%%i is also possible. Note: DoThe following can be followed by any command, not limited to echo, here is just a demo.

(2). Contains/L for the format: for -L%%i in (start, step,enddocommand  This represents a sequence of numbers in increments from start to end, step is step, or each increment is step such as:(1,1,5) will produce sequence 1 2 3 4 5, (1,2,9) will produce 1 3 5 7 9 sequence. What's the use of it? For example, you want to open 3 Notepad programs at the same time, so you can: for do Notpad.exe

(3). Contains/F for format: for/F ["Options"]%%i in (file) Do Command for/F ["Options"]%%i in ("string") Do Command for/F ["Options"]%%i in ('Command‘) Do CommandThis is probably the most common and strongest command to handle the output of files and some commands. File represents one or more files that string represents the command ["Options"] Optional for/f%%i in (file) Do CommandFile is the file name, according to the official, for will be opened in file files, and before the next file to read each file into memory, according to each line into a single element, ignoring blank lines, see an example. If file a.txt has the following content: 1th row 1th column 2nd row 1th column 3rd row 1th column you want to show aWhat command does the content in. txt use? Of course it's type,typeA.Txtfor can also complete the same command: for/f%%i in (a.txt) Do Echo%%i
From the above study, we find that for/F will default to each line as an element, but what if we want to break down each row to a smaller content?
Don't worry, the for command also provides us with more detailed parameters that make it possible to divide each line into smaller elements. They are: Delims and tokensdelims are used to tell for each row what to take as a delimiter, the default delimiter is a space and TAB key for example, or the above file, we execute the following command: Echo %%i shows the result: line 1th 1th column 2nd row 1th row 3rd row 1th column What is this? Because here is the delims parameter,= followed by a space, meaning that each element is separated by a space, the default is to take only the first element after the split.

But this is still a limitation, what if we want the second column of each row? At this time, tokens out to say, I can do it. What it does is that when you divide each line into smaller elements through delims, it controls which or which ones to take. Or the above example, execute the following command: for/F "tokens=2 delims="%%i in (a.txt) Do Echo%%i Execution Result: 1th row 2nd column 2nd row 2nd column 3rd row 2nd column If you want to display the third column, then replace it with tokens=3. The tokens supports wildcard characters *, as well as a limited range. If you want to display the second and third columns, change to tokens=2,3 or tokens=2-3, if there is more: tokens=2-10 or something like that. The command at this point is: for/F "tokens=2,3 delims="%%i in (a.txt) Do Echo%%i%%j How to give a%%j? This is because your tokens takes two columns of each row, replaces the second column with%%i, and replaces the third column with%%j. and must be in alphabetical order,%%J can not be replaced with%%k, because I followed by J execution result is: 1th row 2nd Column 1th row 3rd column 2nd row 2nd column 2nd row 3rd column 3rd row 2nd column 3rd row 3rd column to the wildcard character *, this line is all or the rest of the line as an element. For example: for/F "tokens=* delims="%%i in (a.txt) Do Echo%%i Execution Result: 1th row 1th column 1th row 2nd column 1th row 3rd column 2nd row 1th column 2nd row 2nd column 2nd row 3rd column 3rd row 1th column 3rd row 2nd column 3rd row 3rd column actually with for/f%%i in (a.txt) Do EchoThe result of%%i is the same. Again such as: for/F "tokens=2,* delims="%%i in (a.txt) Do Echo%%i%%j Execution results are: 1th row 2nd Column 1th row 3rd column 2nd row 2nd column 2nd row 3rd column 3rd row 2nd column 3rd row 3rd column with%%i instead of the second column, with%%j instead of the remaining all finally there is skip and EOL:
(a) Skip is to ignore the number of lines before the file
(b) EOL is used to specify that when a line begins with a symbol, it is ignored. For example: for/F "skip=2 tokens=*"%%i in (a.txt) Do Echo%%i Result: line 3rd 1th column 3rd row 2nd column 3rd row 3rd column use skip to tell for Skip the first two lines. If you don't add tokens=* The result is: Row 1th of line 3rd doesn't know what's going on. Another example, when a.txt content becomes:.1th row 1th column 1th row 2nd column 1th row 3rd column.2nd row 1th column 2nd row 2nd column 2nd row 3rd column 3rd row 1th column 3rd row 2nd column 3rd row 3rd column execute for/F "eol=. Tokens=* "%%i in (a.txt) Do Echo%%i Result: 3rd row 1th column 3rd row 2nd column 3rd row 3rd column with EOL to tell for ignore with "."begins with the line. You must also add tokens=*, otherwise it will only show "3rd row 1th column", still do not know what is going on.

Http://www.cnblogs.com/princessd8251/articles/3859021.html

eg

@ for/F "" Usebackq tokens=1-10 delims=\ "" Usebackq tokens=1-10 delims=\ "" Usebackq Tokens=1-10 delims=\ "" Usebackq tokens=1-10 delims=\ " %%a in ('%~dp0 ') do (set b=%%b) @set codedir=%a% @set v=% B%secho%codedir%echo%v%
 for/F "eol=; tokens=2,3* delims=, "%i in (myfile.txt) Do @echo%i%j%k This command parsing myfile. txt file, ignoring lines that begin with a semicolon, passing the second and third tokens from each row to forthe body (tokens are separated by commas or spaces).  forthe body of the statement references%i to obtain the second token, referencing%j to obtain a third token, referencing%k for all remaining tokens. If you provide a file name that contains spaces, use quotation marks to enclose the text (for example,"File Name"). To use quotation marks, you must use USEBACKQ. Otherwise, the quotation marks are interpreted as defining the literal string to parse. %i in forStatements explicitly, and%j and%k use tokens=implied declarations. Use tokens as long as it does not cause an attempt to declare a variable higher than the letter "z" or "Z"= can be specified up to 26a token. To parse the command output by placing filenameset between parentheses, type: for/F "Usebackq delims=="%i in ('Set`) Do @echo%i This example enumerates the environment variable names in the current environment. 

Http://www.cnblogs.com/vus520/archive/2013/01/09/2852584.html


4. If

The basic use of this usage is to determine the code for the execution of the previous command to determine the next step .
In general, the execution result code of the previous command has only two results ,"success" with 0 means "failure" is denoted by 1 .
Http://www.cnblogs.com/dongzhiquan/archive/2012/08/16/2643189.html

REM NEQ case-sensitive when comparing strings @set b=code@set dirend=false@set codedir=  test@Set v=%b%sif %dirend%==false (if %v% NEQ CODEs (@Set Else @set dirend=true)echo %codedir%

Other:

Bat execution, Chinese characters appear garbled:

Open the bat file in Notepad and save it in ANSI format. Before it was UTF-8.

Http://www.cnblogs.com/yank/p/3926643.html









Parsing of a script file

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.