2

次のようなURLのリストを含むテキストファイル(myurls.txt)があります。

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の長さを処理できるように、より柔軟にする必要があります。デフォルトのWindowsXP/ 7インストールで実行できる限り、たとえばWindows Script Host/VBscriptを使用するなどの他のソリューションでも問題ありません。言い換えれば、私はWindowsにawk、grep、sed、pythonなどを使用して仕事を終わらせることができることを知っていますが、ユーザーが標準のWindowsインストール以外のものをインストールする必要はありません。

4

3 に答える 3

4

これはあなたが探しているものかもしれないと思いますが、プロジェクトを特定するためのあなたのルールが何であるかは完全にはわかりません。

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-07T22:33:42.553 に答える
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 に答える
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
于 2012-09-10T11:35:32.660 に答える