2

我有一个文本文件(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 安装之外的任何东西。

4

3 回答 3

4

我认为这可能是您正在寻找的东西,尽管我不确定您确定项目的规则是什么。

它使用 FOR~pnx修饰符来解析路径的一部分。HELP FOR从命令行使用以获取更多信息。它用于\..\..访问祖父“目录”,并\预先设置为使“路径”成为绝对路径。

结果将/and//转换为\,因此变量搜索和替换用于恢复正确的斜杠分隔符,并使用子字符串操作去除前导斜杠。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-07T22:33:42.553 回答
1

好的,我最短且最容易理解但评论最少的解决方案:

@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
于 2012-09-10T11:35:32.660 回答
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
于 2012-09-09T01:37:09.137 回答