VB Script Text File Prepend

Does anyone know how to quickly add (add two new lines of text) to the beginning of an existing text file using either VB Script or Bat file? The most elegant solution gets a tick.

+3
source share
2 answers

How about this:

Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")

Set f = fso.OpenTextFile("test.txt", 1)
ReadAllTextFile =   f.ReadAll
Set f = fso.OpenTextFile("test.txt", 2, True)
f.WriteLine("Blaaa")
f.WriteLine("Blaaaa some more...")
f.Write(ReadAllTextFile)

Source: Tek Tips

+5
source

Mark JosΓ© Basilios for code and link to FSO. You will use it.

BUT: I would not go along the route ReadAllTextFile = f.ReadAll, since it can be several gigabytes (who knows?).

INSTEAD:

  • open new file
  • write prefix lines
  • read line by line from the old file, write to the new file
  • ( )
  • β†’
+5

Source: https://habr.com/ru/post/1707776/


All Articles