Detailed parameter string processing path and batch Batch

1, taken string

  String can be said interception string processing functions most frequently used functions of a child, capable of implementing one or more character strings taken of a particular location. Illustrate its basic functions:

  1.   @ echo off
  2.    set ifo=abcdefghijklmnopqrstuvwxyz0123456789
  3.    echo the original string (second line of each character of the serial number):
  4.    echo %ifo%
  5.    echo 123456789012345678901234567890123456
  6.    echo interception of the first 5 characters:
  7.    echo %ifo:~0,5%
  8.    echo interception last five characters:
  9.    echo %ifo:~-5%
  10.    echo interception of the first to the penultimate six characters:
  11.    echo %ifo:~0,-5%
  12.    echo from the beginning of four characters, five characters taken:
  13.    echo %ifo:~3,5%
  14.    echo from the start of the penultimate 14 characters, the interception of five characters:
  15.    echo %ifo:~-14,5%
  16.   pause


 Of course, the above example is only the basic functions of string manipulation show came out, still do not see what use specific string processing. The following example is time to process.

  1.   @echo off
  2.   echo current time is:% time% Time i.e.,%: ~ 0 , 2 % Time point%: ~ . 3 , 2 % Time min%: ~ . 6 , 2 % sec Time%: ~ . 9 , 2 % centiseconds
  3.   pause

2, replacement string
  replacement string is replaced with the given string is about a specific character string or strings. Illustrates the function:

  1.   @ echo off
  2.    the SET AA = great Chinese! I'm proud of you!
  3.    echo before replacement:% aa%
  4.    echo after replacing:% aa: China = People's Republic of China%
  5.    echo aa = %aa%
  6.    the SET "AA = AA%:% China = People's Republic of China"
  7.    echo aa = %aa%
  8.   pause

For the above example a little explanation, comparing two echo aa =% aa% can be found, if you want to modify the contents of aa variable, then it is necessary to modify the results "% aa: China = People's Republic of China%" assigned to the variable aa. Taken string above also have the same characteristics.

  1. echo % var : 0 = kkk%; 0 replaced kkk
  2. echo % var : 10 = kkk%; 10 replaced kkk
  3. the echo %, var : of 20 = kkk%,
  4. echo % var : * 20 = kkk%; 20 before comprising 20 strings are replaced kkk

 

 

3, strings into

  In fact, the merger is to string two strings together on it. for example:

  1.   @ echo off
  2.    the SET AA = great Chinese!
  3.    the SET bb = I'm proud of you!
  4.    echo out the % 'aa %% the bb%
  5.    echo aa=%aa%
  6.    echo echo Bb =% Bb%
  7.    set "aa=%aa%%bb%"
  8.    echo aa=%aa%
  9.   pause


Similarly, if you want to change the variable aa content, then you need to merge result "% aa %% bb%" assigned to the variable aa.

