如何使用.bat文件替换文本文件中的单词?

时间:2014-04-25 05:46:20

标签: windows batch-file txtextcontrol

我是蝙蝠脚本的新手,所以我打算在名为“sampleFile.txt”的文本文件中更改单词

任何人都可以帮助我吗?谢谢你:)

1 个答案:

答案 0 :(得分:4)

使用下面的批处理文件(如此示例),将apple替换为orange中的file.txt,并将更改写入newfile.txt

changefile.bat "apple" "orange" "file.txt" >"newfile.txt"

检查DOS Batch - Find and Replace

@echo off
REM -- Prepare the Command Processor --
SETLOCAL ENABLEEXTENSIONS
SETLOCAL DISABLEDELAYEDEXPANSION

::BatchSubstitude - parses a File line by line and replaces a substring"
::syntax: BatchSubstitude.bat OldStr NewStr File
::          OldStr [in] - string to be replaced
::          NewStr [in] - string to replace with
::          File   [in] - file to be parsed
:$changed 20100115
:$source http://www.dostips.com
if "%~1"=="" findstr "^::" "%~f0"&GOTO:EOF
for /f "tokens=1,* delims=]" %%A in ('"type %3|find /n /v """') do (
    set "line=%%B"
    if defined line (
        call set "line=echo.%%line:%~1=%~2%%"
        for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X
    ) ELSE echo.
)