Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / HTML, CSS, Scripts / MS VBScript / August 2008



Tip: Looking for answers? Try searching our database.

File Extension

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
freddy - 28 Aug 2008 14:46 GMT
I have a script that removes files with specific file extension and the
script works fine. Here is the script:

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

strFolderName = "C:\Test"

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

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

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

For Each objFile in colFiles
   If objFile.Extension = "txt" Or objFile.Extension = "doc" Then
       objTextFile.WriteLine objFile.FileName
       WScript.Echo objFile.FileName & objFile.Extension
   End If
Next

For Each objFolder in colSubfolders
   GetSubFolders strFolderName
Next

Sub GetSubFolders(strFolderName)

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

   For Each objFolder2 in colSubfolders2
       strFolderName = objFolder2.Name

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

   For Each objFile in colFiles
       If objFile.Extension = "txt" Or objFile.Extension = "doc" Then
           objTextFile.WriteLine strFolderName & "\" & objFile.FileName &
"." & objFile.Extension & " has been deleted on " & Date() & " " & Time
           WScript.Echo strFolderName & "\" & objFile.FileName & "." &
objFile.Extension & " has been deleted on " & Date() & " " & Time
           'will delete file
           objFile.delete
       End If
   Next

       GetSubFolders strFolderName
   Next
End Sub

One question in the following code
If objFile.Extension = "txt" Or objFile.Extension = "doc" Then ...
For each extension do I have to use objFile.Extension = "doc" or
objFile.Extension = "RW" or  objFile.Extension = "rwx" and so on.... - Is
there an easier way to just list the file extensions without repeating
objFile.Extension about 5 times

Thanks
James Whitlow - 28 Aug 2008 15:03 GMT
>I have a script that removes files with specific file extension and the
> script works fine. Here is the script:
[quoted text clipped - 5 lines]
> there an easier way to just list the file extensions without repeating
> objFile.Extension about 5 times

   You could use 'Select Case':

Select Case LCase(objFile.Extension)
Case "txt","doc","rw","rwx"
 'Do Something
Case Else
 'Do Something Else
End Select
Richard Mueller [MVP] - 28 Aug 2008 15:17 GMT
>>I have a script that removes files with specific file extension and the
>> script works fine. Here is the script:
[quoted text clipped - 14 lines]
>  'Do Something Else
> End Select

Or, to avoid repeatedly retrieving a property value I often assign the value
to a variable. For example

strExt = LCase(objFile.Extension)
If (strExt = "txt") Or (strExt = "doc") Then

In this situation, the Select Case is best (and it is easier to read).

Signature

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

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.