2015-11-24 121 views
1

我的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 
+0

我是vb脚本的新手。我仍然尝试了一些东西,例如.. – pals

+0

我无法将整个脚本放在适当的格式中。我想这是VB脚本。如果我错了,请纠正我。 – pals

+0

@ simonalexander2005 - 我无法将我的脚本放在适当的格式中。我已经在上面发布了Watever,它是否可读? – pals

回答

0

如果你想要把特定的行从一个文件到另一个文件,最好逐行读取源文件行:

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 
相关问题