How to “Save As” text template
Parent page: Service Functions – System (QTP, VBScript)
Question
I need to modify text file template (add a few lines) and “Save as”.
Answer
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.
Implementation
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) objTextStream.WriteLine("http://automation-beyond.com/") objTextStream.Close() Set objTextStream = Nothing Set objFile = Nothing Set FSO = Nothing