2017-01-05 5 views
0

私はプログラムが必要です。ユーザーが入力した文字列をファイルでチェックし、文字列が存在する場合はメッセージを表示しますが、存在しない場合はリストに追加します。ファイルの末尾に追加するには?

Const ForReading = 1 

Dim strSearchFor 
strSearchFor = inputbox("What is the url of the song?",,"") 

Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objTextFile = objFSO.OpenTextFile("autoplaylist.txt", ForAppending) 

do until objTextFile.AtEndOfStream 
    strLine = objTextFile.ReadLine() 

    If InStr(strLine, strSearchFor) <> 0 then 
     Wscript.Echo "That song is already in the list." 
    Else 


     Wscript.Echo "That song was added to end of list." 
    End If 
loop 
objTextFile.Close 

しかし、私は、ファイルにテキストを追加するかどうかはわかりません。ここで

は、私がこれまで持っているものです。 また、1行ごとにメッセージが表示され、3000行が表示されます。これを修正する方法はありますか?

答えて

1

これはどう...

Const ForReading = 1 
Const ForAppending = 8 

Dim strSearchFor, strFileText, strFileName 
strSearchFor = inputbox("What is the url of the song?",,"") 

Set objFSO = CreateObject("Scripting.FileSystemObject") 

strFileName = "autoplaylist.txt" 

' Check file exists and ReadAll 
' ------------------------------ 
If objFSO.FileExists(strFileName) Then 
    On Error Resume Next 

    With objFSO.OpenTextFile(strFileName, ForReading) 
     strFileText = .ReadAll 
     .Close 
    End With 

    If Err.Number <> 0 Then 
     WScript.Echo "File access error" 
     WScript.Quit 
    End If 

    On Error Goto 0 
Else 
    Wscript.Echo "File does not exists" 
    Wscript.Quit 
End If 

' Search for input string 
' If found append user input 
' ---------------------------- 
If Instr(strFileText, strSearchFor) = 0 Then 
    With objFSO.OpenTextFile(strFileName, ForAppending) 
     .WriteLine(strSearchFor) 
     .Close 
    End With 
    Wscript.Echo strSearchFor & " was not found in " & strFileName & " and has been appended" 
Else 
    Wscript.Echo strSearchFor & " has been found in " & strFileName 
End If 

WScript.Quit 
関連する問題