Find and replace a string in multiple files using Batch script

微笑、不失礼 提交于 2020-01-03 12:30:14

问题


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


回答1:


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

fart *.txt E:\Test E:\Test\Temp



回答2:


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.



来源:https://stackoverflow.com/questions/9645848/find-and-replace-a-string-in-multiple-files-using-batch-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!