Solved

Modify VBS Script to Write CVS file to a Share

Posted on 2009-04-08
5
295 Views
Last Modified: 2013-11-25
Attention Experts,

I have a .vbs file that, when executed,  generates a .csv file on the same drive/folder from which the the .vbs file was ran.
Example: The .vbs file is on the desktop. I execute the .vbs file, and a .csv file appears on the desktop.

Now, I need the outputted .csv file to be written to a network share, when the .vbs file is executed.
Example: The .vbs file is on the desktop. I execute the .vbs file, and a .csv file appears on a specified shared folder <that the user has 'write' access to, of course>.

Thanks in advance!



Set oNet   = CreateObject("Wscript.Network")
set oFSO   = CreateObject("Scripting.FileSystemObject")
strComputer = "."
UserName = oNet.UserName
Set objOutputFile = oFSO.CreateTextFile(Username+".csv",True)
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colDrives = objWMIService.ExecQuery _
    ("Select * From Win32_LogicalDisk Where DriveType = 4")
 
 
'************
'get username and create row 1 with username
'************
   objOutputFile.WriteLine(Username & ",")
   'wscript.echo(UserName)
 
'************
'get drive letter and create row 2 with Drive letters
'************
For Each objDrive in colDrives
    'Wscript.Echo "Drive letter: " & objDrive.DeviceID
    objOutputFile.Write(objDrive.DeviceID & ",")
Next
 
'***********
'End Row 2
'***********
objOutputFile.WriteLine(" ")
 
'***********
'Get UNC and create column 3 under the corresponding drive letter
'***********
 
For Each objDrive in colDrives
    'Wscript.Echo "Network path: " & objDrive.ProviderName
    objOutputFile.Write(objDrive.ProviderName & ",")
Next

Open in new window

0
Comment
Question by:paydfody
  • 3
  • 2
5 Comments
 
LVL 15

Accepted Solution

by:
oobayly earned 250 total points
ID: 24099429
As long as the user executing the script has permission to access the share, I don't see why you need to win management code.

Surely you just have to write the csv file a path like this, assuming you're using the FileSystemObject & a TextStream
Dim filesys, testfile
Dim outputFile
outputFile = "\\remote_computer_name\shared_folder\something.csv"
Set filesys = CreateObject("Scripting.FileSystemObject")
Set testfile= filesys.CreateTextFile(outputFile , True)
 
' generate csv data
 
testfile.Close 

Open in new window

0
 

Author Comment

by:paydfody
ID: 24100053
oobayly,

That did not work. I'm not even sure how to add your mod to my code. Thanks, anyway.
0
 
LVL 15

Expert Comment

by:oobayly
ID: 24100457
Well, what does your code look like. It has to have somewhere the name of the file that is created. The fact that the file is created on the Desktop when you execute the script on the Desktop implies that the file name the csv file is given something like "data.csv", all you should have to do is change it an absolute path, ie: "\\remote_computer_name\shared_folder\data.csv"
0
 

Author Comment

by:paydfody
ID: 24100522
My original code is posted, in the 'code snippet' section, directly under the question. I have been tweaking around with it, but I still haven't gotten it to write exactly where I want it. Anyways, the code is below (thanks):


Set oNet   = CreateObject("Wscript.Network")
set oFSO   = CreateObject("Scripting.FileSystemObject")
strComputer = "."
UserName = oNet.UserName
Set objOutputFile = oFSO.CreateTextFile(Username+".csv",True)
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colDrives = objWMIService.ExecQuery _
    ("Select * From Win32_LogicalDisk Where DriveType = 4")
 
 
'************
'get username and create row 1 with username
'************
   objOutputFile.WriteLine(Username & ",")
   'wscript.echo(UserName)
 
'************
'get drive letter and create row 2 with Drive letters
'************
For Each objDrive in colDrives
    'Wscript.Echo "Drive letter: " & objDrive.DeviceID
    objOutputFile.Write(objDrive.DeviceID & ",")
Next
 
'***********
'End Row 2
'***********
objOutputFile.WriteLine(" ")
 
'***********
'Get UNC and create column 3 under the corresponding drive letter
'***********
 
For Each objDrive in colDrives
    'Wscript.Echo "Network path: " & objDrive.ProviderName
    objOutputFile.Write(objDrive.ProviderName & ",")
Next
0
 

Author Comment

by:paydfody
ID: 24100706
oobayly,

Good news. I've finally tweaked this thing like i need it. I appreciate the your thought-provoking ideas. Therefore, I am awarding you the 250 points.

Thanks again.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Quality of Service (QoS) options are nearly endless when it comes to networks today. This article is merely one example of how it can be handled in a hub-n-spoke design using a 3-tier configuration.
As companies replace their old PBX phone systems with Unified IP Communications, many are finding out that legacy applications such as fax do not work well with VoIP. Fortunately, Cloud Faxing provides a cost-effective alternative that works over an…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question