How to “Save As” text template

Posted by Albert Gareev on Oct 26, 2010 | Categories: How toSource codeText Data

Parent page: Service Functions – System (QTP, VBScript)


I need to modify text file template (add a few lines) and “Save as”.


One of the typical challenges that people face while trying to automate work tasks, is “procedure – result” confusion. Here, the end result* is confused with a way** to do the task.

* What is wanted

Text file with required contents is stored under defined name in required location.

** How it’s done manually

Open text file template in a text editor – add required text – save as new file (select path to store, type in new name).

How to automate

First of all, check that file exists. Create a copy of the template file. If you are not sure that new name and/or location is valid, use error handling. Open text file for appending and add required text. Close file to get it saved.

Note. Sample code below is equally suitable for QTP and TestComplete. 


  Dim FSO
  Dim objFile, objTextStream
  Dim boolRC
  Set FSO = CreateObject("Scripting.FileSystemObject")

  Call FSO.CopyFile("C:\Temp\template.txt", "C:\Temp\myblog.txt", True)   
  Set objTextStream = FSO.OpenTextFile("C:\Temp\myblog.txt", 8, True)
  Set objTextStream = Nothing
  Set objFile = Nothing
  Set FSO = Nothing

  • Leave a Reply

    * Required
    ** Your Email is never shared

Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported
This work by Albert Gareev is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported.