I need to delete some files from remote machines (eg: all files ending with error.doc). I have got a script for the same. But the problem is it will delete only the files I specify, like if I mention error.doc it will delete only that particular file, it will not delete newerror.doc. Can you please modify the following script for the same. Also I need this script has to pick the machine names one by one from a text file and perform the task
****************START OF SCRIPT**********************
Option Explicit
Dim strComputer, strFileName, strExtension, boolFileFound, strFileList, objWMIService, colFileList, objFile, boolPinged, objFSO
strComputer = InputBox("Please enter the computer IP or hostname that you want to look for a file on:", "Computer to search", "")
strFileName = InputBox("Please enter the file name (no extension) to search for:", "File name to search for", "")
strExtension = InputBox("Please enter the extension of the file name to search for:", "File name extension to search for", "")
boolPinged = Ping(strComputer)
If boolPinged = "True" Then
strFileList = "Files found and deleted on" & strComputer & vbCrLf
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFileList = objWMIService.ExecQuery _
("SELECT * FROM CIM_Datafile WHERE (Drive = 'c:' OR Drive = 'd:' OR Drive = 'e:') AND FileName = '" & strFileName & "' AND Extension = '" & strExtension & "'")
On Error Resume Next
For Each objFile In colFileList
If Err Then
MsgBox "No files were found."
WScript.Quit
End If
strFileList = strFileList & objFile.Name & vbCrLf
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.DeleteFile(objFile.Name)
Next
MsgBox strFileList
Else
MsgBox "Could not ping " & strComputer
End If
WScript.Quit
Function Ping(strComputer)
Dim objShell, boolCode
Set objShell = CreateObject("WScript.Shell")
boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
If boolCode = 0 Then
Ping = True
Else
Ping = False
End If
End Function
*************** END OF SCRIPT****************************
Bookmarks