Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - Is the script ok or does it need improvement

Reply
 
Old 09-05-2008   #1 (permalink)
freddy


 
 

Is the script ok or does it need improvement

Here it is:

strComputer = "."
Dim strNetdrive, strNetpath
strNetdrive = "v:\"
'strNetpath =
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set objNetwork = CreateObject("Wscript.Network")
Set colItems = objWMIService.ExecQuery _
("Select * From Win32_LogicalDisk Where DeviceID" = strNetdrive )

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.CreateTextFile("C:\Scripts\Test.txt")

Set colSubfolders = objWMIService.ExecQuery _
("Associators of {Win32_Directory.Name='" & strNetdrive & "'} " _
& "Where AssocClass = Win32_Subdirectory " _
& "ResultRole = PartComponent")

Set colFiles = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='" & strNetdrive & "'} Where " _
& "ResultClass = CIM_DataFile")
For Each objFile In colFiles
If objFile.Extension = "RW" Or objFile.Extension = "txt" Or
objFile.Extension = "csv" Or objFile.Extension = "rwx" Then
objTextFile.WriteLine strNetdrive & "\" & objFile.FileName & "." &
objFile.Extension & " has been deleted on " & Date() & " " & Time
WScript.Echo strNetdrive & "\" & objFile.FileName & "." &
objFile.Extension & " has been deleted on " & Date() & " " & Time
End If
Next

For Each objFolder in colSubfolders
GetSubFolders strNetdrive
i = i + 1
Next
WScript.Echo "Total Files: " & i


Sub GetSubFolders(strNetdrive)

Set colSubfolders2 = objWMIService.ExecQuery _
("Associators of {Win32_Directory.Name='" & strNetdrive & "'} " _
& "Where AssocClass = Win32_Subdirectory " _
& "ResultRole = PartComponent")

For Each objFolder2 in colSubfolders2
strNetdrive = objFolder2.Name

Set colFiles = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='" & strNetdrive & "'} Where
" _
& "ResultClass = CIM_DataFile")

For Each objFile In colFiles
If objFile.Extension = "RW" Or objFile.Extension = "txt" Or
objFile.Extension = "csv" Or objFile.Extension = "rwx" Then
objTextFile.WriteLine strNetdrive & "\" & objFile.FileName & "."
& objFile.Extension & " has been deleted on " & Date() & " " & Time
WScript.Echo strNetdrive & "\" & objFile.FileName & "." &
objFile.Extension & " has been deleted on " & Date() & " " & Time
'will delete file

End If
Next

GetSubFolders strNetdrive

Next

End Sub

It runs fine but I think I have the WScript.Echo "Total Files: " & i in the
wrong place. I left out the objfile.delete, this is just a test run. Where it
says objFile.Extension over and over again can I do something different like
strEXten = "txt, rw, rxz"

My System SpecsSystem Spec
Old 09-05-2008   #2 (permalink)
Richard Mueller [MVP]


 
 

Re: Is the script ok or does it need improvement


"freddy" <freddy@xxxxxx> wrote in message
news:F6AE359E-CD7B-47CC-868E-3C42F98B5CD2@xxxxxx
Quote:

> Here it is:
>
> strComputer = "."
> Dim strNetdrive, strNetpath
> strNetdrive = "v:\"
> 'strNetpath =
> Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
> Set objNetwork = CreateObject("Wscript.Network")
> Set colItems = objWMIService.ExecQuery _
> ("Select * From Win32_LogicalDisk Where DeviceID" = strNetdrive )
>
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> Set objTextFile = objFSO.CreateTextFile("C:\Scripts\Test.txt")
>
> Set colSubfolders = objWMIService.ExecQuery _
> ("Associators of {Win32_Directory.Name='" & strNetdrive & "'} " _
> & "Where AssocClass = Win32_Subdirectory " _
> & "ResultRole = PartComponent")
>
> Set colFiles = objWMIService.ExecQuery _
> ("ASSOCIATORS OF {Win32_Directory.Name='" & strNetdrive & "'} Where " _
> & "ResultClass = CIM_DataFile")
> For Each objFile In colFiles
> If objFile.Extension = "RW" Or objFile.Extension = "txt" Or
> objFile.Extension = "csv" Or objFile.Extension = "rwx" Then
> objTextFile.WriteLine strNetdrive & "\" & objFile.FileName & "." &
> objFile.Extension & " has been deleted on " & Date() & " " & Time
> WScript.Echo strNetdrive & "\" & objFile.FileName & "." &
> objFile.Extension & " has been deleted on " & Date() & " " & Time
> End If
> Next
>
> For Each objFolder in colSubfolders
> GetSubFolders strNetdrive
> i = i + 1
> Next
> WScript.Echo "Total Files: " & i
>
>
> Sub GetSubFolders(strNetdrive)
>
> Set colSubfolders2 = objWMIService.ExecQuery _
> ("Associators of {Win32_Directory.Name='" & strNetdrive & "'} " _
> & "Where AssocClass = Win32_Subdirectory " _
> & "ResultRole = PartComponent")
>
> For Each objFolder2 in colSubfolders2
> strNetdrive = objFolder2.Name
>
> Set colFiles = objWMIService.ExecQuery _
> ("ASSOCIATORS OF {Win32_Directory.Name='" & strNetdrive & "'} Where
> " _
> & "ResultClass = CIM_DataFile")
>
> For Each objFile In colFiles
> If objFile.Extension = "RW" Or objFile.Extension = "txt" Or
> objFile.Extension = "csv" Or objFile.Extension = "rwx" Then
> objTextFile.WriteLine strNetdrive & "\" & objFile.FileName &
> "."
> & objFile.Extension & " has been deleted on " & Date() & " " & Time
> WScript.Echo strNetdrive & "\" & objFile.FileName & "." &
> objFile.Extension & " has been deleted on " & Date() & " " & Time
> 'will delete file
>
> End If
> Next
>
> GetSubFolders strNetdrive
>
> Next
>
> End Sub
>
> It runs fine but I think I have the WScript.Echo "Total Files: " & i in
> the
> wrong place. I left out the objfile.delete, this is just a test run. Where
> it
> says objFile.Extension over and over again can I do something different
> like
> strEXten = "txt, rw, rxz"
The variable objNetwork is not used. The variable colItems is also not used
and there is an error in the statement:

Set colItems = objWMIService.ExecQuery _
("Select * From Win32_LogicalDisk Where DeviceID" = strNetdrive )

It probably should be:

Set colItems = objWMIService.ExecQuery _
("Select * From Win32_LogicalDisk Where DeviceID='" & strNetdrive & "'")

except I don't think the trailing backslash in the value of strNetdrive is
allowed. No matter since colItems is not used.
I would suggest:

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate,authenticationLevel=Pkt}!\\" _
& strComputer & "\root\cimv2")

Instead of:

Date() & " " & Time()

I would use:

Now()

I don't believe there is any better way to handle the file extensions. Your
variable i counts folders, not files, and only at the parent level. If you
want to count files the variable should be incremented in the For Each loop
that enumerates colFiles. Since the Sub is recursive, the variable must have
global scope. Finally, I would suggest using "Option Explicit" and declaring
all variables. Some variables are used in both the main program and
subroutine, which I believe is OK, but it would help to make the scope clear
with Dim statements.

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--


My System SpecsSystem Spec
Old 09-09-2008   #3 (permalink)
freddy


 
 

Re: Is the script ok or does it need improvement


I tried moving the i=i+1 but nothing is happening it just echos out: file =
Where do I put the i=i+1. Here is where I put it

Sub GetSubFolders(strNetdrive)

Set colSubfolders2 = objWMIService.ExecQuery _
("Associators of {Win32_Directory.Name='" & strNetdrive & "'} " _
& "Where AssocClass = Win32_Subdirectory " _
& "ResultRole = PartComponent")

For Each objFolder2 in colSubfolders2
strNetdrive = objFolder2.Name

Set colFiles = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='" & strNetdrive & "'} Where
" _
& "ResultClass = CIM_DataFile")

For Each objFile In colFiles

If objFile.Extension = "RW" Or objFile.Extension = "PRN" Or
objFile.Extension = "csv" Or objFile.Extension = "rwx" Or objFile.Extension =
"RPT" Then
objTextFile.WriteLine strNetdrive & "\" & objFile.FileName & "."
& objFile.Extension & " has been deleted on " & Now ()
WScript.Echo strNetdrive & "\" & objFile.FileName & "." &
objFile.Extension & " has been deleted on " &Now ()
'will delete file

End If
i = i + 1
Next
GetSubFolders strNetdrive
Next
End Sub

Thanks



"Richard Mueller [MVP]" wrote:
Quote:

>
> "freddy" <freddy@xxxxxx> wrote in message
> news:F6AE359E-CD7B-47CC-868E-3C42F98B5CD2@xxxxxx
Quote:

> > Here it is:
> >
> > strComputer = "."
> > Dim strNetdrive, strNetpath
> > strNetdrive = "v:\"
> > 'strNetpath =
> > Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
> > Set objNetwork = CreateObject("Wscript.Network")
> > Set colItems = objWMIService.ExecQuery _
> > ("Select * From Win32_LogicalDisk Where DeviceID" = strNetdrive )
> >
> > Set objFSO = CreateObject("Scripting.FileSystemObject")
> > Set objTextFile = objFSO.CreateTextFile("C:\Scripts\Test.txt")
> >
> > Set colSubfolders = objWMIService.ExecQuery _
> > ("Associators of {Win32_Directory.Name='" & strNetdrive & "'} " _
> > & "Where AssocClass = Win32_Subdirectory " _
> > & "ResultRole = PartComponent")
> >
> > Set colFiles = objWMIService.ExecQuery _
> > ("ASSOCIATORS OF {Win32_Directory.Name='" & strNetdrive & "'} Where " _
> > & "ResultClass = CIM_DataFile")
> > For Each objFile In colFiles
> > If objFile.Extension = "RW" Or objFile.Extension = "txt" Or
> > objFile.Extension = "csv" Or objFile.Extension = "rwx" Then
> > objTextFile.WriteLine strNetdrive & "\" & objFile.FileName & "." &
> > objFile.Extension & " has been deleted on " & Date() & " " & Time
> > WScript.Echo strNetdrive & "\" & objFile.FileName & "." &
> > objFile.Extension & " has been deleted on " & Date() & " " & Time
> > End If
> > Next
> >
> > For Each objFolder in colSubfolders
> > GetSubFolders strNetdrive
> > i = i + 1
> > Next
> > WScript.Echo "Total Files: " & i
> >
> >
> > Sub GetSubFolders(strNetdrive)
> >
> > Set colSubfolders2 = objWMIService.ExecQuery _
> > ("Associators of {Win32_Directory.Name='" & strNetdrive & "'} " _
> > & "Where AssocClass = Win32_Subdirectory " _
> > & "ResultRole = PartComponent")
> >
> > For Each objFolder2 in colSubfolders2
> > strNetdrive = objFolder2.Name
> >
> > Set colFiles = objWMIService.ExecQuery _
> > ("ASSOCIATORS OF {Win32_Directory.Name='" & strNetdrive & "'} Where
> > " _
> > & "ResultClass = CIM_DataFile")
> >
> > For Each objFile In colFiles
> > If objFile.Extension = "RW" Or objFile.Extension = "txt" Or
> > objFile.Extension = "csv" Or objFile.Extension = "rwx" Then
> > objTextFile.WriteLine strNetdrive & "\" & objFile.FileName &
> > "."
> > & objFile.Extension & " has been deleted on " & Date() & " " & Time
> > WScript.Echo strNetdrive & "\" & objFile.FileName & "." &
> > objFile.Extension & " has been deleted on " & Date() & " " & Time
> > 'will delete file
> >
> > End If
> > Next
> >
> > GetSubFolders strNetdrive
> >
> > Next
> >
> > End Sub
> >
> > It runs fine but I think I have the WScript.Echo "Total Files: " & i in
> > the
> > wrong place. I left out the objfile.delete, this is just a test run. Where
> > it
> > says objFile.Extension over and over again can I do something different
> > like
> > strEXten = "txt, rw, rxz"
>
> The variable objNetwork is not used. The variable colItems is also not used
> and there is an error in the statement:
>
> Set colItems = objWMIService.ExecQuery _
> ("Select * From Win32_LogicalDisk Where DeviceID" = strNetdrive )
>
> It probably should be:
>
> Set colItems = objWMIService.ExecQuery _
> ("Select * From Win32_LogicalDisk Where DeviceID='" & strNetdrive & "'")
>
> except I don't think the trailing backslash in the value of strNetdrive is
> allowed. No matter since colItems is not used.
> I would suggest:
>
> Set objWMIService = GetObject("winmgmts:" _
> & "{impersonationLevel=impersonate,authenticationLevel=Pkt}!\\" _
> & strComputer & "\root\cimv2")
>
> Instead of:
>
> Date() & " " & Time()
>
> I would use:
>
> Now()
>
> I don't believe there is any better way to handle the file extensions. Your
> variable i counts folders, not files, and only at the parent level. If you
> want to count files the variable should be incremented in the For Each loop
> that enumerates colFiles. Since the Sub is recursive, the variable must have
> global scope. Finally, I would suggest using "Option Explicit" and declaring
> all variables. Some variables are used in both the main program and
> subroutine, which I believe is OK, but it would help to make the scope clear
> with Dim statements.
>
> --
> Richard Mueller
> MVP Directory Services
> Hilltop Lab - http://www.rlmueller.net
> --
>
>
>
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
WLM an improvement? Live Mail
Possible UAC Improvement Vista security
Contacts in need of improvement Vista mail
2GB to 4GB, how much of an improvement? Vista hardware & devices
No Improvement in RC1 since Beta 2 Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46