【问题标题】:How to create a new file by searching specific text in old file如何通过在旧文件中搜索特定文本来创建新文件
【发布时间】:2016-02-26 16:02:46
【问题描述】:

我的 OLDFile 看起来像 FolderList.txt,内容如下:

\\srv10177\temppahir$\Desktop\WORK\6758\Archive\Article\20140825_123400
\\srv10177\temppahir$\Desktop\WORK\6758\Archive\Article\20140827_065126
\\srv10177\temppahir$\Desktop\WORK\6758\Archive\Article\20141006_094447
\\srv10177\temppahir$\Desktop\WORK\6758\Archive\Article\20141006_110546
\\srv10177\temppahir$\Desktop\WORK\6758\Archive\Article\20141008_105947
\\srv10177\temppahir$\Desktop\WORK\6758\Archive\Article\20150917_093710
\\srv10177\temppahir$\Desktop\WORK\6758\Archive\Article\20151005_190254
\\srv10177\temppahir$\Desktop\WORK\6758\Archive\Article\20151005_191124

我想创建一个名称为FolderListNew.txt 的新文件,内容为:

\\srv10177\temppahir$\Desktop\WORK\6758\Archive\Article\20141006_110546
\\srv10177\temppahir$\Desktop\WORK\6758\Archive\Article\20141008_105947
\\srv10177\temppahir$\Desktop\WORK\6758\Archive\Article\20150917_093710
\\srv10177\temppahir$\Desktop\WORK\6758\Archive\Article\20151005_190254

所以基本上我想对 2 个文本进行搜索:

Text1 = 20141006_110546
Text2 = 20151005_190254

Text1 的第一次出现直到Text2 出现。 假设数据总是这样,并且按照这些时间戳升序排列。

我已经尝试了下面的脚本,它不起作用并且不完整:

Option Explicit
Dim objFSO, msg, Filename, file, filestreamOUT, objFile, strContents, strLine, line, Text1, Text2, OLDFilename, tmpStr, MyPos
Set objFSO = CreateObject("Scripting.FileSystemObject") 
Text1 = "20141006_110546"
Text2 = "20151005_190254"
OLDFilename="C:\Users\IBM_ADMIN\Desktop\VB\Folderlist.txt"
WScript.Echo "String to find is : " & Text1
Wscript.Echo "OLDFilename is " & OLDFilename
Set objFile = objFSO.OpenTextFile(OLDFilename, 1)
Wscript.Echo "Reading file the first time:"
strContents = objFile.ReadAll
Wscript.Echo "Line being read is" & strContents

WScript.Echo "tmpStr.ReadLine is : " & Line
'MyPos = InStr (tmpStr.ReadLine, Text1)
WScript.Echo "MyPos value is : " & MyPos
If MyPos >= 0 Then
  'WScript.Echo "Match NOT Found in File and tmpStr.ReadLine is : " & tmpStr.ReadLine
Else 
  WScript.Echo "string value is : " & strLine
  WScript.Echo "Match Found in File and tmpStr.ReadLine is : " & tmpStr.ReadLine
End If

【问题讨论】:

  • 我是 vb 脚本的新手。我仍然尝试了一些类似的东西..
  • 我无法将整个脚本以正确的格式放在这里。我猜这是 VB 脚本。如果我错了,请纠正我。
  • @simonalexander2005 - 我无法将我的脚本以正确的格式放在这里。我在上面发布的水,它可读吗?
  • @varocarbas - 这是 VB 脚本。我从一个星期以来一直在尝试自己学习这件事,但直到现在才能够实现这个简单的事情,而且脚本只是从这里开始,还有很长的路要走。因此,任何帮助都很棒,而且可以节省很多时间。谢谢!
  • 好的。下次确保使用正确的标签。请记住,VB.NET 是一个完全不同的故事(例如,它是一种编译语言)。

标签: vbscript scripting


【解决方案1】:

如果您想将一个文件中的特定行放入另一个文件中,最好逐行读取源文件:

Set inFile = objFSO.OpenTextFile(OLDFilename)
Do Untile inFile.AtEndOfStream
  line = inFile.ReadLine
  ...
Loop
inFile.Close

如果该行包含Text1,则开始写入输出文件,并在包含Text2后停止写入:

Text1 = "20141006_110546"
Text2 = "20151005_190254"

writeOutput = False

Set inFile = objFSO.OpenTextFile("C:\path\to\input.txt")
Set outFile = objFSO.OpenTextFile("C:\path\to\output.txt", 2, True)
Do Until inFile.AtEndOfStream
  line = inFile.ReadLine
  If InStr(line, "20141006_110546") > 0 Then writeOutput = True
  If writeOutput Then outFile.WriteLine line
  If InStr(line, "20151005_190254") > 0 Then Exit Do
Loop
inFile.Close
outFile.Close

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-25
    • 1970-01-01
    • 1970-01-01
    • 2019-05-05
    • 2013-03-18
    • 2015-02-24
    相关资源
    最近更新 更多