run a script to rename several words in a txt file

前端 未结 1 1116
北荒
北荒 2021-01-25 10:50

I need your help. I have a txt file in a directory (folder) and need to run a script to rename several words, eg.

where LX4XAB to LX4xab and where is XS3X44 to Xs3x44

相关标签:
1条回答
  • 2021-01-25 11:43

    Here you go:

    @echo off
    setlocal enabledelayedexpansion
    (for /f "tokens=*" %%f in (input1.txt) do (
            set "line=%%f"
            set "line=!line:LX4XAB=LX4xab!"
            set "line=!line:XS3X44=Xs3x44!"
            echo(!line!
    )) > newfile.txt
    

    Revision 1

    Here is how you can do it with multiple files and doing the naming the way you asked for.

    @echo off
    setlocal enabledelayedexpansion
    cd /d C:\Temp
    for %%a in (*.txt) do (
      echo %%~nxa|Find /i "_new">nul
      if errorlevel 1 (
        (for /f "tokens=*" %%f in (%%a) do (
            set "line=%%f"
            set "line=!line:LX4XAB=LX4xab!"
            set "line=!line:XS3X44=Xs3x44!"
            echo(!line!
        )) > %%~na_new.txt
      ) 
      echo %%~nxa|Find /i "_new">nul
      if errorlevel 1 ren %%~nxa %%~na.old
    )
    
    0 讨论(0)
提交回复
热议问题