5/10/11

How to Edit Text in VBS

Visual Basic Script (VBS) can be used to easily create and edit text files. This can be used for automating logs, manipulating configuration files and many other tasks, as well. The key is the FileSystemObject class, which contains functions for both creating new text files and opening existing files. he text file can then be read from and written to just as if it were the command prompt.
    • 1

      Open your favorite text editor.

    • 2

      Paste the following:

      dim FSO, textFile

      const ForWriting = 2

      const ForAppending = 8

      set FSO = CreateObject("Scripting.FileSystemObject")

      set textFile = FSO.OpenTextFile("c:\textfile.txt", ForWriting)

      textFile.WriteLine "This will overwrite all the current contents of the file and replace it with this sentence."

      textFile.Close

      set textFile = FSO.OpenTextFile("c:\textfile.txt", ForAppending)

      textFile.WriteLine "This will be appended to the end of the file!"

      textFile.Close

      This declares two constants for two different file input/output modes: ForWriting and ForAppending. If a file is opened with the "ForWriting" flag, then new text will wipe all the current contents of the file. On the other hand, if the "ForAppending" flag is used, the new text will be added to the end of the current file, preserving all its contents.

    • 3

      Save your work. You can run the script by double-clicking it.

  • No comments: