批处理/查找和编辑 TXT 文件中的行 [英] Batch / Find And Edit Lines in TXT file

查看:33
本文介绍了批处理/查找和编辑 TXT 文件中的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个批处理,while 可在批处理文件中查找特定行并能够编辑这些行.

I want to create a batch while which finds specific lines in a batch file and are able to edit these lines.

示例:

//TXT 文件//

ex1
ex2
ex3
ex4

我想让批处理文件找到ex3"并将其编辑为ex5"以使其看起来像这样:

i want to let the batch file find 'ex3' and edit this to 'ex5' to let it look like this:

ex1
ex2
ex5
ex4

推荐答案

在本机 Windows 安装中,您可以使用批处理 (cmd.exe) 或 vbscript,而无需获取外部工具.这是 vbscript 中的一个示例:

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:	estfile.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 并在命令行中:

Save as myreplace.vbs and on the command line:

c:	est> cscript /nologo myreplace.vbs  > newfile
c:	est> ren newfile file.txt

这篇关于批处理/查找和编辑 TXT 文件中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