Hello,
I have a script that will ask you to enter in a file extension then check to see what file is associated with it. It will then write the information found to a text file on the local C Drive. I was asked to run this script against every system in my production network. All systems are listed in a large text file. How can i modify this script to read each system from the Workstation Text file, write the data to my output text file and then read the next workstation until all workstations have been examined. Below is the Script i am using...
On Error Resume Next
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Dim objshell,objfso,fso,objtextfile,f,I,strcomputer,strnextline,objdirectory
Set objfso = CreateObject("Scripting.FileSystemObject")
Set objwmiservice = GetObject("winmgmts:\\" & strcomputer & "\root\CIMV2")
Set f=objfspenTextFile("C:\RunWith.txt",ForAppending,True)
Set fso=CreateObject("Scripting.FileSystemObject")
Set objshell = WScript.CreateObject ("WScript.Shell")
Set objtextfile = objFSO.OpenTextFile("C:\workstations.txt", ForReading)
'Set F=objfspenTextFile("\\KASAPP\users\reports\DIMMInfo012709.txt",ForAppending,True)
strcomputer=objshell.RegRead("HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\ComputerName\ComputerName\ComputerName")
'--ClsReg.vbs
'--VBScript class module.
private Const AP = "HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths\"
Private Const CV = "HKLM\Software\Microsoft\Windows\CurrentVersion\"
Private const CK = "\Shell\Open\Command\"
private const CK1 = "\Shell\Opennew\Command\"
Class ClsExtExe
'--------------------------------------------------------------------------------
'--function to find default program for opening a given filetype.(this function does not confirm the EXE path
'--because some paths are in DOS format and a fso.FileExists call will return false on
'--such a path. )
Public Function GetDefaultProgram(sext)
Dim sh, s, s1, s2, spc
Set sh = CreateObject("WScript.Shell")
if left(sext, 1) <> "." Then
sext = "." & sext
end If
on error resume Next
s = sh.RegRead("HKCR\" & sext & "\") '--look up ext in HKCR.
if err.number <> 0 Then
GetDefaultProgram = ""
exit Function
end If
if s <> "" then '--if there's a listing with filetype name look that up under:
s1 = sh.RegRead("HKCR\" & s & CK) '--Shell\open\command or.....
if err.number = 0 and s1 <> "" then
s2 = Stripit(s1)
GetDefaultProgram = s2
exit Function
end If
err.clear
s1 = sh.RegRead("HKCR\" & s & CK1) '--shell\opennew\command.
if err.number = 0 And s1 <> "" then
s2 = Stripit(s1)
GetDefaultProgram = s2
exit Function
end If
else '--if no filetype name is listed look for shell\open\command under the ext. key.
err.clear
s1 = sh.RegRead("HKCR\" & sext & CK)
if err.number = 0 And s1 <> "" then
s2 = Stripit(s1)
GetDefaultProgram = s2
exit Function
end If
end If
'--if none of these checks have found anything return "".
GetDefaultProgram = ""
set sh = Nothing
End Function
'-------------------------------------------------------------------------
'-----------function to find full path of a given , registered EXE.
Public Function GetEXEPath(spath)
dim s, sh
Set sh = CreateObject("WScript.Shell")
err.clear
on error resume Next
s = sh.RegRead(AP & spath & "\")
if err.number = 0 Then
s = stripit(s)
GetEXEPath = s
Else
GetEXEPath = ""
end If
set sh = Nothing
End Function
'-----------------------------------------------------------------------------
'----------------------------------------------------------------------
'--clean up default program string.
Private Function Stripit(sp)
dim ept, sf
if sp = "" Then
Stripit = ""
exit Function
end If
ept = instr(1, sp, "exe", 1) '--find end of exe path.
if ept <> 0 Then
sf = left(sp, ept + 2)
Else
ept = instr(1, sp, "com", 1)
if ept <> 0 Then
sf = left(sp, ept + 2)
end If
end If
if left(sf, 1) = chr(34) Then '--take off any quotes or spaces.
sf = right(sf, (len(sf) - 1))
end If
sf = trim(sf)
Stripit = sf
End Function
End Class
'--ExtDefault.vbs - demonstrates the use of ClsExtExe VBScript class file.
'------------------------------------------------------------------------
Dim sn, cls, arg, ext
'--call sub to get access to the class:
AddClass "C:\xy\clsDemo.vbs"
'--set class object variable:
Set cls = New ClsExtExe
Set fso = CreateObject("Scripting.FileSystemObject")
'--get the default program for opening a file of given extension:
on error resume Next
'--provide option to enter extension or drop a file on script:
If wscript.arguments.count = 0 then
ext = inputbox("Enter file extension to get path of associated program.", "Find Default Program for Filetypes")
Else
arg = wscript.arguments.item(0)
ext = fso.GetExtensionName(arg)
end If
'--in case Cancel was clicked in input box.
if ext = "" then
wscript.Quit
end If
'--call function from class:
sn = cls.GetDefaultProgram(ext)
if sn = "" Then
msgbox "No listing found.", 0, "No default program found"
Else
msgbox sn, 0, "The default program for " & ext & " files:"
F.WriteLine "Workstation Number " & strcomputer &". The default program for file extension " & ext & " is: " &sn
end If
'-----------------------------------------------------------------------------
'--sub AddClass: reads the class vbs file into memory.
'--ExecuteGlobal makes the class code available to the current script.
'--this sub will be necessary in any script that uses the class.
Sub AddClass(spath)
Dim fso, s, f
Set fso = CreateObject("Scripting.FileSystemObject")
if fso.FileExists(spath) = False then
exit Sub
end If
set f = fspenTextFile(spath, 1, false)
s = f.ReadAll
f.Close
set f = Nothing
ExecuteGlobal s
End Sub
I have a script that will ask you to enter in a file extension then check to see what file is associated with it. It will then write the information found to a text file on the local C Drive. I was asked to run this script against every system in my production network. All systems are listed in a large text file. How can i modify this script to read each system from the Workstation Text file, write the data to my output text file and then read the next workstation until all workstations have been examined. Below is the Script i am using...
On Error Resume Next
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Dim objshell,objfso,fso,objtextfile,f,I,strcomputer,strnextline,objdirectory
Set objfso = CreateObject("Scripting.FileSystemObject")
Set objwmiservice = GetObject("winmgmts:\\" & strcomputer & "\root\CIMV2")
Set f=objfspenTextFile("C:\RunWith.txt",ForAppending,True)
Set fso=CreateObject("Scripting.FileSystemObject")
Set objshell = WScript.CreateObject ("WScript.Shell")
Set objtextfile = objFSO.OpenTextFile("C:\workstations.txt", ForReading)
'Set F=objfspenTextFile("\\KASAPP\users\reports\DIMMInfo012709.txt",ForAppending,True)
strcomputer=objshell.RegRead("HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\ComputerName\ComputerName\ComputerName")
'--ClsReg.vbs
'--VBScript class module.
private Const AP = "HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths\"
Private Const CV = "HKLM\Software\Microsoft\Windows\CurrentVersion\"
Private const CK = "\Shell\Open\Command\"
private const CK1 = "\Shell\Opennew\Command\"
Class ClsExtExe
'--------------------------------------------------------------------------------
'--function to find default program for opening a given filetype.(this function does not confirm the EXE path
'--because some paths are in DOS format and a fso.FileExists call will return false on
'--such a path. )
Public Function GetDefaultProgram(sext)
Dim sh, s, s1, s2, spc
Set sh = CreateObject("WScript.Shell")
if left(sext, 1) <> "." Then
sext = "." & sext
end If
on error resume Next
s = sh.RegRead("HKCR\" & sext & "\") '--look up ext in HKCR.
if err.number <> 0 Then
GetDefaultProgram = ""
exit Function
end If
if s <> "" then '--if there's a listing with filetype name look that up under:
s1 = sh.RegRead("HKCR\" & s & CK) '--Shell\open\command or.....
if err.number = 0 and s1 <> "" then
s2 = Stripit(s1)
GetDefaultProgram = s2
exit Function
end If
err.clear
s1 = sh.RegRead("HKCR\" & s & CK1) '--shell\opennew\command.
if err.number = 0 And s1 <> "" then
s2 = Stripit(s1)
GetDefaultProgram = s2
exit Function
end If
else '--if no filetype name is listed look for shell\open\command under the ext. key.
err.clear
s1 = sh.RegRead("HKCR\" & sext & CK)
if err.number = 0 And s1 <> "" then
s2 = Stripit(s1)
GetDefaultProgram = s2
exit Function
end If
end If
'--if none of these checks have found anything return "".
GetDefaultProgram = ""
set sh = Nothing
End Function
'-------------------------------------------------------------------------
'-----------function to find full path of a given , registered EXE.
Public Function GetEXEPath(spath)
dim s, sh
Set sh = CreateObject("WScript.Shell")
err.clear
on error resume Next
s = sh.RegRead(AP & spath & "\")
if err.number = 0 Then
s = stripit(s)
GetEXEPath = s
Else
GetEXEPath = ""
end If
set sh = Nothing
End Function
'-----------------------------------------------------------------------------
'----------------------------------------------------------------------
'--clean up default program string.
Private Function Stripit(sp)
dim ept, sf
if sp = "" Then
Stripit = ""
exit Function
end If
ept = instr(1, sp, "exe", 1) '--find end of exe path.
if ept <> 0 Then
sf = left(sp, ept + 2)
Else
ept = instr(1, sp, "com", 1)
if ept <> 0 Then
sf = left(sp, ept + 2)
end If
end If
if left(sf, 1) = chr(34) Then '--take off any quotes or spaces.
sf = right(sf, (len(sf) - 1))
end If
sf = trim(sf)
Stripit = sf
End Function
End Class
'--ExtDefault.vbs - demonstrates the use of ClsExtExe VBScript class file.
'------------------------------------------------------------------------
Dim sn, cls, arg, ext
'--call sub to get access to the class:
AddClass "C:\xy\clsDemo.vbs"
'--set class object variable:
Set cls = New ClsExtExe
Set fso = CreateObject("Scripting.FileSystemObject")
'--get the default program for opening a file of given extension:
on error resume Next
'--provide option to enter extension or drop a file on script:
If wscript.arguments.count = 0 then
ext = inputbox("Enter file extension to get path of associated program.", "Find Default Program for Filetypes")
Else
arg = wscript.arguments.item(0)
ext = fso.GetExtensionName(arg)
end If
'--in case Cancel was clicked in input box.
if ext = "" then
wscript.Quit
end If
'--call function from class:
sn = cls.GetDefaultProgram(ext)
if sn = "" Then
msgbox "No listing found.", 0, "No default program found"
Else
msgbox sn, 0, "The default program for " & ext & " files:"
F.WriteLine "Workstation Number " & strcomputer &". The default program for file extension " & ext & " is: " &sn
end If
'-----------------------------------------------------------------------------
'--sub AddClass: reads the class vbs file into memory.
'--ExecuteGlobal makes the class code available to the current script.
'--this sub will be necessary in any script that uses the class.
Sub AddClass(spath)
Dim fso, s, f
Set fso = CreateObject("Scripting.FileSystemObject")
if fso.FileExists(spath) = False then
exit Sub
end If
set f = fspenTextFile(spath, 1, false)
s = f.ReadAll
f.Close
set f = Nothing
ExecuteGlobal s
End Sub