4, the expansion of the string

  "Expansion" the term comes from Microsoft's own translation, meaning that the file path for the string representation of special handling, specific functions are listed below:
  =================== ======================
  ~ the I - remove any quotation marks ( "), expanded% I
  % ~ fI - expands% I to a fully qualified path name
  % ~ dI - only expands% I to a drive letter
  % ~ pI - only expands% I to a path
  % ~ nI - only expands% I to a file name
  % ~ xI - only expands% I to a file extension
  % ~ sI - expanded path contains short names
  % ~ aI - expands% I to file attributes
  % ~ tI - expands% I to date / time of the file
  % ~ zI - expands% I to a file the size
  % ~ $ pATH: I - to find the directories listed in the pATH environment variable and expands% I
  to a fully qualified name of the first found if the environment variable name.
  is not defined, or the file is not found, this key combination will expand to
  an empty string
  can be combined to obtain multiple modifiers results:
  % ~ dpI - only expands% I to a drive letter and path
  % ~ nxI -% I expanded to only a file name and expanded Name
  % ~ fsI - only expands% I to a full path name with short names only
  % ~ dp $ PATH: i - lookup column in the path environment variable directory, and expands% I
  The first drive and the number of paths found.
  % ~ ftzaI - expands% I output line similar to the DIR
  ==================================== =====
  above referenced in for /? help. I wherein I represents the variable, but should be noted that not all the variables can be expanded, there are two conditions: 1, the string represents a file path; 2, use a variable to represent% x, x preferably az AZ 0-9 in any one of 62 characters in the. for example:

  1. @ echo off
  2. echo running this batch:
  3. echo full path:% 0
  4. echo removing quotes:% 0
  5. echo District:% ~ d0
  6. echo which path:% ~ p0
  7. echo filename:% ~ n0
  8. echo extensions:% ~ x0
  9. echo file attributes:% ~ a0
  10. echo Modified:% ~ t0
  11. echo File size:% ~ z0
  12. pause
       0% of which is really the first batch parameters inside, representing the full path of the currently running batch.
       There are similar 9% 1%,
  Representing the passed argument 1-9,
  You can replace the above 0%
       Examples are as follows: 
  1.   @echo off
  2.   set aa=C:\Windows\PPP\a.btx
  3.   call :deal aaa %aa% "c c" ddd eee
  4.   pause> null
  5.    exit
  6. :deal
  7.   echo %% 0 = %0
  8.   echo %% 1 = %1
  9.   echo %% 2 = %2
  10.   echo %% 3 = %3
  11.   echo %% 4 = %4
  12.   echo %% 5 = %5


Wherein the variables are not aa before expansion, and the call command by aa passed as a parameter to the subroutine: deal, the variable aa converted into variable% 1,% x format that is consistent, so that the string can be expanded.
  As to the form of taking az AZ% x in x, you can review the for statement, for which the statement is to use variable% x expressed, which can be expanded directly. 

C language and do some comparison: 

First talk about, different batch and high-level language, no string handling functions, such as strcat like, but you can use environment variables to achieve these functions do.
Control herein string handling functions in C language, to explain the implementation in a batch.

First talk about the string stored in the C language, the use of an array of characters or character pointer to store the string, BAT is not the stuff, so you want to store relies on environment variables.
Set the environment variable statement set statement, not described in detail herein, reference set /? Statement.

1, C phrase strcpy function, a character string copied to another pointer or a character array, covering the original string.

Calling methods C language: strcpy (string target, the source string)

Implementation in the batch:

set the target string source string% =%

Example:

  1. @ echo off
  2. :: echoing off the screen (optional)
  3. set str1=This is old string
  4. :: string str1 stored set, note that no double quotes, this is different from the C language!
  5. set str2=This is new string
  6. :: string stored in the setting str2
  7. echo perform copying the previous string:
  8. echo str1=%str1%
  9. echo str2=%str2%
  10. :: first output once the original string
  11. set str1=%str2%
  12. :: string copy
  13. echo performed after the copy of the string:
  14. echo str1=%str1%
  15. echo str2=%str2%
  16. :: string output after executing the copy of the string
  17. echo output, press any key to exit PAUSE &&> NUL && Exit
  18. :: output, when the user presses any key, the end of the batch.


 

2, C phrase strcat function, is connected to the end of a string or a pointer to another character array of characters.

Calling methods C language: strcat (string target, the source string)

Implementation in the batch:

set certain target string string =%%%% Source String

Example: 

  1. @ echo off
  2. set str1=This is string1
  3. set str2=This is string2
  4. And provided str1 :: string stored str2
  5. echo connection string before:
  6. echo str1=%str1%
  7. echo str2=%str2%
  8. :: first output once the original string
  9. set str1=%str1%%str2%
  10. :: string concatenation
  11. echo after the connection string:
  12. echo str1=%str1%
  13. echo str2=%str2%
  14. After two output string :: string concatenation performed
  15. echo output, press any key to exit PAUSE &&> NUL && Exit 

3, string interception, C in the absence of such a function, but can be achieved through the statement, no longer introduced, but to say the batch.

set the target string source string =%: - start value, intercept length%

Note that the start value from zero!
Intercept length is optional, if taken commas and length, will have been taken from a start to the end of the string.

Example:

  1. @ echo off
  2. set str1=This is string1
  3. :: string stored in the setting str1
  4. set str2=%str1:~8,6%
  5. set str3=%str1:~0,4%
  6. set str4=%str1:~5%
  7. :: string interception
  8. echo the original string:
  9. echo str1=%str1%
  10. echo interception string obtained:
  11. echo str2=%str2%
  12. echo str3=%str3%
  13. echo str4=%str4%
  14. :: output execution results
  15. echo output, press any key to exit PAUSE &&> NUL && Exit


 

4, C language is a function strlen to obtain the length of the string.

Calling methods C language: strlen (string)

A method implemented in a batch using goto labels and form a circular structure, a truncated character string continuously, and a variable number of records with truncated until the string becomes empty string.

Example:

  1. @echo off
  2. set str1=This is a test string
  3. set str2=Hello World
  4. :: Set two strings
  5. set str=%str1%
  6. :: copy str1 to str
  7. :next1
  8. :: label for goto jump
  9. :: Note the difference with the comment statement, comments, beginning with two colons, compared with a label colon
  10. if not "%str%"== "" (
  11. :: judge str is not empty string, the statement if it is not to run the following
  12. set /a num+= 1
  13. :: arithmetic operation, so num value is incremented 1 , corresponds num ++ or ++ num Statement
  14. set "str=%str:~1%"
  15. :: string interception, assigned to itself
  16. goto next1
  17. :: Jump to next1 label
  18. Utilized herein :: goto and labels, constituting the cyclic structure
  19. )
  20. :: When the above loop structure is finished, it will execute the following statement
  21. echo str1=%str1%
  22. length of echo str1:% NUM %
  23. :: output
  24. set num = 0
  25. :: mind and with the environment variable num set to 0 to start the next operation.
  26. Viewed size =% str2%
  27. :: Copy to str2 str
  28. :next2
  29. :: Defining a new label
  30. :: careful not to with the existing label of the same name, otherwise it will go wrong!
  31. if not "%str%"== "" (
  32. set /a num+= 1
  33. set "str=%str:~1%"
  34. goto next2
  35. )
  36. :: and on a similar cycle, is no longer introduced
  37. echo str2=%str2%
  38. length of echo str2:% NUM %
  39. :: output
  40. echo output, press any key to exit PAUSE &&> NUL && Exit 

5, C language function in strchr to find a character in a string for the first time the location and return location is found, return the value 0 can not be found.

Batch ideas: constantly truncated strings and string comparison of the first character, and character requirements after taking truncated, if the same on the use of the goto statement out of the loop, the output result, if not the same character, to perform Finally output value of zero.

Example: 

  1. @echo off
  2. Setlocal ENABLEDELAYEDEXPANSION
  3. :: command extensions are enabled to participate in setlocal /? Command
  4. set str1=This is a test string
  5. set ch1=t
  6. :: Note that this is case-sensitive!
  7. set str=%str1%
  8. :: string copy, for truncation, without affecting the source string
  9. : next
  10. if not "%str%"== "" (
  11. set /a num+= 1
  12. if "!str:~0,1!"== "%ch1%" goto last
  13. :: compare the first character is a character requirement, if it is out of the loop
  14. set "str=%str:~1%"
  15. goto next
  16. )
  17. set /a num= 0
  18. :: when no character is found, the num zero
  19. : last
  20. echo the characters '% ch1%' in the string "% str1%" first appeared in the location% NUM %
  21. echo output, press any key to exit PAUSE &&> NUL && Exit

Question 1:

(1) How to achieve the C language function strstr function?
Tip: The function strstr function is to find the string 2 in string 1, return to the first occurrence of the string 2 is found, otherwise it returns a value of 0. And strchr difference is that the second argument is a string, not a character.


 

 

Guess you like

Origin www.cnblogs.com/hjbf/p/11357250.html