Tuesday, April 20, 2010

Creating multiple Text files in VB Script using FileSystemObject

Back To File System Object Page

How to create multiple Text Files?

Using the File System Object method CreateTextFile we create a Text file and using a For Loop we create any number of Text files as desired. Using a InputBox we can also ask the user how many text files to be created.


Example 1: Assuming we know how many text files to create.

'Create a Text File using FSO or FileSystemObject
'Step 1: Create an object variable that interacts with the File System Object Library
Set Fso = CreateObject("Scripting.FileSystemObject")
'Step 2: Use For Loop to run the script for 100 times.
For I = 1 to 5
Fso.CreateTextFile("F:\Test"&i&".txt")
var = "" &var&", Test" &i
Next
Msgbox" The Text Files Created are "&Var

Example 2: Asking the user for input to print that many number of files.

Rewrite the code by just adding one line with InputBox function.

'Create a Text File using FSO or FileSystemObject
'Step 1: Create an object variable that interacts with the File System Object Library
Set Fso = CreateObject("Scripting.FileSystemObject")
'Step 2: Use For Loop to run the script for 100 times.
n=CINT(InputBox("Enter how many files to be created"))
For I = 1 to n
Fso.CreateTextFile("F:\Test"&i&".txt")
var = "" &var&", Test" &i
Next
Msgbox" The Text Files Created are "&Var

Back To File System Object Page

No comments:

Post a Comment