在记事本中编辑文本

我需要帮助编辑记事本.csv文件中的文本。 我有一个Excel文件太多的行。 我需要在记事本中打开文件,在记事本中删除该文件的前15行,并将其保存为.txt文件。 如果可能,我希望能够select一个文件夹,其中包含多个.csv的子文件夹,我需要运行此macros。 前15行不总是包含相同的文本。 有人可以帮我解决这个问题吗?

谢谢

这里是一个过程的例子,从任何文本文件中删除前15行(不pipe这些行的内容如何)。 它应该能够处理任意大的文件,因为它一次在一行上工作。

Sub removeTopLines() Dim srcFile As String, nSrc As Integer ' source file (read from) Dim dstFile As String, nDst As Integer ' destination file (write to) Dim textLine As String Const LINES_TO_SKIP = 15 Dim lineCounter As Long srcFile = "c:\opt\src.txt" dstFile = "c:\opt\dst.txt" nSrc = FreeFile Open srcFile For Input As #nSrc nDst = FreeFile Open dstFile For Output As #nDst lineCounter = 1 Do Until EOF(nSrc) Line Input #nSrc, textLine If lineCounter > LINES_TO_SKIP Then Print #nDst, textLine End If lineCounter = lineCounter + 1 Loop Close #nDst Close #nSrc End Sub 

您可以在这里看到如何遍历目录树的示例,或者您可以只获取所有这些文件path名的列表,并从另一个循环中一次调用一个文件来调用此过程。

更新:这是另一个版本,而不是一个行计数查找包含“时间”的string,只复制后面的行。

 Sub removeTopLinesAfter() Dim srcFile As String, nSrc As Integer ' source file (read from) Dim dstFile As String, nDst As Integer ' destination file (write to) Dim textLine As String, strAfter As String strAfter = "time" Dim copyLines As Boolean srcFile = "c:\opt\src.txt" dstFile = "c:\opt\dst.txt" nSrc = FreeFile Open srcFile For Input As #nSrc nDst = FreeFile Open dstFile For Output As #nDst copyLines = False Do Until EOF(nSrc) Line Input #nSrc, textLine If Not copyLines Then copyLines = InStr(textLine, strAfter) > 0 Else Print #nDst, textLine End If Loop Close #nDst Close #nSrc End Sub