여기 (상관없이 그 라인의 내용) 텍스트 파일의 상단 선 (15)을 제거하는 방법의 일례이다. 한 번에 한 줄에서 작업 할 때 대용량 파일을 임의로 처리 할 수 있어야합니다.
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
당신은 how to traverse a directory tree here의 예를 볼 수있다, 또는 대안 당신은 모든 파일 경로 이름의 목록을 얻을 수 있으며, 한 번에 그것을 하나 개의 파일을주고 다른 루프에서이 절차를 호출합니다.
업데이트 : 다음은 줄 수 대신 "시간"이 포함 된 문자열을 찾아 그 이후의 줄만 복사하는 다른 버전입니다.
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
이것은 Excel과 어떤 관련이 있습니까? Excel이 어느 시점에서이 문제에 관여합니까? –
배치 스크립트를 사용하여이 작업을 수행하는 것이 좋으면 http://stackoverflow.com/questions/11428692/batch-file-to-delete-first-3-lines-of-a-text-file을 참조하십시오. 또한 하위 폴더 내에 여러 파일을 처리하도록 향상시킵니다. –
메모장에는 매크로 기능이 없습니다. 도구에 대한 가정없이 문제를 기술하고 시도한 내용을 설명하십시오. – Richard