0

すべての出現ではなく、パス文字列内の特定の文字列の最初の出現を置き換えたい。

例:

set strpath=D:\Temp\Test\projects\Test
set strreplace=Test
set strpath=%strpath:%strreplace%=MyProjects%        
echo strpath = %strpath%

ただし、 strpath = D:\Temp\MyProjects\projects\MyProjects のようにすべての出現を置き換えます

次のような出力が必要です

 D:\Temp\MyProjects\projects\Test

前もって感謝します。

4

1 に答える 1

0

私はあなたの問題のための関数を作りました:

@Echo OFF
SETLOCAL Enabledelayedexpansion

:: By Elektro H@cker

set "strpath=D:\Temp\Test\projects\Test"

REM Call :REPLACE_FIRST "Variable" "word" "to replace word"
Call :REPLACE_FIRST "%strpath%" "Test" "Anything"
Echo %replaced_str%
Pause&Exit


:REPLACE_FIRST
Set "str=%~1"
Set "word_before=%~2"
Set "word_after=%~3"
Set "splited_str=%str:\= \ %"

FOR %%# in (%splited_str%) do (
    IF "%%#" EQU "!word_before!" (
        Set "replaced_str=!replaced_str!!word_after!"
        Set "word_before="
    ) ELSE (
        Set "replaced_str=!replaced_str!%%#"
    )
)

GOTO:EOF

出力:

D:\Temp\Anything\projects\Test
于 2012-11-27T09:26:08.493 に答える