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 - File Extension

Reply
 
Old 08-28-2008   #1 (permalink)
freddy


 
 

File Extension

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

My System SpecsSystem Spec
Old 08-28-2008   #2 (permalink)
James Whitlow


 
 

Re: File Extension

"freddy" <freddy@xxxxxx> wrote in message
news:5E4C509E-4E56-44F7-9706-3BB19CA2DBBC@xxxxxx
Quote:

>I have a script that removes files with specific file extension and the
> script works fine. Here is the script:
>
> 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
You could use 'Select Case':

Select Case LCase(objFile.Extension)
Case "txt","doc","rw","rwx"
'Do Something
Case Else
'Do Something Else
End Select


My System SpecsSystem Spec
Old 08-28-2008   #3 (permalink)
Richard Mueller [MVP]


 
 

Re: File Extension


"James Whitlow" <jwhitlow.60372693@xxxxxx> wrote in message
news:%23i$vZbRCJHA.3484@xxxxxx
Quote:

> "freddy" <freddy@xxxxxx> wrote in message
> news:5E4C509E-4E56-44F7-9706-3BB19CA2DBBC@xxxxxx
Quote:

>>I have a script that removes files with specific file extension and the
>> script works fine. Here is the script:
>>
>> 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
>
> You could use 'Select Case':
>
> Select Case LCase(objFile.Extension)
> Case "txt","doc","rw","rwx"
> 'Do Something
> Case Else
> '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).

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


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
File Extension Software
File extension Vista General
.pps file extension Vista General
How do you change file name extension in File/Dir Explorer? Vista General
File Extension PowerShell


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