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