2017-04-05 10 views
0

次のコードを使用して、各行の先頭に;を追加できましたが、特定の単語が見つかった後に;を追加したいとします。 [Abc]。どのようにVBScriptを使用してこれを行うには?私は特定の単語を検索したいと思っています。開始時に

Const ForReading=1 
Const ForWriting=2 

Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set f = objFSO.OpenTextFile("D:\sam.txt", ForReading) 

Do Until f.AtEndOfStream 
    strText = f.ReadLine 
    If Len(strText) = 0 Then 
     blnFound = True 
     MsgBox "blank line found" 
     strText = vbNewLine & strText 
     strContents = strContents & strText & vbCrlf 
    Else 
     strText = ";" & strText 
     strContents = strContents & strText & vbCrlf 
    End If 
Loop 

f.Close 

Set f = objFSO.OpenTextFile("D:\sam.txt", Forwriting) 
f.WriteLine strContents 
f.Close 

Sam.txtには、いくつかの行が含まれています。

 
Hi, need help 
This is a sample text file 
[Abc] 
How are you 
Hope you are doing well! 

だから私は、出力sam.txtファイルが内部のデータの下に持っていなければならないとします

だから、
 
Hi, need help 
This is a sample text file 
[Abc] 
;How are you 
;Hope you are doing well! 
+0

をお試しください。 'strText = replace(strText、" [Abc] "、" [Abc]; ")' –

+0

私の投稿に返信していただきありがとうございます。 Sam.txtは、例えば こんにちはためのいくつかの行を含む。これは、あなたが あなたはよくやっている願っていますどのようにサンプルテキストファイル [Abcの] ある を助けが必要です! だから私は、ファイルsam.txt出力が内部のデータの下に持っている必要がありますしたい: - 、 こんにちは これを助ける必要はサンプルテキストファイル [Abcの] であり、どのようにして であり、あなたはよくやっている願っています! – sam

答えて

0

、基本的に、あなたはINI形式のファイルを持っており、特定のセクションのエントリがコメントしたいと思います。それは次のように達成することができます。

filename = "D:\sam.txt" 

Set fso = CreateObject("Scripting.FileSystemObject") 

txt = Split(fso.OpenTextFile(filename).ReadAll, vbNewLine) 

disable = False 
For i = 0 To UBound(txt) 
    If Left(txt(i), 1) = "[" Then 
    If txt(i) = "[Abc]" Then 
     disable = True 
    Else 
     disable = False 
    End If 
    End If 

    If disable Then txt(i) = ";" & txt(i) 
Next 

fso.OpenTextFile(filename, 2).Write Join(txt, vbNewLine) 
+0

はい、あなたは正しいですが、事は複数のファイルがあり、1つのファイルに複数のセクションがあり、その特定のファイルの要件に従って特定のセクションをコメントしたいと思っています。特定のセクションを検索し、そのセクションをコメントアウトしてください。ありがとう。 – sam

+0

私が投稿したサンプルコードは、すでにそれを行うべきです。 –

+0

はい、しかし、ファイル名とセクション名が異なるので、私はそれがcmdや入力ダイアログbox.Butを介してユーザーから受け入れられるようにしたいが、私はそれがどのようにスクリプトでinputboxを使用するようになっていない。 strAns1 = InputBox関数(「あなたの新しいファイルの名前を入力してください:」) Wscript.EchoステートメントstrAns1 strAns2 = InputBox関数(「セクション名を入力してください」) Wscript.EchoステートメントstrAns2 – sam

0

はそれを置き換えるこの

Option Explicit 

Dim FSO ' Object 

Set FSO = CreateObject("Scripting.FileSystemObject") 

Dim ReadTxtFile, WriteTxtFile ' Object 
Dim TextLine, TextLineToWrite ' String 
Dim AddStr' bool 

' Open both text file in the same time 
Set ReadTextFile = FSO.OpenTextFile("Sam.txt", 1) ' Open file to read 
Set WriteTextFile = FSO.OpenTextFile("Sam_new.txt", 2, True) ' Open file to write 

' Do read file as normal but add a switch 
' Write original text line to text file while switch is disabled 
' Add str to the text line and write once switch is trigger 

AddStr = False ' Add str disabled 
Do Until ReadTextFile.AtEndOfStream ' Start Read 

    Textline = ReadTextFile.Readline 

    If AddStr = True Then ' If add str enabled 
     TextLineToWrite = ";" & Textline ' Add string 
    Else ' if add str disabled 
     TextLineToWrite = Textline ' write original line 
    End If 

    If Trim(Textline) = "[ABC]" Then ' If indicator read 
     AddStr = True ' add str write 
    End if 

    WriteTextFile.WriteLine TextLineToWrite ' Write file when each line is read 
Loop 

ReadTextFile.Close 
WriteTextFile.Close 

msgbox "Done" 
+0

'設定しWriteTextFile = FSO.WriteTextFile( ...) '??? –

+0

申し訳ありませんが、 'Set WriteTextFile = FSO.OpenTextFile(...)'とし、 'Dim StartWrite'を' Dim AddStr'に変更します –

関連する問題