run a script to rename several words in a txt file

牧云@^-^@ 提交于 2019-12-02 07:23:48

问题


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 and another word

until the end save the file with another name.

Can you help me? Thank you.


回答1:


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
)


来源:https://stackoverflow.com/questions/21932829/run-a-script-to-rename-several-words-in-a-txt-file

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