martinshort
Technical User
Hi all
I'm having problems writing out to a csv. This works perfectly if I run it from the local PC, but as soon as I run it from a scheduled task, the output file is created and correctly named but no data is output; the file being 0 Kb.
I have tried running this as a local user, local admin and domain admin to no avail.
Any thoughts from anyone as to what I am doing wrong.
Many thanks
Martin
(One confused VBS newbie!)
I'm having problems writing out to a csv. This works perfectly if I run it from the local PC, but as soon as I run it from a scheduled task, the output file is created and correctly named but no data is output; the file being 0 Kb.
I have tried running this as a local user, local admin and domain admin to no avail.
Any thoughts from anyone as to what I am doing wrong.
Many thanks
Martin
(One confused VBS newbie!)
Code:
Option Explicit
Dim objFSO, objFolder, objShell, objTextFile, objFile
Dim objNetwork, objPrinter, intDrive, intNetLetter
Dim strDirectory, strFile, strText, strPCName
Dim regComputerName
'Read in PCName to variable called strPCName
regComputerName = "HKLM\SYSTEM\CurrentControlSet\Control" &
"\ComputerName\ComputerName\ComputerName"
Set objShell = CreateObject("WScript.Shell")
strPCName = objShell.RegRead(regComputerName)
'-----------------------------------------------------------
strDirectory = "C:"
strFile = "\" & strPCName & ".csv"
' Create the File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Check that the strDirectory folder exists
If objFSO.FolderExists(strDirectory) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Else
Set objFolder = objFSO.CreateFolder(strDirectory)
'WScript.Echo "Just created " & strDirectory
End If
If objFSO.FileExists(strDirectory & strFile) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Else
Set objFile = objFSO.CreateTextFile(strDirectory & strFile)
'Wscript.Echo "Just created " & strDirectory & strFile
End If
set objFile = nothing
set objFolder = nothing
' ---------------------------------------------------------------'
' OpenTextFile Method needs a Const value
' ForAppending = 8 ForReading = 1, ForWriting = 2
Const ForAppending = 8
Const ForWriting = 2
Set objTextFile = objFSO.OpenTextFile _
(strDirectory & strFile, ForWriting, True)
' ---------------------------------------------------------------'
Set objNetwork = CreateObject("WScript.Network")
Set objPrinter = objNetwork.EnumPrinterConnections
For intDrive = 0 To (objPrinter.Count -1) Step 2
intNetLetter = IntNetLetter +1
strText = objPrinter.Item(intDrive) & "," & objPrinter.Item(intDrive +1) & "," & intDrive
objTextFile.WriteLine(strText)
Next
' ---------------------------------------------------------------'
objTextFile.Close
WScript.Quit