Board logo

标题: 字符串分隔后如何循环处理? [打印本页]

作者: prcgolf     时间: 2010-9-10 16:14    标题: 字符串分隔后如何循环处理?

set os="abc\cu\jiu\shijie" rem 无法确定os中有多少个以\为分隔符的字符串,本例中有4个
for /f "tokens=1,2,3* delims=\" %%i in ("%os%") do (
ABC.EXE %%i
ABC.EXE %%j
ABC.EXE %%k
ABC.EXE %%l
)
这样可以达到要求,但是如果分隔后有5个字符串改怎么办呢,有8个呢?关键是不知道具体有多少个?
请诸位大侠拔刀相助,谢谢
作者: DXSX     时间: 2010-9-10 17:06
逐个显示 %1 中被 \ 分开 的单元
如 : l afds\24234\ds4rsar3\ds
将显示为:
afds
24234
ds4rsar3
ds

l.cmd内容
@echo off   
if %1*==* goto end
set l=%1\
set /a n=0
set m=
:top
call set m=%%l:~%n%,1%%
if not defined m goto end
if not "%m%"=="\" (set l0=%l0%%m%) else (if defined l0 (echo %l0%) &set l0=)
set /a n=%n%+1
goto top
:end
set m=
set n=

[ Last edited by DXSX on 2010-9-13 at 08:22 ]
作者: prcgolf     时间: 2010-9-10 17:57    标题: 非常感谢,再麻烦一下

非常感谢,再麻烦一下
能否按照我写的要求,
循环执行ABC.EXE abc
ABC.EXE cu等
你的我实在看不懂,请见谅我的无知,谢谢
作者: slore     时间: 2010-9-10 18:32
set 替换  \ 为 空格
直接写个call ABC %XX%
利用shift就over了。
CODE:  [Copy to clipboard]
@echo off
set os=abc\test\nec\doc\slore\0910
call :ABC %os:\= %
pause
goto :EOF


:ABC
:InnerLoop
if "%1"=="" (goto :EOF)
echo %1
shift
goto :InnerLoop

作者: DXSX     时间: 2010-9-10 21:20


  Quote:
Originally posted by prcgolf at 2010-9-10 17:57:
非常感谢,再麻烦一下
能否按照我写的要求,
循环执行ABC.EXE abc
ABC.EXE cu等
你的我实在看不懂,请见谅我的无知,谢谢

把 2楼中 红色部分 改为 abc.exe %l0%

[ Last edited by DXSX on 2010-9-10 at 21:26 ]
作者: prcgolf     时间: 2010-9-12 21:49    标题: 好像还是不行

@echo off   
if %1*==* goto end
set l="abc\cu\jiu\shijie\"
set /a n=0
set m=
:top
call set m=%%l:~%n%,1%%
if not defined m goto end
if not "%m%"=="\" (set l0=%l0%%m%) else (if defined l0 (echo %l0%) &set l0=)
set /a n=%n%+1
goto top
:end
set m=
set n=
执行后无任何显示!!
作者: prcgolf     时间: 2010-9-12 21:50    标题: 这个也不行

@echo off
set os=abc\test\nec\doc\slore\0910
call :echo %os:\= %
pause
goto :EOF


:ABC
:InnerLoop
if "%1"=="" (goto :EOF)
echo %1
shift
goto :InnerLoop
作者: slore     时间: 2010-9-12 23:29
不知道你的电脑那么高级还是怎么回事。。。

复制的内容还会不一样?7楼的代码不是我的代码。。。
和4楼我的代码不一样。
作者: DXSX     时间: 2010-9-13 08:22


  Quote:
Originally posted by prcgolf at 2010-9-12 21:49:
@echo off   
if %1*==* goto end
set l="abc\cu\jiu\shijie\"
set /a n=0
set m=
:top
call set m=%%l:~%n%,1%%
if not defined m goto end
if not "%m%"=="\" (set l0=% ...

把 2楼 蓝色部分看明白就知道为什么了。

给你一个更精简的:从后先前逐个显示字符串中 \ 段
红色部分替换成你的字符串,注意不要有引号 比如:afds\fddsaf\fsadf\e432\435
蓝色部分替换成你的命令 比如:abc.exe %l0%
要想知道原理 cmd 下 用"set /? " 自己看就明白了。
------------------------
@echo off
set "l=\%1"
set "l0="
:top
if %l%*==* goto end
if not %l:~-1,1%*==\* (set l0=%l:~-1,1%%l0%
  ) else (if defined l0 (echo %l0%) & set "l0=")
set l=%l:~0,-1%
goto top
:end
set "l0=" & set "l="

另一个脚本------------ 从前往后 逐个显示 \  分割段
@echo off
set "l=%1\"
set "l0="
:top
if %l%*==* goto end
if not %l:~0,1%*==\* (set "l0=%l0%%l:~0,1%"
  ) else (if defined l0 (echo %l0%) & set "l0=")
set l=%l:~1%
goto top
:end
set "l0=" & set "l="

[ Last edited by DXSX on 2010-9-13 at 09:27 ]
作者: prcgolf     时间: 2010-9-13 11:32    标题: 多谢诸位大侠帮助,问题已解决

多谢诸位大侠帮助,问题已解决。非常感谢!!