Windows Batch learning (character swapping)---04

Source: Internet
Author: User

Transferred from: https://www.cnblogs.com/DswCnblog/p/5432326.html

1 , intercepting strings
The Intercept string can be described as one of the most commonly used sub-functions in string processing, enabling the ability to intercept one or more characters in a particular position in a string. Illustrate its basic features:

@echo off
Setlocal enabledelayedexpansion
:: Coder by DSW Powerd by IBAT

Set Abc=hello world, this string come from bat
Echo Original string is:%abc%
Echo Intercept first 5 characters:%abc:~0,5%
Echo Intercept last 5 characters:%abc:~-5%
Echo intercepts the first to the 6th character:%abc:~1,-5%
Echo starts with a 4th character intercept of 5 characters:%abc:~3,5%
Echo starts from the 14th character to intercept 5 characters:%abc:~-14,5%

Echo Current time is:%time% namely%time:~0,2% point%time:~3,2% minute%time:~6,2% seconds%time:~9,2% second

Pause

The output is:

The original string is: Hello World, the This string come from bat
The first 5 characters of interception: Hello
Interception of the last 5 characters: M bat
Intercepts the first to the 6th character: Ello World, this string come fro
5 characters from the beginning of the 4th character: Lo wo
5 characters starting from the last 14th character: come
The current time is: 18:37:58.75 that is 18 points 37 minutes 58 seconds 75 cl seconds


2 , replace string
A replacement string that replaces a specific character or string in a string with the given string. Example:

@echo offsetlocal Enabledelayedexpansion::coder by DSW powerd by Ibatset aa= great China! I'm proud of you. Echo Replace before:%aa%echo replace:%aa: China = People's Republic of%echo aa=%aa%
Echo%aa%set AA=%AA: China = People's Republic of%echo Aa=%aa%pause

The output is:

Before the replacement: Great China! I'm proud of you.
After replacement: Great People's Republic of China! I'm proud of you.
Aa= The Great China! I'm proud of you.

The Great China! I'm proud of you.
Aa= Great People's Republic of China! I'm proud of you.

For the above example there is a little explanation, compared to two echo AA =%aa% can be found, its equivalent to: aa=%aa% Echo aa=%aa%


3. String merging
In fact, merging strings is just a matter of putting two strings together. To illustrate:

@echo offsetlocal Enabledelayedexpansion::coder by DSW powerd by Ibatset aa= great China! Set Bb= I'm proud of you echo%aa%%bb%echo aa=%aa%echo bb=%bb%set "aa=%aa%%bb%" Echo aa=%aa%pause

The output is:
The Great China! I'm proud of you.
Aa= The Great China!
Bb=, I'm proud of you.
Aa= The Great China! I'm proud of you.

Similarly, if you want to change the contents of the variable AA, you need to assign the merge result "%aa%%bb%" to the variable AA.

4, extended string
"Enrich" the term is derived from Microsoft's own translation, meaning that the file path represents a string for special processing, the specific functions are listed as follows:
~i-  Remove any quotation marks ("), expand  %i
%~fi-  expands  %I  to a fully qualified pathname
%~di-  only expands  %I  to a drive letter
%~pi-  will only Nbsp;%i   Expand to a path
%~ni-  only expands  %I  to a file name
%~xi-  will only extend  %I  to a filename extension
%~si-  The expanded path contains only the short name
%~ai-  Add  %I  to file properties
%~ti-  add  %I  to file date/time
%~zi-  Will & Nbsp;%i   Expansion to file size
%~ $PATH: I-  Find the directory of the PATH environment variable and  %I  expand
to the first fully qualified name found. If the environment variable name is not defined, or if the file is not found, the key combination expands to an empty string to combine modifiers to get multiple results:
%~dpi-  only expands  %I  to a drive letter and path
%~nxi-  only  %I  expand to a file name and extension
%~FSI-  only expands  %I  to a full pathname with a short name
%~dp$path:i-  Find the directory that is listed in the PATH environment variable. and expands  %I  to the first drive letter and path found. &NBSP
%~ftzai-  extends  %I  to  dir of similar output lines

The above content is referenced in the for/? Help information. The I represents the variable i, but it should be noted that not all variables can be expanded, there are two conditions: 1, the string represents a file path, 2, the variable to use%x to represent, X is a-Z a-to-0-9 Total 62 characters of any one. To illustrate:

@echo offsetlocal Enabledelayedexpansion::coder by DSW powerd by Ibatecho This batch is running: Echo full path:%0echo Remove quotation marks:%~0echo partition:% ~d0echo path:%~p0echo file name:%~n0echo Extension:%~x0echo file property:%~a0echo Modified:%~t0echo file size:%~z0echo%~nxtza0
Pause

The output is:

This batch is running:
Full path: C:\Users\dsw\Desktop\test. BAT
Remove quotation marks: C:\Users\dsw\Desktop\test. BAT
Partition: C:
Path: \users\dsw\desktop\
File name: Test
Extension:. BAT
File properties:--a------
Modification Time: 2016/04/25 18:52
File Size: 295

--A------2016/04/25 19:11 test. BAT

Where%0 is the parameter inside the batch, representing the full path of the currently running batch. Similarly, there are%1-%9, which represent the 第1-9个 parameters that are passed. Examples are as follows:

@echo offsetlocal Enabledelayedexpansion::coder by DSW powerd by Ibatset aa=c:\windows\ppp\a.dswcall:deal aaa%aaa% "C C" DDD Eeeecho call func exitpause>nulexit:dealecho%%0=%0echo%%1=%1echo%%2=%2echo%%3=%3echo%%4=%4echo%%5=%5pause

The output is:

%0=:d EAL
%1=aaa
%2=c:\windows\ppp\a.dsw
%3= "C C"
%4=ddd
%5=eee

Where the variable AA is not extensible before, through the call command and pass AA as a parameter to the child function: Deal, the AA variable is converted to the variable% 1, that is, conform to the%x format, so that the string can be expanded.
As for the X-A-A-Z in%x form, you can review the for statement, the variables inside the for statement are represented by%x, and thus can be expanded directly.

string manipulation in combination with set usage

@echo offsetlocal Enabledelayedexpansion::coder by DSW powerd by Ibatset a=abcd efgh Ijklecho The value before replacement is: "%a%" set Var=%a: =%ec The value after Ho is replaced by: "%var%" set B=bbs.verybat.cnset Var=%b:~1,2%echo intercepts the string:%var%set Var=%a:~3%echo intercepts the string:%var%set var=%a:~0, -3%echo intercepts the string:%var%pause output is: The value before the replacement is: "ABCD efgh IJKL" replaced by the value: "ABCDEFGHIJKL" intercept the string is: BS intercept the string is: D efgh ijkl intercept the string is: ABCD EFGH I

-denotes the meaning of the right side of the string, 3 means 0 from the right of the string and 3 positions to the left

Windows Batch learning (character swapping)---04

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.