我有一个文本文件(myurls.txt),其内容是URL列表,如下所示:
Slides_1: http://linux.koolsolutions.com/svn/ProjectA/tags/REL-1.0
Exercise_1: http://linux.koolsolutions.com/svn/Linux/tags/REL-1.0
Slides_2: http://linux.koolsolutions.com/svn/oldproject/ProjectB/tags/REL-2.0
Exercise_2: http://linux.koolsolutions.com/svn/ProjectB/tags/REL-1.0
Exercise_3: http://linux.koolsolutions.com/svn/BlueBook/ProjectA/tags/REL-1.0现在,我想在for循环中解析这个文本文件,以便在每次迭代之后(例如,从上面的文件中获取第一个url ),我将以下信息放入不同的变量中:
%i% = REL-1.0
%j% = http://linux.koolsolutions.com/svn/ProjectA
%k% = http://linux.koolsolutions.com/svn/ProjectA/tags/REL-1.0经过一些实验后,我有了以下代码,但它只在URL有相同数量的斜杠的情况下才能工作:
@echo off
set FILE=myurls.txt
FOR /F "tokens=2-9 delims=/ " %%i in (%FILE%) do (
@REM <do something with variables i, j and k.>
)显然,我需要让它更灵活,这样它才能处理任意的url长度。我对其他解决方案很满意,比如使用Windows Script Host/VBscript,只要它能在默认的Windows XP/7安装下运行即可。换句话说,我知道我可以在Windows上使用awk、grep、sed、python等来完成这项工作,但我不希望用户必须安装除了标准windows安装之外的任何东西。
发布于 2012-09-08 06:33:43
我认为这可能是你正在寻找的,尽管我不是绝对确定你的规则是什么来识别项目。
它使用FOR ~pnx修饰符来解析路径的一部分。从命令行使用HELP FOR获取更多信息。它使用\..\..到达父级“目录”,并在前面加上\以使“路径”成为绝对路径。
结果将/和//转换为\,因此使用变量搜索和替换来恢复正确的斜杠分隔符,并使用子字符串操作来去除前导斜杠。有关搜索、替换和子字符串操作的详细信息,请从命令行使用HELP SET。
使用延迟扩展是因为它需要扩展在同一代码块中设置的变量。
@echo off
setlocal enableDelayedExpansion
set "file=myurls.txt"
for /f "tokens=1*" %%A in (%file%) do (
for /f "delims=" %%C in ("\%%B\..\..") do (
set "project=%%~pnxC"
set "project=!project:~1!"
set "project=!project:\=/!"
set "project=!project:http:/=http://!"
echo header = %%A
echo url = %%B
echo project = !project!
echo release = %%~nxB
echo(
)
)以下是示例数据的结果:
header = Slides_1:
url = http://linux.koolsolutions.com/svn/ProjectA/tags/REL-1.0
project = http://linux.koolsolutions.com/svn/ProjectA
release = REL-1.0
header = Exercise_1:
url = http://linux.koolsolutions.com/svn/ProjectA/tags/REL-1.0
project = http://linux.koolsolutions.com/svn/ProjectA
release = REL-1.0
header = Slides_2:
url = http://linux.koolsolutions.com/svn/oldproject/ProjectB/tags/REL-2.0
project = http://linux.koolsolutions.com/svn/oldproject/ProjectB
release = REL-2.0
header = Exercise_2:
url = http://linux.koolsolutions.com/svn/ProjectB/tags/REL-1.0
project = http://linux.koolsolutions.com/svn/ProjectB
release = REL-1.0
header = Exercise_3:
url = http://linux.koolsolutions.com/svn/BlueBook/ProjectA/tags/REL-1.0
project = http://linux.koolsolutions.com/svn/BlueBook/ProjectA
release = REL-1.0发布于 2012-09-09 09:37:09
@echo off
:: First seperate into Label, URI type, and internet path
for /f "tokens=1-3 delims=:" %%x in (URLs.txt) do (
echo.
:: Take the Label
for /f %%a in ("%%x") do set LabelNam=%%a
:: Assemble Release URL
set ReleaseURL=http:%%z
:: Delayed variable expansion is required just for 'z'
setlocal enabledelayedexpansion
:: Take Release URL Path
set z=%%z
:: Extract the Release
for /f "tokens=2" %%b in ("!z:/tags/= !") do set Release=%%b
:: Split the Internet Path at the '/''s and call ':getURL'
call :getURL %%y !z:/= !
:: Output the information
echo Label = !LabelNam!
echo Release = !Release!
echo URL = !URL!
echo Release URL = !ReleaseURL!
:: End variable expansion
endlocal
)
goto :eof
:getURL
:: Get URL type
set URL=%1:/
:: shift all arguments one to the left
shift
:URLloop
:: Assemble URL
set URL=%URL%/%1
shift
:: If we haven't fount 'tags' yet, loop
if "%1" neq "tags" goto :URLloop
goto :eof发布于 2012-09-10 19:35:33
好吧,这是我最简短、最容易理解、但评论最少的解决方案:
@echo off
for /f "tokens=1-3 delims=: " %%x in (URLs.txt) do (
set LabelNam=%%x
set ReleaseURL=%%y:%%z
for /f "tokens=1-31 delims=/" %%a in ("%%y:%%z") do call :getURL %%a %%b %%c %%d %%e %%f %%g %%h %%i %%j %%k %%l %%m %%n %%o %%p %%q %%r %%s %%t %%u %%v
echo.
echo Label = %LabelNam%
echo Release = %Release%
echo URL = %URL%
echo Release URL = %ReleaseURL%
)
goto :eof
:getURL
set URL=%1/
shift
:URLloop
set URL=%URL%/%1
shift
if "%1" neq "tags" goto :URLloop
Set Release=%2
goto :eofhttps://stackoverflow.com/questions/12325725
复制相似问题