2012-09-07 152 views
1

我有一个文本文件(myurls.txt),其内容的网址如下列表:从Windows批处理文件解析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 

一些实验后,我有以下的代码,但它仅适用(种),如果网址有相同数量的斜杠:

@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以外,不得不安装任何东西。

回答

3

我认为这可能是您正在寻找的东西,但我并不完全确定您的规则是用于识别项目的。

它使用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 
+0

感谢您的答复!事实上,没有确定项目的规则。事实上,这些URL甚至可能没有名称中的Project。例如,URL可以简单地为:http://linux.koolsolutions.com/svn/LinuxMaterial/tags/REL-1.0。唯一的形式是每个URL都有标题(如Slides_N或Exercise_N)并以/tags/REL-X.Y结尾。 – modest

+0

@modest - 那是一个规则:-)谢天谢地,它也符合我实施的规则。我去掉最后2个路径组件来获得项目,这将对应于“/tags/REL-X.Y”。它看起来像你的问题已经回答。 – dbenham

+0

是的,您的解决方案在任何情况下均可使用。非常感谢! – modest

1
@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 
+0

抱歉这么长时间才能发布,但我非常分心的一个非常奇怪的错误。如果你在':: Extract the Release'上面添加另一个冒号定界的注释,我会得到一个'系统找不到指定的驱动器'错误。只需一对冒号就可以了。但用'REM'代替'::',它的行为很好。我玩了它,并得到不同的错误。这只是** BIZZARE **。我还没有弄清楚发生了什么。 –

+1

** BIZZARE **效果来自括号内的主要/辅助标签行[SO:windows批处理文件,goto命令不起作用](http://stackoverflow.com/a/4006006/463115) – jeb

+0

@jeb - Ach,我应该知道!我记得'::'通过使代码像在括号之外一样行为来破坏我的代码。谢谢你提醒我。^_^ –

1

OK,我的时间最短,最可以理解的,但至少评论的解决方案:

@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 :eof