批处理/查找和编辑在行TXT文件 [英] Batch / Find And Edit Lines in TXT file
本文介绍了批处理/查找和编辑在行TXT文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在这一个批处理文件中找到特定的行创建一个批处理,并能够编辑这些行。
例如:
// txt文件//
EX1
EX2
EX3
EX4
我希望让该批处理文件找到EX3和编辑本'的EX5',让它看起来像这样:
EX1
EX2
EX5
EX4
解决方案
这是一个原生的Windows安装,您可以使用批处理(CMD.EXE)或VBScript,而不需要得到外部工具。
这里是在VBScript中的例子
设置OBJFS =的CreateObject(Scripting.FileSystemObject的)
strFile =C:\\测试\\ file.txt的
设置OBJFILE = objFS.OpenTextFile(strFile)
做,直到objFile.AtEndOfStream
strLine中= objFile.ReadLine
如果INSTR(strLine中,EX3)> 0,则
strLine中=替换(strLine中,EX3,EX5)
万一
WScript.Echo strLine中
循环
另存为myreplace.vbs和命令行
C:\\试验> CSCRIPT / NOLOGO myreplace.vbs> newfile中
C:\\试验>仁的newfile file.txt的
I want to create a batch while which finds specific lines in a batch file and are able to edit these lines.
Example:
//TXT FILE//
ex1
ex2
ex3
ex4
i want to let the batch file find 'ex3' and edit this to 'ex5' to let it look like this:
ex1
ex2
ex5
ex4
解决方案
on a native Windows install, you can either use batch(cmd.exe) or vbscript without the need to get external tools. here's an example in 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
save as myreplace.vbs and on the command line
c:\test> cscript /nologo myreplace.vbs > newfile
c:\test> ren newfile file.txt
这篇关于批处理/查找和编辑在行TXT文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文