Find and replace a string in multiple files using

2019-06-22 07:22发布

I have more 3000 files in a folder. I want to find and replace text with another one. How can I do that? I'm a newbie in batch script. I can replace it in 1 file but I don't know how to replace in multiple files.

FOR /F %%L IN (lala.txt) DO (
    SET "line=%%L"
    SETLOCAL ENABLEDELAYEDEXPANSION
    set "x=!line:E:\Test=E:\Test\Temp!"
    echo f | xcopy /E !line! !x! 
    ENDLOCAL
)

How can I edit my code to replace the string in all files? Waiting for your help. Thanks

2条回答
叼着烟拽天下
2楼-- · 2019-06-22 07:57

You could use a second loop for the files.

for %%f in (*.txt) do (
    FOR /F %%L IN (%%f) DO (
      SET "line=%%L"
      SETLOCAL ENABLEDELAYEDEXPANSION 
      set "x=!line:E:\Test=E:\Test\Temp!" 
      echo f | xcopy /E !line! !x! 
      ENDLOCAL
  )
)

This code shows only how to build the loop for process all text files.
The inner code uses the code of the OP, which will not replace anything, but this wasn't the question.

查看更多
我命由我不由天
3楼-- · 2019-06-22 08:21

Install the Find And Replace Text command line utility and then you can simply enter

fart *.txt E:\Test E:\Test\Temp
查看更多
登录 后发表回答