【发布时间】:2018-09-21 16:15:07
【问题描述】:
我有一个包含数千条记录的文本文件 Sample.txt。 见下文。我创建了接受截止记录输入框的 vbscript。在这个例子中,我输入了 10005 作为截止记录。 然后脚本将读取截止记录之后的所有记录,在这种情况下从 10006 开始到 10010,然后以当前日期作为文件名(如 20180920.txt)写入一个新的文本文件
10000 10001 10002 10003 10004 10005 10006 10007 10008 10009 10010预期输出:
10006 10007 10008 10009 10010示例脚本,但尚未完成新文件的编写。
Dim Lastrecord
Dim IsFound, IsFound2
Dim CurrentDate
Const ForReading = 1
Set wShell = CreateObject("WScript.Shell")
Set oExec = wShell.Exec("mshta.exe ""about:<input type=file id=FILE><script>FILE.click();new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).WriteLine(FILE.value);close();resizeTo(0,0);</script>""")
sFileSelected = oExec.StdOut.ReadLine
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(sFileSelected)
Set objFile1 = objFSO.OpenTextFile(objFSO.GetAbsolutePathName(objFile), ForReading)
CurrentDate = Replace(Date, "/", "")
Lastrecord = InputBox("Last Last Record:")
IsFound = 0
IsFound1 = 0
Do Until objFile1.AtEndOfStream
strLine = objFile1.ReadLine
If Trim(strLine) = Trim(LastRecord) Then
IsFound = 1
Exit Do
End If
Loop
objFile1.Close
WScript.Echo strLine
WScript.Echo Trim(Lastrecord)
-------我已经完成了脚本,请参阅下面的 @Ansgar Wiechers 的功劳。
Dim Lastrecord
Dim IsFound, IsFound2
Dim CurrentDate
Const ForReading = 1
Set wShell=CreateObject("WScript.Shell")
Set oExec=wShell.Exec("mshta.exe ""about:<input type=file id=FILE><script>FILE.click();new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).WriteLine(FILE.value);close();resizeTo(0,0);</script>""")
sFileSelected = oExec.StdOut.ReadLine
CurrentDate = Year(NOW) & Right("00" & Month(NOW), 2) & Right("00" & Day(NOW), 2) & Right("00" & Hour(NOW), 2) & Right("00" & Minute(NOW), 2) & Right("00" & Second(NOW), 2)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(sFileSelected)
Set objFile1 = objFSO.OpenTextFile(objFSO.GetAbsolutePathName(objFile), ForReading)
Lastrecord = InputBox("Last Last Record:")
IsFound = False
Set outFile = objFSO.CreateTextFile(objFSO.GetParentFolderName(objFile) & "\" & CurrentDate & ".DAT", True)
Do Until objFile1.AtEndOfStream
strLine = objFile1.ReadLine
If IsFound Then outFile.WriteLine strLine
If Trim(strLine) = Trim(LastRecord) Then IsFound = True
Loop
Wscript.Echo "New file created successfully at: " & objFSO.GetParentFolderName(objFile) & "\" & CurrentDate & ".DAT"
outFile.Close
objFile1.Close
【问题讨论】:
-
脚本在哪里?没有它,很难说出如何修复您的代码
-
@PankajJaju ,见上文我附加了我的初始脚本,但写入一个新的文本文件,我还没有完成。
-
我假设行计数器(10005、10005 等)中不会有任何重复?你能确认一下吗?
-
@pankajJaju ,有可能,但是当它找到第一个命中时,无论它是否有重复,它都会获取下面的所有记录并将其写入不同的 txt 文件中。
-
重新打开,b/c 所谓的重复是关于如何读取/写入文件,而 OP 的问题是关于如何选择性地将数据从一个文件写入另一个文件。
标签: vbscript