amaru96
asked on
VB script to display mapped drives and printers (output to username.txt)
Hi guys, I have the below code (which I stole from here) but need a little help in making a small change.
Instead of the output going to a fixed filename I need it to create it based on the user's username.
In DOS it would be equivalent to "c:\folder\%username%.txt" but have no idea how to do this in VB.
Instead of the output going to a fixed filename I need it to create it based on the user's username.
In DOS it would be equivalent to "c:\folder\%username%.txt"
Option Explicit
Const cForWriting = 2
Const cOutFile = "C:\folder\filename.txt"
Dim objFSO, objNetwork, colDrives, intDrive, intNetLetter
Dim objWMIService, objItem, colItems, strComputer, intPrinters
dim objOutFile
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutFile = objFSO.OpenTextFile(cOutFile, cForWriting, True)
Set objNetwork = CreateObject("WScript.Network")
Set colDrives = objNetwork.EnumNetworkDrives
If colDrives.Count = 0 Then
WScript.Echo "Guy's warning: No Drives Mapped "
Wscript.Quit
End If
objOutFile.WriteLine("Network Drives:" & vbCrLf)
For intDrive = 0 To (colDrives.Count -1) Step 2
intNetLetter = IntNetLetter +1
objOutFile.WriteLine("UNC Path " & intDrive & " - " & colDrives.Item(intDrive) & " Mapped drive No : " & colDrives.Item(intDrive +1))
Next
objOutFile.WriteLine("")
strComputer ="."
intPrinters = 1
' --------------------------------------------
' Pure WMI Section
Set objWMIService = GetObject ("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery ("SELECT * FROM Win32_Printer")
Call Wait() ' Goto Sub Routine at the end
' On Error Resume Next
For Each objItem In colItems
objOutFile.WriteLine("Printers on " _
& objItem.name & ", Printer Number: " & intPrinters & VbCrLf & _
"====================================" & VbCrLf & _
"Availability: " & objItem.Availability & VbCrLf & _
"Description: " & objItem.Description & VbCrLf & _
"Printer: " & objItem.DeviceID & VbCrLf & _
"Driver Name: " & objItem.DriverName & VbCrLf & _
"Port Name: " & objItem.PortName & VbCrLf & _
"Printer State: " & objItem.PrinterState & VbCrLf & _
"Printer Status: " & objItem.PrinterStatus & VbCrLf & _
"PrintJobDataType: " & objItem.PrintJobDataType & VbCrLf & _
"Print Processor: " & objItem.PrintProcessor & VbCrLf & _
"Spool Enabled: " & objItem.SpoolEnabled & VbCrLf & _
"Separator File: " & objItem.SeparatorFile & VbCrLf & _
"Queued: " & objItem.Queued & VbCrLf & _
"Status: " & objItem.Status & VbCrLf & _
"StatusInfo: " & objItem.StatusInfo & VbCrLf & _
"Published: " & objItem.Published & VbCrLf & _
"Shared: " & objItem.Shared & VbCrLf & _
"ShareName: " & objItem.ShareName & VbCrLf & _
"Direct: " & objItem.Direct & VbCrLf & _
"Location: " & objItem.Location & VbCrLf & _
"Priority: " & objItem.Priority & VbCrLf & _
"Work Offline: " & objItem.WorkOffline & VbCrLf & _
"Horizontal Res: " & objItem.HorizontalResolution & VbCrLf & _
"Vertical Res: " & objItem.VerticalResolution & VbCrLf & _
"")
intPrinters = intPrinters + 1
Next
objOutFile.Close
WScript.Quit
sub Wait()
If strComputer = "." then
strComputer = "Local Host"
else
strComputer = strComputer
end if
' WScript.Echo "Wait 2 mins for " & strComputer & " to enumerate printers"
End Sub
ASKER
Hi sshah, I entered that code as below but am getting an error when I run it.
Option Explicit
Set wshShell = CreateObject( "WScript.Shell" )
str_username wshShell.ExpandEnvironment Strings( "%USERNAME%" )
wshShell = Nothing
Const cForWriting = 2
Const cOutFile = "C:\temp\" & str_username & ".txt"
Dim objFSO, objNetwork, colDrives, intDrive, intNetLetter
Dim objWMIService, objItem, colItems, strComputer, intPrinters
dim objOutFile
vberror.jpg
Option Explicit
Set wshShell = CreateObject( "WScript.Shell" )
str_username wshShell.ExpandEnvironment
wshShell = Nothing
Const cForWriting = 2
Const cOutFile = "C:\temp\" & str_username & ".txt"
Dim objFSO, objNetwork, colDrives, intDrive, intNetLetter
Dim objWMIService, objItem, colItems, strComputer, intPrinters
dim objOutFile
vberror.jpg
Change
Const cOutFile = "C:\temp\" & str_username & ".txt"
to
cOutFile = "C:\temp\" & str_username & ".txt"
~bp
Const cOutFile = "C:\temp\" & str_username & ".txt"
to
cOutFile = "C:\temp\" & str_username & ".txt"
~bp
ASKER
Am now getting:
Line: 3
Char: 1
Error: Variable is undefined: 'wshShell'
Code: 800A01F4
Source: Microsoft VBScript runtime error
Line: 3
Char: 1
Error: Variable is undefined: 'wshShell'
Code: 800A01F4
Source: Microsoft VBScript runtime error
You will need to DIM all variables before they are used, since you have OPTION EXPLICIT. So you need at least this line, but there may be more.
Dim wshShell, str_username, cOutFile
~bp
Dim wshShell, str_username, cOutFile
~bp
ASKER
Am getting another error now.
Line: 6
Char: 1
Error: Type mismatch
Code: 800A000D
Below is the first 20 lines of my code:
Option Explicit
Dim wshShell, str_username, cOutFile
Set wshShell = CreateObject( "WScript.Shell" )
str_username wshShell.ExpandEnvironment Strings( "%USERNAME%" )
wshShell = Nothing
Const cForWriting = 2
cOutFile = "C:\temp\" & str_username & ".txt"
Dim objFSO, objNetwork, colDrives, intDrive, intNetLetter
Dim objWMIService, objItem, colItems, strComputer, intPrinters
dim objOutFile
Set objFSO = CreateObject("Scripting.Fi leSystemOb ject")
Set objOutFile = objFSO.OpenTextFile(cOutFi le, cForWriting, True)
Set objNetwork = CreateObject("WScript.Netw ork")
Set colDrives = objNetwork.EnumNetworkDriv es
Line: 6
Char: 1
Error: Type mismatch
Code: 800A000D
Below is the first 20 lines of my code:
Option Explicit
Dim wshShell, str_username, cOutFile
Set wshShell = CreateObject( "WScript.Shell" )
str_username wshShell.ExpandEnvironment
wshShell = Nothing
Const cForWriting = 2
cOutFile = "C:\temp\" & str_username & ".txt"
Dim objFSO, objNetwork, colDrives, intDrive, intNetLetter
Dim objWMIService, objItem, colItems, strComputer, intPrinters
dim objOutFile
Set objFSO = CreateObject("Scripting.Fi
Set objOutFile = objFSO.OpenTextFile(cOutFi
Set objNetwork = CreateObject("WScript.Netw
Set colDrives = objNetwork.EnumNetworkDriv
Looks like an equals sign is missing.
str_username = wshShell.ExpandEnvironment Strings( "%USERNAME%" )
~bp
str_username = wshShell.ExpandEnvironment
~bp
ASKER
Error
Line: 7
Char: 1
Error: Object variable not set
Line 7 is: wshShell = Nothing
Appreciate your help so far.
Line: 7
Char: 1
Error: Object variable not set
Line 7 is: wshShell = Nothing
Appreciate your help so far.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Fantastic, works great. Thanks!!
Welcome.
~bp
~bp
Set wshShell = CreateObject( "WScript.Shell" )
str_username wshShell.ExpandEnvironment
wshShell = Nothing
then use that username to open the appropriate file
Const cOutFile = "C:\folder\" & str_username & ".txt"
The rest of the things should remain the same.
Sandip