▽1
●
a-kuma3 ●300ポイント ベストアンサー |
こんな感じになります。
targetFolderName = "D:\test\hoge" ' ※ hoge フォルダのパス Set fs = WScript.CreateObject("Scripting.FileSystemObject") Set targetFolder = fs.getFolder(targetFolderName) For Each folder In targetFolder.SubFolders For Each f In folder.Files If f.Name = "a.html" Then Set target = fs.OpenTextFile(f.Path, 8) ' for append target.WriteLine "----------------" target.WriteLine "追加する文章" target.WriteLine "----------------" target.Close End If Next Next
すみません、UTF8のテキストファイルに書き込んだら
追加する文章 という文字が文字化けしてしまいました
文字化けせずにするにはどうすればいいでしょうか?
UTF-8 の場合には、以下のようにしてください。
Set ado = WScript.CreateObject("adodb.stream") ado.Type = 2 ado.Charset = "UTF-8" Set fs = WScript.CreateObject("Scripting.FileSystemObject") Set targetFolder = fs.getFolder(targetFolderName) For Each folder In targetFolder.SubFolders For Each f In folder.Files If f.Name = "a.html" Then ado.Open ado.LoadFromFile f.Path txt = ado.ReadText ado.Close ado.Open ado.Position = 0 ado.WriteText txt ado.WriteText "----------------" & vbCrLf ado.WriteText "追加する文章" & vbCrLf ado.WriteText "----------------" & vbCrLf ado.SaveToFile f.Path, 2 ado.Close End If Next Next Set ado = Nothing