Excerpt from: http://www.jb51.net/article/50354.htm
The files in the folder need to be processed with batch commands today, using the for command in bat and some of the commands that the string intercepts. Find a lot of useful information in the links above, in order to find and disseminate useful knowledge in the future, excerpt here.
Criticism of the original works
There are some variables in the for command, their usage many novice friends still do not understand, today to explain their usage!
first list The for variables: ~i -Remove any quotation marks ("), expand%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, the key combination expands to an empty string
We can see that each row has an uppercase letter "I", which is actually what we are going to bring in for the variable, what is the name of the variable we are substituting for the for statement, and what is written here.
For example: for/f
%%z in (' Set ') do @echo
%%z
The variable name we're substituting here is Z then we're going to change that I to Z, for example%~fi to%~FZ
As for the previous%~p such content is the grammar!
Good start: ~i-Remove any quotation marks ("), expand%i The function of this variable is like his description, delete the quotation marks! Let's take a look at this example: first we create a text file on the desktop named Temp.txt, in which we enter the contents "11112222" "3333"44 "44Then create a bat file with the following code:for/f "delims="%%i in (Temp.txt)Do@echo%%~iPauseAfter execution,We look at the echo of CMD as follows: 11112222 "333344 "44In contrast to the previous temp.txt, we'll see that the quotes from the first and third lines are gone,This is the effect of removing quotes ~i! The Delete quotation mark rule is as follows (Bat Brother added!) 1If the string is quoted at the same time, the end-to-end quotation marks are removed; 2If there is no quotation mark at the end of the string, the quotation marks of the first string are removed; 3, if there is a quotation mark in the middle of the string, or only in the trailing quotation marks, it is not deleted. %~fi-Will%I Extend to a fully qualified path name look at the example: Save the code in any place, I'll put it on the table.for/f "delims=="%%i in ('Dir/b ')Do@echo%%~fiPauseAfter execution, the contents are displayed as follows C:\Documents and settings\administrator\ desktop \test.Batc:\documents and settings\administrator\ desktop \test.VBS when I change the code%%~FI directly to%%ifor/f "delims=="%%i in ('Dir/b ')Do@echo%%iPauseThe content is displayed after the test is executed.Battest.VBS By contrast, we can easily see that there is no path, which is "will%I Extended to a fully qualified pathname "If the content of the%i variable is a file name, he will print out the absolute path where the file is located,Instead of just printing a file name,You know it under your own hands.!%~di-%I only Extend to a drive letter See example: code below,I still put it on the desktop to do it!for/f "delims=="%%i in ('Dir/b ')Do@echo%%~diPauseAfter executing my cmd shows the following c:c: My desktop is two files Test.bat,test.vbs,%%~di function is, if the variable%%i content is a file or directory name,He will print out the disk symbol of his file or directory!%~pi-only%I Extend to a path this usage is the same as above,He only prints the path does not print the file namefor/f "delims=="%%i in ('Dir/b ')Do@echo%%~piPauseI will not hit the result, we copy the code to see the results, the following are the use of this, the code is given out,Let's see the results for yourselves.!%~ni-%I only Extend to a file name to print only the filenamefor/f "delims=="%%i in ('Dir/b ')Do@echo%%~niPause%~xi-will only%I Extend to a file name extension to print only the file extensionfor/f "delims=="%%i in ('Dir/b ')Do@echo%%~xiPause%~si-Extended path only contains short name print absolute short file namefor/f "delims=="%%i in ('Dir/b ')Do@echo%%~siPause%~ai-Will%I Extended file properties to files properties of the print filefor/f "delims=="%%i in ('Dir/b ')Do@echo%%~aiPause%~ti-Will%I Date extended to File/Date when the print file was createdfor/f "delims=="%%i in ('Dir/b ')Do@echo%%~tiPause%~zi-Will%I Size of the file to be expanded to the size of the print filefor/f "delims=="%%i in ('Dir/b ')Do@echo%%~ziPause%~$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, the key combination expands to an empty string this is the last one, and the above are not the same,I'll say it alone! First, we build a temp.txt file on the desktop,It's written in this. C: \Windowsc:\windows\System32c:\windows\Teett then save the code as a batch and put it on the desktop.for/f "delims=="%%i in (Temp.txt)do @echo%%~$path:ipausewindowsc:\windows\system32 echo is open. Why is this, there is a line c:\windows\teett? We enter echo%path % will show C:\WINDOWS\system32; C:\WINDOWS; C:\windows\system32\wbem%%~$path:i is in the text to find the content matching the value of the path variable, if the contents of the same as the path variable, print it out if it's not the same, I'm not testing! OK, the for's variable is introduced here!
BAT batch string substitution function and string intercept function
1, the string is replaced, the symbol says this, now say%path:str1=str2% the above syntax means: Replace the str1 in the string variable%path% with the STR2
@echo off all=set demo123echo %all%set vat=%all:1=aaa% echo %vat%---result is demoaaa23pause
2, the string interception ********************************************** interception function Unified Syntax format:%a:~[m[,n]]% ********************************* *************
from M to intercept N-length string, M is offset (default = 0), n is intercept length (default is all), n can be negative
%a:~0,n% equivalent to the function left (a,n) takes the N-bit%a:~-m% equivalent function right (A,M) takes the M-bit%a:~m,n% equivalent to the function mid (a,m+1,n) starting from the M+1 bit to fetch n bits%a:~m,-n % is equivalent to the function mid (A,m+1,len (a)-m-n), starting from the m+1 bit, to the reciprocal n+1 bit%a:~m% equivalent to the function mid (A,m+1,len (a)-m) or right (A,len (a)-m), starting from the m+1 bit to take all the rights. %a:~[m[,n]]%%123:~0,1%
Results 1
Bat Intercept string
@echo offSetstr=123456789EchoThe first character is:%str:~0,1%EchoThe first two characters are:%str:~0,2%EchoThe first 5 characters are:%str:~0,5%EchoThe string after the last character is removed:%str:~0,-1%EchoThe string after the last 3 characters is removed:%str:~0,-3%EchoThe 4th character is:%str:~3,1%EchoThe 4th and subsequent 3 characters are:%str:~3,4%EchoThe last character is:%str:~-1%EchoThe last character is:%str:~-1,1%EchoThe last two characters are:%str:~-1,2%EchoThe 4th character to the bottom is:%str:~-4,1%EchoThe 4th and subsequent characters of the countdown are:%str:~-4%EchoThe 4th and subsequent 1 characters are:%str:~-4,2%EchoThe 4th and subsequent 2 characters are:%str:~-4,3%Pause
To illustrate this problem, I am here to take the batch processing character, do a further explanation, I hope to be inspired by the following: Echo%var:~n,k%
Here's a description of each parameter: "%var", which is the string from which we want to intercept the character. "~" to take the word
Symbol (as I understand it), "n" We interpret it as a pointer, "K" we interpret it as an offset address. (Note
: The pointer and offset address are all zero-based) we still use the NAMEJM moderator example to do the following:
@echo offSetstr=123456789REMdefines a str string as 123456789EchoThe first character is:%str:~0,1%REMThe pointer is 0 and the offset address is 1, which starts with the No. 0 bit and takes 1 bitsEchoThe first two characters are:%str:~0,2%REMThe pointer is 0 and the offset address is 2, which starts with the No. 0 bit and takes 2 bitsEchoThe first 5 characters are:%str:~0,5%REMThe pointer is 0 and the offset address is 5, which starts with the No. 0 bit and takes 5 bitsEchoThe string after the last character is removed:%str:~0,-1%REMwhen "K" is negative, we can understand that by starting at the beginning of the pointer, take all the characters that follow it, and subtractBack "abs (k) bit". So this sentence we can do the following explanation: Takes all of its characters starting from the No. 0 bitis: 123456789 then subtract abs from the back(k) bit, so the final result is: 12345678EchoThe string after the last 3 characters is removed:%str:~0,-3%REMexplanation of the sentence ibid. ↑EchoThe last character is:%str:~-1%REMparameters "N," and "K" can all be default, the default "N," can be understood as: from the ABS (k) bit start to take all itsEchoThe 4th and subsequent characters of the countdown are:%str:~-4%REMexplanation ibid. ↑EchoThe last character is:%str:~-1,1%REMwhen n is negative, the character is truncated from the back, and the K bit is taken (n should start at 1)EchoThe last character is:%str:~-1,2%REMexplanation ibid. ↑EchoThe 4th character to the bottom is:%str:~-4,1%REMexplanation ibid. ↑EchoThe 4th and subsequent 1 characters are:%str:~-4,2%REMexplanation ibid. ↑EchoThe 4th and subsequent 2 characters are:%str:~-4,3%REMexplanation ibid. ↑Pause
Bat intercept string (for command) recommended collection