我想创建一个批处理,同时在批处理文件中查找特定的行并能够编辑这些行。在TXT文件批处理/查找和编辑行
例子:
// txt文件//
ex1
ex2
ex3
ex4
我希望让该批处理文件找到 'EX3' 和编辑本 '的EX5',让它看起来像这样:
ex1
ex2
ex5
ex4
我想创建一个批处理,同时在批处理文件中查找特定的行并能够编辑这些行。在TXT文件批处理/查找和编辑行
例子:
// txt文件//
ex1
ex2
ex3
ex4
我希望让该批处理文件找到 'EX3' 和编辑本 '的EX5',让它看起来像这样:
ex1
ex2
ex5
ex4
在本地Windows安装上,您可以使用批处理(cmd.exe)或vbscript,而无需获取外部工具。 这里是在VBScript
Set objFS = CreateObject("Scripting.FileSystemObject")
strFile = "c:\test\file.txt"
Set objFile = objFS.OpenTextFile(strFile)
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
If InStr(strLine,"ex3")> 0 Then
strLine = Replace(strLine,"ex3","ex5")
End If
WScript.Echo strLine
Loop
另存为myreplace.vbs和命令行
c:\test> cscript /nologo myreplace.vbs > newfile
c:\test> ren newfile file.txt
这是一种东西sed
是作了(当然,你需要sed在你的系统上)。
SED的/ EX3/EX5/G'input.txt中> output.txt的
你要么需要一个Unix系统或Windows Cygwin样的平台这一点。
还有GnuWin32 for sed。 (GnuWin32 installation and usage)。
这是我需要的东西太多。我想设置这个公众;所以它应该轻松完成。 – 2009-07-12 08:30:24
上的例子没有搜索,并在XP或2K3命令行替换功能或流编辑(不要了解Vista或更高版本)。所以,你需要使用一个像Ghostdog发布的脚本,或者搜索并替换sed等有用的工具。
不止一种方法去做一件事,因为这脚本显示:
@echo off
SETLOCAL=ENABLEDELAYEDEXPANSION
rename text.file text.tmp
for /f %%a in (text.tmp) do (
set foo=%%a
if !foo!==ex3 set foo=ex5
echo !foo! >> text.file)
del text.tmp
ghostdog74的例子提供了我需要的核心,因为我以前从未编写过任何VBS要做到这一点需要。这不是完美的,但我充实了这个例子到一个完整的脚本,以防有人发现它有用。
'ReplaceText.vbs
Option Explicit
Const ForAppending = 8
Const TristateFalse = 0 ' the value for ASCII
Const Overwrite = True
Const WindowsFolder = 0
Const SystemFolder = 1
Const TemporaryFolder = 2
Dim FileSystem
Dim Filename, OldText, NewText
Dim OriginalFile, TempFile, Line
Dim TempFilename
If WScript.Arguments.Count = 3 Then
Filename = WScript.Arguments.Item(0)
OldText = WScript.Arguments.Item(1)
NewText = WScript.Arguments.Item(2)
Else
Wscript.Echo "Usage: ReplaceText.vbs <Filename> <OldText> <NewText>"
Wscript.Quit
End If
Set FileSystem = CreateObject("Scripting.FileSystemObject")
Dim tempFolder: tempFolder = FileSystem.GetSpecialFolder(TemporaryFolder)
TempFilename = FileSystem.GetTempName
If FileSystem.FileExists(TempFilename) Then
FileSystem.DeleteFile TempFilename
End If
Set TempFile = FileSystem.CreateTextFile(TempFilename, Overwrite, TristateFalse)
Set OriginalFile = FileSystem.OpenTextFile(Filename)
Do Until OriginalFile.AtEndOfStream
Line = OriginalFile.ReadLine
If InStr(Line, OldText) > 0 Then
Line = Replace(Line, OldText, NewText)
End If
TempFile.WriteLine(Line)
Loop
OriginalFile.Close
TempFile.Close
FileSystem.DeleteFile Filename
FileSystem.MoveFile TempFilename, Filename
Wscript.Quit
您可以随时使用“FAR”=“查找和替换”。它是用java编写的,所以它在Java工作的地方工作(几乎在任何地方)。与目录和子目录一起工作,搜索和替换文件,也可以重命名它们。也可以重新命名批量文件.Licence =免费,适用于个人或公司。非常快,由开发人员维护。在这里找到它:http://findandreplace.sourceforge.net/
也可以使用GrepWin。工作几乎相同。你可以在这里找到它:http://tools.tortoisesvn.net/grepWin.html
你可以这样做:
rename %CURR_DIR%\ftp\mywish1.txt text.txt
for /f %%a in (%CURR_DIR%\ftp\text.txt) do (
if "%%a" EQU "ex3" (
echo ex5 >> %CURR_DIR%\ftp\mywish1.txt
) else (
echo %%a >> %CURR_DIR%\ftp\mywish1.txt
)
)
del %CURR_DIR%\ftp\text.txt
@echo off
set "replace=something"
set "replaced=different"
set "source=Source.txt"
set "target=Target.txt"
setlocal enableDelayedExpansion
(
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" %source%') do (
set "line=%%b"
if defined line set "line=!line:%replace%=%replaced%!"
echo(!line!
)
) > %target%
endlocal
Source。希望它能帮助一些人。
它看起来很清楚,如果我说'BATCH' – 2009-07-12 09:14:14