SoFunction
Updated on 2025-04-08

String segmentation implementation code in batch processing

Batch String Splitting Example

Use the for command to perform segmentation processing of strings.

Split string

@echo off
::Define a semicolon-delimited string
set str=AAA;BBB;CCC;DDD;EEE;FFF
::strCopy of
set remain=%str%
:loop
for /f "tokens=1* delims=;" %%a in ("%remain%") do (
	::Output the first segment(Token)
	echo %%a
	rem Assign the remaining part to the variableremain,In fact, you can use the delay variable switch here
	set remain=%%b
)
::If there is still left,Continue to split
if defined remain goto :loop
pause

Main explanation for the for statement:

delims=; means to use a semicolon as the delimiter to split the remain string.
tokens=1*, tokens represents the way of segmentation, tokens=1* represents the first separator; the previous one is part, and the remaining one (* represents) is part. These two parts can be used in the circulation body to represent the first part and %%b represents the second part.

Batch processing traversal path environment variables

We know that path environment variables are also divided by semicolons and are in batch processing, so we can also use the above code to traverse path environment variables.

@echo off
setlocal enabledelayedexpansion 
::Define a semicolon-delimited string
set str=%path%
::strCopy of
set remain=%str%
:loop
for /f "tokens=1* delims=;" %%a in ("%remain%") do (
	::Output the first segment(Token)
	echo %%a
	rem Assign the remaining part to the variableremain,In fact, you can use the delay variable switch here
	set remain=%%b
)
::If there is still left,Continue to split
if defined remain goto :loop
pause

Running results:

D:\dev\workspace\MarkdownTools
......
C:\windows\system32
D:\dev\java\jdk1.8.0_91\bin
F:\Program Files\nodejs\node_global
F:\Program Files\Git\bin
D:\dev\apache-maven-3.5.4\bin
......
Please press any key to continue. . .

Batch Determines whether there is a directory in the path environment variable

For example, find out whether the D:\dev\workspace\MarkdownTools directory exists in the system path environment variable:

@echo off
setlocal enabledelayedexpansion 
::Define a semicolon-delimited string
::set str=AAA;BBB;CCC;DDD;EEE;FFF
set str=%path%
::strCopy of
set remain=%str%
set toFind=D:\dev\workspace\MarkdownTools
set isFind=false
:loop
for /f "tokens=1* delims=;" %%a in ("%remain%") do (
	if "%toFind%"=="%%a" (
		::Setting the mark,For subsequent use
		set isFind=true
		::If you find it, don't look for it
		goto :finded
	)
	rem Assign the remaining part to the variableremain,In fact, you can use the delay variable switch here
	set remain=%%b
)
::If there is still left,Continue to split
if defined remain goto :loop
:finded
echo %isFind%
pause

Running results:

true

Please press any key to continue. . .

References

Recently, there is a small requirement to move the functions of shell scripts to Windows, but I found that there is an array concept in shell, but it does not exist in windows. At the same time, there are many ways to deal with string segmentation in shell, but it seems a bit useless in bat. After some searching, I finally got a solution (: /questions/1707058/how-to-split-a-string-in-a-windows-batch-file):

Solution: Through the for loop processing, there are two ways to process, one is ordinary for and the other is for file processing:

Plan 1:

@echo off & setlocal
rem Pay attention to thesdefinition,The value is not enclosed in double quotes
rem also works for comma-separated lists, . ABC,DEF,GHI,JKL
set s=AAA BBB CCC DDD EEE FFF
for %%a in (%s%) do echo %%a

Plan 2: is the best for (most) arbitrary delimiter characters.

@echo off & setlocal
set s=AAA BBB CCC DDD EEE FFF
set t=%s%
:loop
for /f "tokens=1*" %%a in ("%t%") do (
 echo %%a
 rem Assign the remaining part tot,In fact, you can use the delay variable switch here
 set t=%%b
 )
if defined t goto :loop

A guy gave a more complete one (using the delay variable):

@echo off
setlocal ENABLEDELAYEDEXPANSION

REM Set a string with an arbitrary number of substrings separated by semi colons
set teststring=The;rain;in;spain

REM Do something with each substring
:stringLOOP
 REM Stop when the string is empty
 if "!teststring!" EQU "" goto END

 for /f "delims=;" %%a in ("!teststring!") do set substring=%%a

  REM Do something with the substring - 
  REM we just echo it for the purposes of demo
  echo !substring!

REM Now strip off the leading substring
:striploop
 set stripchar=!teststring:~0,1!
 set teststring=!teststring:~1!

 if "!teststring!" EQU "" goto stringloop

 if "!stripchar!" NEQ ";" goto striploop

 goto stringloop
)

:END
endlocal

And there are:

set input=AAA BBB CCC DDD EEE FFF
set nth=4
for /F "tokens=%nth% delims= " %%a in ("%input%") do set nthstring=%%a
echo %nthstring%

In fact, there may be more built-in functions in Powershell that can be used:

PS C:\> "AAA BBB CCC DDD EEE FFF".Split()

Someone also proposed to use vbscript instead of bat:

Set objFS = CreateObject("")
Set objArgs = 
str1 = objArgs(0)
s=Split(str1," ")
For i=LBound(s) To UBound(s)
  s(i)
  s(9) ' get the 10th element
Next
usage:
c:\test> cscript /nologo  "AAA BBB CCC"

Finally, there is a small difficulty in bat: variable delay (from top to bottom, one by one (simple statements, compound statements (for and if statement blocks are only counted as one))) instead of line by line)

Detailed explanation of variable delay call setlocal

The above is the detailed content of the string segmentation implementation code in batch processing. For more information about batch string segmentation, please pay attention to my other related articles!