打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
批处理中字符串分割实现代码

批处理 字符串分割 实例

使用for命令可以对字符串进行分段处理。

分割字符串

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@echo off
::定义一个以分号作为分隔的字符串
set str=AAA;BBB;CCC;DDD;EEE;FFF
::str的副本
set remain=%str%
:loop
for /f "tokens=1* delims=;" %%a in ("%remain%") do (
    ::输出第一个分段(令牌)
    echo %%a
    rem 将截取剩下的部分赋给变量remain,其实这里可以使用延迟变量开关
    set remain=%%b
)
::如果还有剩余,则继续分割
if defined remain goto :loop
pause

主要解释for语句:

delims=;表示以分号作为分隔符,对remain字符串进行分割处理。
tokens=1*,tokens表示分段的方式,tokens=1*表示第一个分隔符;之前的作为一部分,剩下的(*表示)作为一部分。这两部分在循环体总可以用%%a表示第一部分,%%b表示第二部分。

批处理 遍历path环境变量

我们知道path环境变量也是以分号作为分隔符的,批处理中,所以同样可以用上面的代码来遍历path环境变量。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@echo off
setlocal enabledelayedexpansion
::定义一个以分号作为分隔的字符串
set str=%path%
::str的副本
set remain=%str%
:loop
for /f "tokens=1* delims=;" %%a in ("%remain%") do (
    ::输出第一个分段(令牌)
    echo %%a
    rem 将截取剩下的部分赋给变量remain,其实这里可以使用延迟变量开关
    set remain=%%b
)
::如果还有剩余,则继续分割
if defined remain goto :loop
pause

运行结果:

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
......
请按任意键继续. . .

批处理 判断path环境变量中是否有某个目录

例如查找系统path环境变量中是否存在D:\dev\workspace\MarkdownTools这个目录:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
@echo off
setlocal enabledelayedexpansion
::定义一个以分号作为分隔的字符串
::set str=AAA;BBB;CCC;DDD;EEE;FFF
set str=%path%
::str的副本
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" (
        ::设置标记,以便后续使用
        set isFind=true
        ::找到了就不找了
        goto :finded
    )
    rem 将截取剩下的部分赋给变量remain,其实这里可以使用延迟变量开关
    set remain=%%b
)
::如果还有剩余,则继续分割
if defined remain goto :loop
:finded
echo %isFind%
pause

运行结果:

true

请按任意键继续. . .

参考资料

最近有有个小需求需要将shell 脚本的功能挪到windows中,但发现shell中有数组概念,但windows中却没有,同时shell中有很多方式处理字符串分割,但bat中就显得比较鸡肋,经过一番查找,终于有了方案(Stack Overflow:http://stackoverflow.com/questions/1707058/how-to-split-a-string-in-a-windows-batch-file):

方案: 通过for循环处理,而处理的方式又可以分两种,一种是普通for,一种是for的文件处理方式:

方案一:

?
1
2
3
4
5
@echo off & setlocal
rem 注意这里的s定义,其值不是使用双引号引起来的
rem also works for comma-separated lists, e.g. ABC,DEF,GHI,JKL
set s=AAA BBB CCC DDD EEE FFF
for %%a in (%s%) do echo %%a

方案二:is the best for (most) arbitrary delimiter characters.

?
1
2
3
4
5
6
7
8
9
10
@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 将截取剩下的部分赋给t,其实这里可以使用延迟变量开关
 set t=%%b
 )
if defined t goto :loop

有个老兄给了个更完整的(用到了延迟变量):

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
@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

还有这样的:

?
1
2
3
4
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%

其实Powershell里可能有更多的内置函数可以使用:

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

还有人提出用vbscrip代替bat:

?
1
2
3
4
5
6
7
8
9
10
Set objFS = CreateObject("Scripting.FileSystemObject")
Set objArgs = WScript.Arguments
str1 = objArgs(0)
s=Split(str1," ")
For i=LBound(s) To UBound(s)
 WScript.Echo s(i)
 WScript.Echo s(9) ' get the 10th element
Next
usage:
c:\test> cscript /nologo test.vbs "AAA BBB CCC"

最后来一个bat中的小难点: 变量延迟(自上而下,逐条(简单语句、复合语句(for、if 语句块只算一条))执行,而非逐行执行)

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
BAT 命令中如何消除字符串变量前后空格?
Windows BAT批处理命令详解
精通批处理教程--隐客居
DOS里如何判断文件大小及其它?我的一些
批处理命令学习之:set命令详解
DOS批处理中的字符串处理详解(字符串截取)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服