String manipulation tips in the bat file

Source: Internet
Author: User
Tags trim

Write the bat file today, you need to connect two strings, but there are errors. The reason is this

Set vscomntools= "%vs140comntools%"

If not DEFINED "%vcinstalldir%" (
Call "%vscomntools%vsvars32.bat"
)

The path that is actually called after call is "C:\Program Files (x86) \microsoft Visual Studio 11.0\common7\tools\" vsvars32.b
At

So I need to get rid of the string header with a double quote and the middle of the double quotation mark.

On the Internet to find similar problems, refer to the link here is a good solution, is the use of DOS string manipulation techniques, links are as follows:

Concatinating string in a batch file

Finally my change is this, run correctly:

Set vscomntools= "%vs140comntools%"

If not DEFINED "%vcinstalldir%" (
Set Filepath=%vscomntools:~0,-1%vsvars32.bat "
Call%filepath%
)



By the way, the bat file string manipulation techniques are also affixed to, very useful, the English text is as follows (string manipulation technique original link):

dos-string Manipulation Basic string manipulation in batch like you is used to from the other programming languages.


Align Right Align text to the right i.e. improve readability of number columns.
Left String Extract characters from the beginning of a string.
Map and Lookup Use the Key-value pair list to lookup and translate values.
Mid String Extract a Substring by Position.
Remove Remove a substring using string substitution.
Remove both Ends Remove the first and the last character of a string.
Remove Spaces Remove all spaces in a string via substitution.
Replace Replace a substring using string substitution.
Right String Extract characters from the end of a string.
Split String Split a String, Extract substrings by delimiters.
String concatenation ADD one string to another string.
Trim Left Trim spaces from the beginning of a string via "for" command.
Trim Quotes Remove surrounding quotes via for command.
Trim Right Trim spaces from the end of a string via "for" command.
Trim Right Trim spaces from the end of a string via substitution.
TOP
2008-01-01 Align right-align Text to the right i.e. to improve readability of number columns
Description: Add leading spaces to a string to make sure the output lines up. i.e. for variables no longer than 8 characters add 8 spaces on the front and then show only the last 8 characters of the V Ariable.
Script:
1.
2.
3.
4.
5.
6.
Set x=3000
Set y=2
Set x=%x%
Set y=%y%
Echo. x=%x:~-8%
Echo. y=%y:~-8%
Script Output:
Script Output
x=
y=       2
TOP
2008-01-01 Left string-extract characters from the beginning of a String
Description: Similar to the , left  function in VB a Batch script can return a specified number of characters from the left side of a string by specifying a substring for an Expansion given a position of 0 and a length using: ~ While expanding a variable content. The example shows how to return the first 4 characters of a string.
Script:
1.
2.
3.
4.
set str=politic
echo.%str%
set str=%str:~0,4%
echo.%str%
Script Output:
  script Output
 politic poli 
TOP
2008-01-01 Map and Lookup-use key-value pair list to Lookup and translate values
Description: This example shows an approach to map a name of a month into it's corresponding the number of the digit number. The Key-value pairs is listed in the map variable separated by semicolon. Key and value itself is separated by one dash character. Same can used to tranlate a day-of-the-week short string into a day-of-the-week long string by changing the map content Only.
Script:
1.
2.
3.
4.
5.
6.
7.
8.
9.
.
one.
.
.
.
.
.
.
.
rem ---- Example 1: Translate name of month into two  digit number ----
Set v=mai

set map=jan-01; Feb-02; Mar-03; Apr-04; Mai-05; Jun-06; Jul-07; Aug-08; sep-09;oct-10; nov-11;dec-12
call set v=%%map:*%v%-=%%
set v=%v:;=&rem.%

echo.%v%


rem ---- Example 2: Translate abbreviation into full string ----
Set v=sun

set map=mon-monday;tue-tuesday;wed-wednesday;thu-thursday;fri-friday; Sat-saturday;sun-sunday
call set v=%%map:*%v%-=%%
set v=%v:;=&rem.%

   echo.%v%
Script Output:
Script Output
  Sunday
TOP
2008-01-01 Mid string-extract a Substring by Position
Description: Similar to the Mid function in VB a batch script can return a specified number of characters from any position in Side a string by specifying a substring for the expansion given a position and length using: ~ While expanding a variable C Ontent. The example here shows what to extract the parts of a date.
Script:
1.
2.
3.
4.
5.
Echo. Date:%date%
Echo. Weekday:%date:~0,3%
Echo. Month:%date:~4,2%
Echo. Day:%date:~7,2%
Echo. Year:%date:~10,4%
Script Output:
Script Output
Date   : Sat 03/11/2006
weekday:sat
Month  : from
:   2006
TOP
2008-01-01 remove-remove a substring using string substitution
Description: The string substitution feature can also is used to remove a substring from another string. The example shown here removes all occurrences of "the" from the string variable str.
Script:
1.
2.
3.
4.
Set Str=the Cat in the Hat
echo.%str%
Set Str=%str:the =%
echo.%str%
Script Output:
Script Output
The Cat in the Hat
cat in hat
TOP
2008-01-01 Remove both Ends-remove the first and the last character of a string
Description: Using : ~1,-1 within a variable expansion would remove the first and last character of the string.
Script:
1.
2.
3.
4.
Set Str=politic
echo.%str%
Set str=%str:~1,-1%
echo.%str%
Script Output:
Script Output
Politic
Oliti
TOP
2008-01-01 Remove Spaces-remove All Spaces in a string via substitution
Description: This script snippet can is used to remove any spaces from a string.
Script:
1.
2.
3.
4.
Set str= word &rem
echo. " %str% "
Set STR=%STR: =%
echo. " %str% "
Script Output:
Script Output
      word "
word"
TOP
2008-01-01 replace-replace a substring using string substitution
Description: To replace a substring with another string use the string substitution feature. The example shown here replaces all occurrences "teh" misspellings with "the" in the string variable str.
Script:
1.
2.
3.
4.
Set Str=teh Cat in teh hat
echo.%str%
Set str=%str:teh=the%
echo.%str%
Script Output:
Script Output
teh cat in teh hat the cat in the
hat

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.