首页 > 解决方案 > 在文件中查找字符串并复制该行

问题描述

我正在处理一个 CSV 文件,我想检查其中是否有特定的字符串。如果是这样,我想复制找到字符串的行。

编码:

x = InputBox("String to find")

fileName = "file.csv"

Set oshell = CreateObject("WScript.Shell")
Set fs = CreateObject("Scripting.FileSystemObject")

Set fich = fs.OpenTextFile(fileName, 1, "True")

linea = fich.ReadAll

'here I want to read the row where I found the value of x

line = Left (linea, 117)
MsgBox line

fich.Close

标签: csvvbscript

解决方案


你逐行阅读。

Set Inp = WScript.Stdin
Set Outp = Wscript.Stdout
Do Until Inp.AtEndOfStream
    Text = Inp.readline
    If InStr(LCase(Text), "cat") then Msgbox Text
Loop

在命令提示符中使用

cscript //nologo C:\folder\file.vbs < C:\folder\FileToSearch.txt

推荐阅读