In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly describes how to use bat to intercept strings, the text is very detailed, has a certain reference value, interested friends must read!
First, list all the variables FOR:
~I -Remove any quotes ("), extend %I
%~fI -expands %I to a fully qualified pathname
%~dI -Extend %I to one drive letter only
%~pI -extend %I to only one path
%~nI -extends %I to only one filename
%~xI -extends %I to only one file extension
%~sI -Extended path contains only short names
%~aI -extends %I to file attributes of file
%~tI -Date/time %I was extended to file
%~zI -Extend %I to file size
%~$PATH:I -Find directories listed in path environment variable and extend %I
To find the first fully qualified name. If the environment variable name
Not defined, or file not found, this key combination extends to
empty string
We can see that each line has a capital letter "I," and that I is actually the variable that we're putting in FOR, whatever the variable name that we're putting in FOR statement, we're going to put in here.
For example:FOR /F %%z IN ('set') DO @echo %%z
Here we're substituting z, so we're going to change that I to z, for example %~fI to %~fz.
As for the content of %~p in front, it is syntax!
Okay, let's get started:
~I -Remove any quotes ("), extend %I
This variable works just as it says, remove the quotes!
Let's look at this example:
First we create a text file called temp.txt on the desktop and type these contents into it.
"1111
2222"
"3333"
44"44
Then create a BAT file with the following code:
FOR /F "delims=" %%i IN (temp.txt) DO @echo %%~i
pause
After execution, we see CMD echo as follows:
1111
2222"
3333
44"44
Compare this to the previous temp.txt, and we'll see that the quotes on the first and third lines are missing, which is why the quotes ~i are deleted!
The rules for deleting quotation marks are as follows (BAT brother supplement!)
1. If there are quotation marks at the beginning and end of the string, delete the quotation marks at the beginning and end;
2. If there are no quotation marks at the end of the string, delete the quotation marks at the beginning of the string;
If there are quotation marks in the middle of the string, or only quotation marks at the end, it is not deleted.
%~fI -expands %I to a fully qualified pathname
Take an example:
Save the code anywhere, I'll put it on the desktop.
FOR /F "delims==" %%i IN ('dir /b') DO @echo %%~fi
pause
After execution, the display content is as follows
C:\Documents and Settings\Administrator\Desktop\test.bat
C:\Documents and Settings\Administrator\Desktop\test.vbs
When I change %%~fi in the code to %%i
FOR /F "delims==" %%i IN ('dir /b') DO @echo %%i
pause
This will be displayed after execution
test.bat
test.vbs
By comparison, it's easy to see that there are no paths, and that's what "extend %I to a fully qualified pathname" does.
If the content of the %i variable is a file name, it will print the absolute path of the file, not just the file name.
Single print a file name, do your own experiments will know!
%~dI -Extend %I to one drive letter only
Take an example:
The code is as follows, I still put on the desktop execution!
FOR /F "delims==" %%i IN ('dir /b') DO @echo %%~di
pause
After execution, my CMD displays the following
C:
C:
My desktop has only two files test.bat,test.vbs,%%~di. The function is, if the content of variable %%i is a file or directory name, it will put this file
Or the directory where the disk symbol printed out!
%~pI -extend %I to only one path
This usage is the same as above, it only prints the path, not the file name
FOR /F "delims==" %%i IN ('dir /b') DO @echo %%~pi
pause
I will not play the results, we see the results of it, the following are a few such usage, code to come out, we see the results of it!
%~nI -extends %I to only one filename
Print only file names
FOR /F "delims==" %%i IN ('dir /b') DO @echo %%~ni
pause
%~xI -extends %I to only one file extension
Print file extensions only
FOR /F "delims==" %%i IN ('dir /b') DO @echo %%~xi
pause
%~sI -Extended path contains only short names
Print absolute short file names
FOR /F "delims==" %%i IN ('dir /b') DO @echo %%~si
pause
%~aI -extends %I to file attributes of file
Print the attributes of the file
FOR /F "delims==" %%i IN ('dir /b') DO @echo %%~ai
pause
%~tI -Date/time %I was extended to file
Print the date the file was created
FOR /F "delims==" %%i IN ('dir /b') DO @echo %%~ti
pause
%~zI -Extend %I to file size
Print file size
FOR /F "delims==" %%i IN ('dir /b') DO @echo %%~zi
pause
%~$PATH:I -Find directories listed in path environment variable and extend %I
To find the first fully qualified name. If the environment variable name
Not defined, or file not found, this key combination extends to
empty string
This is the last one, and it's different from all of the above, and I'll talk about it alone!
Let's start by creating a temp.txt file on the desktop and writing these things in it.
c:\windows
c:\windows\system32
c:\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:i
pause
CMD displays the following after execution
c:\WINDOWS
c:\WINDOWS\system32
ECHO is open.
Why is that? There's a line c:\windows\teett?
If we type echo %path% directly in CMD, it will display C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem.
%%~$PATH:i is to find the content in the text that matches the PATH variable value, and if the content is the same as the PATH variable, print it out.
If not, a blank line is displayed!
It only applies to PATH variables, not other variables, if you want to compare a lot of values, try assigning values to PATH variables, I didn't test!
Well, that's all there is to it!
Here are some examples:
bat Batch string substitution and string truncation functions
1. String substitution is good. Speaking of symbols, now say %PATH:str1=str2% The syntax above means: replace str1 with str2 in the string variable %PATH%.
The code is as follows:
@echo off
@color 02
set ALL=demo123
echo %ALL%
set VAT=%ALL:1=aaa%
echo %VAT%
---The result is demoaaa23.
pause
2. String interception **********************************************
Intercept a string of length n from m,m is offset (default is 0), n is truncation length (default is all),n can be negative
%a:~ 0, n % corresponds to the function left(a,n) taking n bits from the left %a:~-m% corresponds to the function right (a,m) taking m bits from the right %a:~m,n% corresponds to the function mid(a,m+1,n) taking n bits from m+1% a:~m,-n% corresponds to the function mid(a,m+1,len(a)-m-n), starting from m+1 bit to the reciprocal n+1 bit %a:~m % corresponds to the function mid(a,m+1,len(a)-m) or right(a,len(a)-m) taking all the right bits from m+1 bit.
%a:~[m[,n]]%
3:~0,1%
Results 1
bat intercepts strings
The code is as follows:
@echo off
set str=123456789
echo The first character is: %str:~0,1%
The first two characters of echo are: %str:~0,2%
The first 5 characters of echo are: %str:~0,5%
echo The string after removing the last character is: %str:~0,-1%
echo The string after removing the last 3 characters is: %str:~0,-3%
echo 4th character: %str:~3,1%
echo The fourth and subsequent three characters are: %str:~3,4%
echo Last character: %str:~-1%
echo Last character: %str:~-1,1%
echo Last two characters: %str:~-1,2%
echo Last 4 characters: %str:~-4,1%
echo The penultimate 4 characters and the following characters are: %str:~-4%
echo The penultimate 4 characters and 1 character after echo are: %str:~-4,2%
echo The penultimate 4 characters and the next 2 characters are: %str:~-4,3%
pause
To illustrate this problem, I'm here to take the batch characters, do a further explanation, hoping to inspire beginners
as follows:
echo %var:~n,k%
We'll specify each parameter here:"%var", the string from which we want to intercept characters. " ~ "
The symbol identifier (as I understand it),"n" we understand as a pointer,"k" we understand as an offset address. (Note)
: pointer and offset address are zero-based)
Let's use the example of namejm moderator to illustrate:
The code is as follows:
@echo off
set str=123456789
rem defines a str string as 123456789
echo The first character is: %str:~0,1%
rem pointer is 0, offset address is 1, i.e. starting from bit 0, take 1 bit
The first two characters of echo are: %str:~0,2%
rem pointer is 0, offset address is 2, i.e. starting from bit 0, take 2 bits
The first 5 characters of echo are: %str:~0,5%
rem pointer is 0, offset address is 5, i.e. 5 bits starting from bit 0
echo The string after removing the last character is: %str:~0,-1%
rem When "k" is negative, we can understand it by taking all the characters after the pointer from the beginning and subtracting
After "abs(k) bit".. So we can explain this sentence as follows: take all its characters from the 0 th position
is:123456789 and subtracts abs(k) bits from the back, so the final result is:12345678
echo The string after removing the last 3 characters is: %str:~0,-3%
Rem, this sentence is explained as above ↑
echo Last character: %str:~-1%
rem parameters "n," and "k" can be default, default "n," can be understood as: abs(k) from the beginning of all its
echo The penultimate 4 characters and the following characters are: %str:~-4%
rem explained as above ↑
echo Last character: %str:~-1,1%
When rem n is negative, it means that the character is intercepted from the back, taking k bits (n should start from 1 in this case)
echo Last character: %str:~-1,2%
rem explained as above ↑
echo Last 4 characters: %str:~-4,1%
rem explained as above ↑
echo The penultimate 4 characters and 1 character after echo are: %str:~-4,2%
rem explained as above ↑
echo The penultimate 4 characters and the next 2 characters are: %str:~-4,3%
rem explained as above ↑
pause
The above is "how to use bat to intercept strings" all the content of this article, thank you for reading! Hope to share the content to help everyone, more relevant knowledge, welcome to pay attention to the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.