Search code examples
vbscriptwsh

How to create a file from Wscript.Echo strLine


Just starting with VBScript:

This script retrieves a file path from a text file and displays the output on the screen:

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("c:\temp\log.txt", ForReading)
Const ForReading = 1
Dim arrFileLines()
i = 0
Do Until objFile.AtEndOfStream
Redim Preserve arrFileLines(i)
arrFileLines(i) = objFile.ReadLine
i = i + 1
Loop
objFile.Close
For Each strLine in arrFileLines
WScript.Echo strLine
Next

WScript.Echo strLine displays the name of the file on the screen

Is there a way to create a file of the same name as the value contained in strLine ?

I have tried running:

cscript vbscript > strLine

but it only creates a file called "strLine" rather than the file that is stored in strLine

any assistance appreciated

thanks


Solution

  • Use Set tsX = objFS.CreateTextFile(strLine, True) - after reading about this method in the docs.

    Update:

    In code:

    Option Explicit
    Dim goFS : Set goFS = CreateObject("Scripting.FileSystemObject")
    Dim tsNames : Set tsNames = goFS.OpenTextFile("fnames.txt")
    Do Until tsNames.AtEndOfStream
       Dim tsX : Set tsX = goFS.CreateTextFile(tsNames.ReadLine())
       tsX.WriteLine "Abracadabra"
       tsX.Close
    Loop
    tsNames.Close