1

ファイル名を引数として受け取る.cmdファイルを作成し、検索する文字列と置き換える文字列を要求します。

希望のアウトプット

findthis は指定されたファイルから replacewith に置き換える必要がありますが、機能していません

以下は私が試したコードです..

@echo off
setlocal enabledelayedexpansion

if not exist "%1" (echo this file does not exist...)&goto :eof

set /p findthis=find this text string:
set /p replacewith=and replace it with this:


for /f "tokens=*" %%a in (%1) do (

   set write=%%a
   if %%a==%findthis% set write=%replacewith%

   echo !write! 
   echo !write! >>%~n1.replaced%~x1
)

ありがとう

4

1 に答える 1

0

私が修正したコードには、いくつかの単純なバグがあります。文字列にはスペースが含まれる可能性が非常に高いため、文字列を引用符で囲む必要があります。場合によっては、エコーで問題を引き起こすヌル文字列になる可能性があるため、「;」を追加しました。それに対処するためにエコーに:

@echo off
setlocal enabledelayedexpansion

if not exist "%1" (echo this file does not exist...)&goto :eof

set /p findthis=find this text string:
set /p replacewith=and replace it with this:


for /f "tokens=*" %%a in (%1) do (

   set write=%%a
   if "%%a"=="%findthis%" set write=%replacewith%

   echo;!write! 
   echo;!write! >>%~n1.replaced%~x1
)

ただし、これは行全体に一致して置換することしかできず、行内のテキスト文字列には一致しません。行内の部分文字列を置き換えるには、可変文字列置換構文を使用する必要があります (これは、質問で何度か引用されています:ここと 最後にここ.

後者の回答には、元の問題を解決するために必要なすべてが含まれています。

于 2014-12-26T09:58:24.297 に答える