2015-11-24 82 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 
相關問題