使用批处理文件查找和替换文本

时间:2011-08-08 11:45:40

标签: batch-file replace

我需要一个批处理文件,它在文本文件中搜索字符串并用另一个字符串替换它。我试图做this之类的事情,但它不起作用。谁能告诉我我做错了什么?

@echo off
setLocal DisableDelayedExpansion

if not exist "%1" (echo FindAndReplace: File Not Found)
set /p findX= Find ?
set /p replaceWithX= Replace With ?

for /f "tokens=* delims= " %%G in (%1) do (
    set str=%%G
    setLocal EnableDelayedExpansion
    set str=!str:[%findX%]=[%replaceWithX%]!
    >> %1.txt echo(!str!
    endlocal
)

1 个答案:

答案 0 :(得分:0)

这里有一些有用的代码就是它的帮助菜单

搜索文件或文件中的文本字符串。

FIND [/V] [/C] [/N] [/I] [/OFF[LINE]] "string" [[drive:][path]filename[ ...]]

/V         Displays all lines NOT containing the specified string.
/C         Displays only the count of lines containing the string.
/N         Displays line numbers with the displayed lines.
/I         Ignores the case of characters when searching for the string.
/OFF[LINE] Do not skip files with offline attribute set.
"string"   Specifies the text string to find.
[drive:][path]filename
         Specifies a file or files to search.

If a path is not specified, FIND searches the text typed at the prompt
or piped from another command.