2016-11-02 4 views
0

私は数字のテキストファイルを持っています。行に値がある場合、テキストファイルの前の行を削除する

45048 
67113.88 
74484 
597.6 
1945.65 
8714.5 
32085.9 
741.12 
1721.39 
35266.7 
8260.71 
23635.8 
40487.5 
40702.18 
29544.74 
110000 
810000 
3161000 
29201.91 
33000 

したがって、番号110000より前の行を削除する必要があります。または、別の日に私は1721.39より前の行を削除する必要があります。私は、ユーザーの入力値の前に削除を行う方法を知らない。ユーザーのプロンプト25の場合は最初の25行を削除してください。

Const ForReading = 1 
Const ForWriting = 2 

Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objFile = objFSO.OpenTextFile("C:\db\a.txt", ForReading) 

strText = objFile.ReadAll 
objFile.Close 
strNewText = Replace(strText, " ", "") 

Set objFile = objFSO.OpenTextFile("C:\db\a.txt", ForWriting) 
objFile.WriteLine strNewText 

objFile.Close 

Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objFile = objFSO.OpenTextFile("C:\db\a.txt", ForReading) 

strText = objFile.ReadAll 
objFile.Close 

strNewText = Replace(strText, "forward", "") 

Set objFile = objFSO.OpenTextFile("C:\db\a.txt", ForWriting) 
objFile.WriteLine strNewText 
objFile.Close 

Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objFile = objFSO.OpenTextFile("C:\db\a.txt", ForReading) 

strText = objFile.ReadAll 
objFile.Close 

strNewText = Replace(strText, ".", "") 

Set objFile = objFSO.OpenTextFile("C:\db\a.txt", ForWriting) 
objFile.WriteLine strNewText 
objFile.Close 

Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objFile = objFSO.OpenTextFile("C:\db\a.txt", ForReading) 

strText = objFile.ReadAll 
objFile.Close 

strNewText = Replace(strText, " ", "") 

Set objFile = objFSO.OpenTextFile("C:\db\a.txt", ForWriting) 
objFile.WriteLine strNewText 
objFile.Close 

Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objFile = objFSO.OpenTextFile("C:\db\a.txt", ForReading) 

Do Until objFile.AtEndOfStream 
    strLine = objFile.Readline 
    strLine = Trim(strLine) 
    If Len(strLine) > 0 Then 
     strNewContents = strNewContents & strLine & vbCrLf 
    End If 
Loop 

objFile.Close 

Set objFile = objFSO.OpenTextFile("C:\db\a.txt", ForWriting) 
objFile.Write strNewContents 
objFile.Close 
+0

他人がコードを書く場所ではありません。これまでに何を試しましたか(コードを表示してください)、期待どおりに動かないものはありますか? –

+0

申し訳ありませんコードを忘れました:D – wittman

答えて

2

これを行数を削除するには、試してみてください。

Const FOR_READING = 1 
Const FOR_WRITING = 2 
strFileName = "C:\txt.txt" 
NumberOfLinesToBeDelete=inputbox("How many lines want to delete from beginning","Delete","") 


Set objFS = CreateObject("Scripting.FileSystemObject") 
Set objTS = objFS.OpenTextFile(strFileName, FOR_READING) 
strContents = objTS.ReadAll 
objTS.Close 

arrLines = Split(strContents, vbNewLine) 
Set objTS = objFS.OpenTextFile(strFileName, FOR_WRITING) 

For i=0 To UBound(arrLines) 
    If i > (NumberOfLinesToBeDelete - 1) Then 
     objTS.WriteLine arrLines(i) 
    End If 
Next 
+0

ありがとうございました。 – wittman

+0

あなたはようこそ! – Arokiyanathan

関連する問題