Link to home
Start Free TrialLog in
Avatar of bsharath
bsharathFlag for India

asked on

Need to delete files from all machines in the file.

Hi,

I have Sophos Antivirus which detect Malware and unwanted files but i have set it not to delete any as i first need to vaalidate then delete it manually.
So i want a way that a script can delete any file in the txt file and all machines whose names are in the txt file.

I will have the files in this format
E:
C:\windows\system32\filename

So the script has to take the machine names from the txt file and search for any available path or file if there then delete the file.
No matter with what application it is shared with.Needs to kill the process or stop service and delete the file.
Any help..

Regards
SHarath
Avatar of bsharath
bsharath
Flag of India image

ASKER

I get this.
DEV-CHEN-PC1703: ERROR deleting D:\program files\stickypad\stickypad.exe : 70 -
Permission denied
C:\program files\quicktime alternative\qttask.exe does not exist on DEV-CHEN-PC1
703
D:\program files\stickypad\stickypad.exe does not exist on DEV-CHEN-PC378
DEV-CHEN-PC378: ERROR deleting C:\program files\quicktime alternative\qttask.exe
 : 70 - Permission denied
Avatar of RobSampson
Sharath, if you're running this code:

'===========
Set objShell = CreateObject("Wscript.Shell")
If LCase(Right(Wscript.FullName, 11)) = "wscript.exe" Then
      strPath = Wscript.ScriptFullName
      strCommand = "%comspec% /k cscript  """ & strPath & """"
      objShell.Run(strCommand), 1, True
      Wscript.Quit
End If

Set objFSO = CreateObject("Scripting.FileSystemObject")

strComputersFile = "Computers.txt"
strFileNamesFile = "FilesToDelete.txt"
' Please specify the full path to PSList.exe here
strPSListPath = "n:\utilities\pstools\pslist.exe"
strPSListPath = objFSO.getfile(strPSListPath).ShortPath

Const intForReading = 1
Set objInputFile = objFSO.OpenTextFile(strComputersFile, intForReading, False)

strLog = "Deleting files from remote computers"

While Not objInputFile.AtEndOfStream
      strUserComputer = objInputFile.ReadLine

      If Ping(strUserComputer) = True Then
            Set objFileNamesFile = objFSO.OpenTextFile(strFileNamesFile, intForReading, False)
            While Not objFileNamesFile.AtEndOfStream
                  'strFileToDelete = "C:\program files\Nokia\nokia pc suite 6\PcSync2.exe"
                  strFileToDelete = objFileNamesFile.ReadLine
                  If objFSO.FileExists("\\" & strUserComputer & "\" & Replace(strFileToDelete, ":", "$")) = True Then
                        strProcessName = Mid(strFileToDelete, InStrRev(strFileToDelete, "\") + 1)
                        If InStr(strProcessName, ".") > 0 Then strProcessName = Left(strProcessName, InStrRev(strProcessName, ".") - 1)
                        'PSList gives the following return values
                        ' 1 : process does not exist
                        ' 0 : successful - process found
                        strResponse = objShell.Run(strPSListPath & " -accepteula -e \\" & strComputer & " " & strProcessName, 0, True)
                        While strResponse = 0
                              WScript.Echo strProcessName & " is running."
                              strCommand = "TASKKILL /S " & strUserComputer &  " /F /IM " & Mid(strFileToDelete, InStrRev(strFileToDelete, "\") + 1) & " /T"
                              objShell.Run strCommand, 0, True
                              strResponse = objShell.Run(strPSListPath & " -accepteula -e \\" & strComputer & " " & strProcessName, 1, True)
                        Wend
                        'WScript.Echo "Attempting to delete " & "\\" & strUserComputer & "\" & Replace(strFileToDelete, ":", "$")
                        On Error Resume Next
                        objFSO.DeleteFile "\\" & strUserComputer & "\" & Replace(strFileToDelete, ":", "$"), True
                        If Err.Number <> 0 Then
                              WScript.Echo strUserComputer & ": ERROR deleting " & strFileToDelete & " : " & Err.Number & " - " & Err.Description
                              strLog = strLog & VbCrLf & strUserComputer & ": ERROR deleting " & strFileToDelete & " : " & Err.Number & " - " & Err.Description
                              Err.Clear
                              On Error GoTo 0
                        Else
                              Err.Clear
                              On Error GoTo 0
                              WScript.Echo strUserComputer & ": Deleted " & strFileToDelete
                              strLog = strLog & VbCrLf & strUserComputer & ": Deleted " & strFileToDelete
                        End If
                  Else
                        WScript.Echo strFileToDelete & " does not exist on " & strUserComputer
                        strLog = strLog & VbCrLf & strFileToDelete & " does not exist on " & strUserComputer
                  End If
            Wend
            objFileNamesFile.Close
            Set objFileNamesFile = Nothing
      Else
            WScript.Echo "Could not ping " & strUserComputer
            strLog = strLog & VbCrLf & "Could not ping " & strUserComputer
      End If
Wend
objInputFile.Close
Set objInputFile = Nothing

Set objOutputFile = objFSO.CreateTextFile("Delete_File_Results.txt", True)
objOutputFile.Write strLog
objOutputFile.Close
Set objOutputFile = Nothing

WScript.Echo ""
WScript.Echo "Done"
MsgBox "Done"

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

and you're not seeing
"<processnmae>  is running."

in your DOS prompt, then there will be another process holding onto the file.  I'll have a look at a few tools and see if I can find out what has a file open....

Regards,

Rob.
Thats the only message that i get
Just a reminder ....
Sharath, please try this....I haven't tested it yet.

'===========
Set objShell = CreateObject("Wscript.Shell")
If LCase(Right(Wscript.FullName, 11)) = "wscript.exe" Then
      strPath = Wscript.ScriptFullName
      strCommand = "%comspec% /k cscript  """ & strPath & """"
      objShell.Run(strCommand), 1, True
      Wscript.Quit
End If

Set objFSO = CreateObject("Scripting.FileSystemObject")

strComputersFile = "Computers.txt"
strFileNamesFile = "FilesToDelete.txt"
' Please specify the full path to PSList.exe here
strPSListPath = "n:\utilities\pstools\pslist.exe"
strPSListPath = objFSO.getfile(strPSListPath).ShortPath

Const intForReading = 1
Set objInputFile = objFSO.OpenTextFile(strComputersFile, intForReading, False)

strLog = "Deleting files from remote computers"

While Not objInputFile.AtEndOfStream
      strUserComputer = objInputFile.ReadLine

      If Ping(strUserComputer) = True Then
            Set objFileNamesFile = objFSO.OpenTextFile(strFileNamesFile, intForReading, False)
            While Not objFileNamesFile.AtEndOfStream
                  'strFileToDelete = "C:\program files\Nokia\nokia pc suite 6\PcSync2.exe"
                  strFileToDelete = objFileNamesFile.ReadLine
                  If objFSO.FileExists("\\" & strUserComputer & "\" & Replace(strFileToDelete, ":", "$")) = True Then
                        strProcessName = Mid(strFileToDelete, InStrRev(strFileToDelete, "\") + 1)
                        If InStr(strProcessName, ".") > 0 Then strProcessName = Left(strProcessName, InStrRev(strProcessName, ".") - 1)
                        'PSList gives the following return values
                        ' 1 : process does not exist
                        ' 0 : successful - process found
                        strResponse = objShell.Run(strPSListPath & " -accepteula -e \\" & strComputer & " " & strProcessName, 0, True)
                        While strResponse = 0
                              WScript.Echo strProcessName & " is running."
                              strCommand = "TASKKILL /S " & strUserComputer &  " /F /IM " & Mid(strFileToDelete, InStrRev(strFileToDelete, "\") + 1) & " /T"
                              objShell.Run strCommand, 0, True
                              strResponse = objShell.Run(strPSListPath & " -accepteula -e \\" & strComputer & " " & strProcessName, 1, True)
                        Wend
                        ' Now look for any process that is not the same name, but may have the file open
                        boolProcHold = True
                        While boolProcHold = True
                              WScript.Echo "Checking if any other process has a handle on this file."
                              strCommand = "TASKLIST /S " & strUserComputer &  " /V | FINDSTR /I """ & Mid(strFileToDelete, InStrRev(strFileToDelete, "\") + 1) & """ > " & Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "ProcessHold.txt"
                              objShell.Run strCommand, 0, True
                              Set objProcHold = objFSO.OpenTextFile(Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "ProcessHold.txt", intForReading, False)
                              If Not objProcHold.AtEndOfStream Then
                                    boolProcHold = True
                                    strHoldingProcess = Trim(Left(objProcHold.ReadLine, 28))
                                    WScript.Echo strHoldingProcess & " has a handle on " & Mid(strFileToDelete, InStrRev(strFileToDelete, "\") + 1)
                                    strCommand = "TASKKILL /S " & strUserComputer &  " /F /IM " & strHoldingProcess & " /T"
                                    objShell.Run strCommand, 0, True
                              Else
                                    WScript.Echo "No other process is holding onto " & Mid(strFileToDelete, InStrRev(strFileToDelete, "\") + 1)
                                    boolProcHold = False
                              End If
                              objProcHold.Close
                              Set objProcHold = Nothing
                              objFSO.DeleteFile Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "ProcessHold.txt", True
                        Wend
                        'WScript.Echo "Attempting to delete " & "\\" & strUserComputer & "\" & Replace(strFileToDelete, ":", "$")
                        On Error Resume Next
                        objFSO.DeleteFile "\\" & strUserComputer & "\" & Replace(strFileToDelete, ":", "$"), True
                        If Err.Number <> 0 Then
                              WScript.Echo strUserComputer & ": ERROR deleting " & strFileToDelete & " : " & Err.Number & " - " & Err.Description
                              strLog = strLog & VbCrLf & strUserComputer & ": ERROR deleting " & strFileToDelete & " : " & Err.Number & " - " & Err.Description
                              Err.Clear
                              On Error GoTo 0
                        Else
                              Err.Clear
                              On Error GoTo 0
                              WScript.Echo strUserComputer & ": Deleted " & strFileToDelete
                              strLog = strLog & VbCrLf & strUserComputer & ": Deleted " & strFileToDelete
                        End If
                  Else
                        WScript.Echo strFileToDelete & " does not exist on " & strUserComputer
                        strLog = strLog & VbCrLf & strFileToDelete & " does not exist on " & strUserComputer
                  End If
            Wend
            objFileNamesFile.Close
            Set objFileNamesFile = Nothing
      Else
            WScript.Echo "Could not ping " & strUserComputer
            strLog = strLog & VbCrLf & "Could not ping " & strUserComputer
      End If
Wend
objInputFile.Close
Set objInputFile = Nothing

Set objOutputFile = objFSO.CreateTextFile("Delete_File_Results.txt", True)
objOutputFile.Write strLog
objOutputFile.Close
Set objOutputFile = Nothing

WScript.Echo ""
WScript.Echo "Done"
MsgBox "Done"

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

Regards,

Rob.
I get this Rob..

Checking if any other process has a handle on this file.
C:\Delete all files from all computes.vbs(50, 31) Microsoft VBScript runtime err
or: File not found
Ha ha, that's what I get for not testing....please try this:

'===========
Set objShell = CreateObject("Wscript.Shell")
If LCase(Right(Wscript.FullName, 11)) = "wscript.exe" Then
      strPath = Wscript.ScriptFullName
      strCommand = "%comspec% /k cscript  """ & strPath & """"
      objShell.Run(strCommand), 1, True
      Wscript.Quit
End If

Set objFSO = CreateObject("Scripting.FileSystemObject")

strComputersFile = "Computers.txt"
strFileNamesFile = "FilesToDelete.txt"
' Please specify the full path to PSList.exe here
strPSListPath = "n:\utilities\pstools\pslist.exe"
strPSListPath = objFSO.getfile(strPSListPath).ShortPath

Const intForReading = 1
Set objInputFile = objFSO.OpenTextFile(strComputersFile, intForReading, False)

strLog = "Deleting files from remote computers"

While Not objInputFile.AtEndOfStream
      strUserComputer = objInputFile.ReadLine

      If Ping(strUserComputer) = True Then
            Set objFileNamesFile = objFSO.OpenTextFile(strFileNamesFile, intForReading, False)
            While Not objFileNamesFile.AtEndOfStream
                  'strFileToDelete = "C:\program files\Nokia\nokia pc suite 6\PcSync2.exe"
                  strFileToDelete = objFileNamesFile.ReadLine
                  If objFSO.FileExists("\\" & strUserComputer & "\" & Replace(strFileToDelete, ":", "$")) = True Then
                        strProcessName = Mid(strFileToDelete, InStrRev(strFileToDelete, "\") + 1)
                        If InStr(strProcessName, ".") > 0 Then strProcessName = Left(strProcessName, InStrRev(strProcessName, ".") - 1)
                        'PSList gives the following return values
                        ' 1 : process does not exist
                        ' 0 : successful - process found
                        strResponse = objShell.Run(strPSListPath & " -accepteula -e \\" & strComputer & " " & strProcessName, 0, True)
                        While strResponse = 0
                              WScript.Echo strProcessName & " is running."
                              strCommand = "TASKKILL /S " & strUserComputer &  " /F /IM " & Mid(strFileToDelete, InStrRev(strFileToDelete, "\") + 1) & " /T"
                              objShell.Run strCommand, 0, True
                              strResponse = objShell.Run(strPSListPath & " -accepteula -e \\" & strComputer & " " & strProcessName, 1, True)
                        Wend
                        ' Now look for any process that is not the same name, but may have the file open
                        boolProcHold = True
                        While boolProcHold = True
                              WScript.Echo "Checking if any other process has a handle on this file."
                              strProcHoldFile = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "ProcHold.txt"
                              Set objOutputFile = objFSO.CreateTextFile(strProcHoldFile, True)
                              objOutputFile.Close
                              Set objOutputFile = Nothing
                              strProcHoldFile = objFSO.GetFile(strProcHoldFile).ShortPath
                              strCommand = "cmd /c TASKLIST /S " & strUserComputer &  " /V | FINDSTR /I """ & Mid(strFileToDelete, InStrRev(strFileToDelete, "\") + 1) & """ > " & strProcHoldFile
                              objShell.Run strCommand, 0, True
                              Set objProcHold = objFSO.OpenTextFile(strProcHoldFile, intForReading, False)
                              If Not objProcHold.AtEndOfStream Then
                                    boolProcHold = True
                                    strHoldingProcess = Trim(Left(objProcHold.ReadLine, 28))
                                    WScript.Echo strHoldingProcess & " has a handle on " & Mid(strFileToDelete, InStrRev(strFileToDelete, "\") + 1)
                                    strCommand = "TASKKILL /S " & strUserComputer &  " /F /IM " & strHoldingProcess & " /T"
                                    objShell.Run strCommand, 0, True
                              Else
                                    WScript.Echo "No other process is holding onto " & Mid(strFileToDelete, InStrRev(strFileToDelete, "\") + 1)
                                    boolProcHold = False
                              End If
                              objProcHold.Close
                              Set objProcHold = Nothing
                              objFSO.DeleteFile strProcHoldFile, True
                        Wend
                        'WScript.Echo "Attempting to delete " & "\\" & strUserComputer & "\" & Replace(strFileToDelete, ":", "$")
                        On Error Resume Next
                        objFSO.DeleteFile "\\" & strUserComputer & "\" & Replace(strFileToDelete, ":", "$"), True
                        If Err.Number <> 0 Then
                              WScript.Echo strUserComputer & ": ERROR deleting " & strFileToDelete & " : " & Err.Number & " - " & Err.Description
                              strLog = strLog & VbCrLf & strUserComputer & ": ERROR deleting " & strFileToDelete & " : " & Err.Number & " - " & Err.Description
                              Err.Clear
                              On Error GoTo 0
                        Else
                              Err.Clear
                              On Error GoTo 0
                              WScript.Echo strUserComputer & ": Deleted " & strFileToDelete
                              strLog = strLog & VbCrLf & strUserComputer & ": Deleted " & strFileToDelete
                        End If
                  Else
                        WScript.Echo strFileToDelete & " does not exist on " & strUserComputer
                        strLog = strLog & VbCrLf & strFileToDelete & " does not exist on " & strUserComputer
                  End If
            Wend
            objFileNamesFile.Close
            Set objFileNamesFile = Nothing
      Else
            WScript.Echo "Could not ping " & strUserComputer
            strLog = strLog & VbCrLf & "Could not ping " & strUserComputer
      End If
Wend
objInputFile.Close
Set objInputFile = Nothing

Set objOutputFile = objFSO.CreateTextFile("Delete_File_Results.txt", True)
objOutputFile.Write strLog
objOutputFile.Close
Set objOutputFile = Nothing

WScript.Echo ""
WScript.Echo "Done"
MsgBox "Done"

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

Regards

Rob.
Rob just to clarify...

If i have 10 machines in "Computers.txt" and 5 file paths in the other txt file.
The script will scan each file path with all the machines and if found in any machine will delete it.
Am i right?
If there is any process holding it  then it will kill the process.
Yes, in theory (hopefully)!  I can't guarantee that it will be able to kill the processes, but it should find whatever program is holding onto that particular file, and (hopefully) kill the process, then delete the file...

Rob.
ANy help with the excel part Rob...
I get this...
C:\WINDOWS\inf\Other.exe does not exist on DEV-CHEN-PC1152
Checking if any other process has a handle on this file.
C:\Delete all files from all computes.vbs(49, 31) Microsoft VBScript runtime err
or: Permission denied

I have 250 machines and 80 files .
I get the above message in the 5th machine
Sharath, above this line:
Set objOutputFile = objFSO.CreateTextFile(strProcHoldFile, True)

please put this line
If objFSO.FileExists(strProcHoldFile) = True Then objFSO.DeleteFile strProcHoldFile, True

It seems very strange that it can't create "ProcHold.txt" in the same folder that the script is run from....

Regards,

Rob.
It gets stuck ar this point.Say fom 10min its the same...

D:\\Lor\data\x\xxafaaaa.a\psexec.exe does not exist on DEV-CHEN-PC748
Checking if any other process has a handle on this file.
I just tried deleted the file and then ran the script i get this...

Checking if any other process has a handle on this file.
C:\Delete all files from all computes.vbs(49, 51) Microsoft VBScript runtime err
or: Permission denied
Sharath, that's really strange.....that line where you get the error only deletes the temporary ProcHold.txt file:
If objFSO.FileExists(strProcHoldFile) = True Then objFSO.DeleteFile strProcHoldFile, True

try commenting it out and see the next line automatically overwrites it anyway....

Regards,

Rob.
Rob i have noticed that Findstr.exe is holding the "Prohold.txt" file thats the reason the script is not able to remove it .I guess...
I tried Commenting it and i get this now...

C:\Delete all files from all computes.vbs(50, 31) Microsoft VBScript runtime error: Permission denied
Ah, right....yeah, the new error would be for the same reason then....it's trying to delete / create a file that already has a lock on it.  Try this, it should continue to try to delete the file every second....

'===========
Set objShell = CreateObject("Wscript.Shell")
If LCase(Right(Wscript.FullName, 11)) = "wscript.exe" Then
      strPath = Wscript.ScriptFullName
      strCommand = "%comspec% /k cscript  """ & strPath & """"
      objShell.Run(strCommand), 1, True
      Wscript.Quit
End If

Set objFSO = CreateObject("Scripting.FileSystemObject")

strComputersFile = "Computers.txt"
strFileNamesFile = "FilesToDelete.txt"
' Please specify the full path to PSList.exe here
strPSListPath = "n:\utilities\pstools\pslist.exe"
strPSListPath = objFSO.getfile(strPSListPath).ShortPath

Const intForReading = 1
Set objInputFile = objFSO.OpenTextFile(strComputersFile, intForReading, False)

strLog = "Deleting files from remote computers"

While Not objInputFile.AtEndOfStream
      strUserComputer = objInputFile.ReadLine

      If Ping(strUserComputer) = True Then
            Set objFileNamesFile = objFSO.OpenTextFile(strFileNamesFile, intForReading, False)
            While Not objFileNamesFile.AtEndOfStream
                  'strFileToDelete = "C:\program files\Nokia\nokia pc suite 6\PcSync2.exe"
                  strFileToDelete = objFileNamesFile.ReadLine
                  If objFSO.FileExists("\\" & strUserComputer & "\" & Replace(strFileToDelete, ":", "$")) = True Then
                        strProcessName = Mid(strFileToDelete, InStrRev(strFileToDelete, "\") + 1)
                        If InStr(strProcessName, ".") > 0 Then strProcessName = Left(strProcessName, InStrRev(strProcessName, ".") - 1)
                        'PSList gives the following return values
                        ' 1 : process does not exist
                        ' 0 : successful - process found
                        strResponse = objShell.Run(strPSListPath & " -accepteula -e \\" & strComputer & " " & strProcessName, 0, True)
                        While strResponse = 0
                              WScript.Echo strProcessName & " is running."
                              strCommand = "TASKKILL /S " & strUserComputer &  " /F /IM " & Mid(strFileToDelete, InStrRev(strFileToDelete, "\") + 1) & " /T"
                              objShell.Run strCommand, 0, True
                              strResponse = objShell.Run(strPSListPath & " -accepteula -e \\" & strComputer & " " & strProcessName, 1, True)
                        Wend
                        ' Now look for any process that is not the same name, but may have the file open
                        boolProcHold = True
                        While boolProcHold = True
                              WScript.Echo "Checking if any other process has a handle on this file."
                              strProcHoldFile = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "ProcHold.txt"
                              If objFSO.FileExists(strProcHoldFile) = True Then
                                    On Error Resume Next
                                    objFSO.DeleteFile strProcHoldFile, True
                                    While Err.Number <> 0 Then
                                          WScript.Sleep 1000
                                          objFSO.DeleteFile strProcHoldFile, True
                                    Wend
                                    Err.Clear
                                    On Error GoTo 0
                              End If                                          
                              Set objOutputFile = objFSO.CreateTextFile(strProcHoldFile, True)
                              objOutputFile.Close
                              Set objOutputFile = Nothing
                              strProcHoldFile = objFSO.GetFile(strProcHoldFile).ShortPath
                              strCommand = "cmd /c TASKLIST /S " & strUserComputer &  " /V | FINDSTR /I """ & Mid(strFileToDelete, InStrRev(strFileToDelete, "\") + 1) & """ > " & strProcHoldFile
                              objShell.Run strCommand, 0, True
                              Set objProcHold = objFSO.OpenTextFile(strProcHoldFile, intForReading, False)
                              If Not objProcHold.AtEndOfStream Then
                                    boolProcHold = True
                                    strHoldingProcess = Trim(Left(objProcHold.ReadLine, 28))
                                    WScript.Echo strHoldingProcess & " has a handle on " & Mid(strFileToDelete, InStrRev(strFileToDelete, "\") + 1)
                                    strCommand = "TASKKILL /S " & strUserComputer &  " /F /IM " & strHoldingProcess & " /T"
                                    objShell.Run strCommand, 0, True
                              Else
                                    WScript.Echo "No other process is holding onto " & Mid(strFileToDelete, InStrRev(strFileToDelete, "\") + 1)
                                    boolProcHold = False
                              End If
                              objProcHold.Close
                              Set objProcHold = Nothing
                              If objFSO.FileExists(strProcHoldFile) = True Then
                                    On Error Resume Next
                                    objFSO.DeleteFile strProcHoldFile, True
                                    While Err.Number <> 0 Then
                                          WScript.Sleep 1000
                                          objFSO.DeleteFile strProcHoldFile, True
                                    Wend
                                    Err.Clear
                                    On Error GoTo 0
                              End If                                          
                        Wend
                        'WScript.Echo "Attempting to delete " & "\\" & strUserComputer & "\" & Replace(strFileToDelete, ":", "$")
                        On Error Resume Next
                        objFSO.DeleteFile "\\" & strUserComputer & "\" & Replace(strFileToDelete, ":", "$"), True
                        If Err.Number <> 0 Then
                              WScript.Echo strUserComputer & ": ERROR deleting " & strFileToDelete & " : " & Err.Number & " - " & Err.Description
                              strLog = strLog & VbCrLf & strUserComputer & ": ERROR deleting " & strFileToDelete & " : " & Err.Number & " - " & Err.Description
                              Err.Clear
                              On Error GoTo 0
                        Else
                              Err.Clear
                              On Error GoTo 0
                              WScript.Echo strUserComputer & ": Deleted " & strFileToDelete
                              strLog = strLog & VbCrLf & strUserComputer & ": Deleted " & strFileToDelete
                        End If
                  Else
                        WScript.Echo strFileToDelete & " does not exist on " & strUserComputer
                        strLog = strLog & VbCrLf & strFileToDelete & " does not exist on " & strUserComputer
                  End If
            Wend
            objFileNamesFile.Close
            Set objFileNamesFile = Nothing
      Else
            WScript.Echo "Could not ping " & strUserComputer
            strLog = strLog & VbCrLf & "Could not ping " & strUserComputer
      End If
Wend
objInputFile.Close
Set objInputFile = Nothing

Set objOutputFile = objFSO.CreateTextFile("Delete_File_Results.txt", True)
objOutputFile.Write strLog
objOutputFile.Close
Set objOutputFile = Nothing

WScript.Echo ""
WScript.Echo "Done"
MsgBox "Done"

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

Regards,

Rob.
I get this on the start...
---------------------------
Windows Script Host
---------------------------
Script:      C:\Delete all files from all computes.vbs
Line:      52
Char:      59
Error:      Expected statement
Code:      800A0400
Source:       Microsoft VBScript compilation error

---------------------------
OK  
---------------------------
Ooops, please remove the "Then" word from the two statements that are:
While Err.Number <> 0 Then

which are on lines 52 and 81.

Regards,

Rob.
Now it gets stuck here
C:\WINDOWS\system32\windev-peers.ini does not exist on DEV-CHEN-PC748
Checking if any other process has a handle on this file.
Reminder....
Sharath, try this....the only change I've made is to use a maximum retry to delete the file.

Perhaps it is worth finding out what is using ProcHold.txt though....so before you try the below code, run what you have again, then when it gets stuck, open a command prompt on your computer, and run this command in it:
TASKLIST /V | FINDSTR /I "ProcHold.txt"

'===========
Set objShell = CreateObject("Wscript.Shell")
If LCase(Right(Wscript.FullName, 11)) = "wscript.exe" Then
      strPath = Wscript.ScriptFullName
      strCommand = "%comspec% /k cscript  """ & strPath & """"
      objShell.Run(strCommand), 1, True
      Wscript.Quit
End If

Set objFSO = CreateObject("Scripting.FileSystemObject")

strComputersFile = "Computers.txt"
strFileNamesFile = "FilesToDelete.txt"
' Please specify the full path to PSList.exe here
strPSListPath = "n:\utilities\pstools\pslist.exe"
strPSListPath = objFSO.getfile(strPSListPath).ShortPath

Const intForReading = 1
Set objInputFile = objFSO.OpenTextFile(strComputersFile, intForReading, False)

strLog = "Deleting files from remote computers"

While Not objInputFile.AtEndOfStream
      strUserComputer = objInputFile.ReadLine

      If Ping(strUserComputer) = True Then
            Set objFileNamesFile = objFSO.OpenTextFile(strFileNamesFile, intForReading, False)
            While Not objFileNamesFile.AtEndOfStream
                  'strFileToDelete = "C:\program files\Nokia\nokia pc suite 6\PcSync2.exe"
                  strFileToDelete = objFileNamesFile.ReadLine
                  If objFSO.FileExists("\\" & strUserComputer & "\" & Replace(strFileToDelete, ":", "$")) = True Then
                        strProcessName = Mid(strFileToDelete, InStrRev(strFileToDelete, "\") + 1)
                        If InStr(strProcessName, ".") > 0 Then strProcessName = Left(strProcessName, InStrRev(strProcessName, ".") - 1)
                        'PSList gives the following return values
                        ' 1 : process does not exist
                        ' 0 : successful - process found
                        strResponse = objShell.Run(strPSListPath & " -accepteula -e \\" & strComputer & " " & strProcessName, 0, True)
                        While strResponse = 0
                              WScript.Echo strProcessName & " is running."
                              strCommand = "TASKKILL /S " & strUserComputer &  " /F /IM " & Mid(strFileToDelete, InStrRev(strFileToDelete, "\") + 1) & " /T"
                              objShell.Run strCommand, 0, True
                              strResponse = objShell.Run(strPSListPath & " -accepteula -e \\" & strComputer & " " & strProcessName, 1, True)
                        Wend
                        ' Now look for any process that is not the same name, but may have the file open
                        boolProcHold = True
                        While boolProcHold = True
                              WScript.Echo "Checking if any other process has a handle on this file."
                              strProcHoldFile = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "ProcHold.txt"
                              If objFSO.FileExists(strProcHoldFile) = True Then
                                    On Error Resume Next
                                    objFSO.DeleteFile strProcHoldFile, True
                                    intRetry = 0
                                    While Err.Number <> 0 And intRetry < 10
                                          WScript.Sleep 1000
                                          objFSO.DeleteFile strProcHoldFile, True
                                          intRetry = intRetry + 1
                                    Wend
                                    Err.Clear
                                    On Error GoTo 0
                                    If intRetry = 10 Then WScript.Echo "Failed to delete " & strProcHoldFile & " after 10 attempts."
                              End If                                          
                              Set objOutputFile = objFSO.CreateTextFile(strProcHoldFile, True)
                              objOutputFile.Close
                              Set objOutputFile = Nothing
                              strProcHoldFile = objFSO.GetFile(strProcHoldFile).ShortPath
                              strCommand = "cmd /c TASKLIST /S " & strUserComputer &  " /V | FINDSTR /I """ & Mid(strFileToDelete, InStrRev(strFileToDelete, "\") + 1) & """ > " & strProcHoldFile
                              objShell.Run strCommand, 0, True
                              Set objProcHold = objFSO.OpenTextFile(strProcHoldFile, intForReading, False)
                              If Not objProcHold.AtEndOfStream Then
                                    boolProcHold = True
                                    strHoldingProcess = Trim(Left(objProcHold.ReadLine, 28))
                                    WScript.Echo strHoldingProcess & " has a handle on " & Mid(strFileToDelete, InStrRev(strFileToDelete, "\") + 1)
                                    strCommand = "TASKKILL /S " & strUserComputer &  " /F /IM " & strHoldingProcess & " /T"
                                    objShell.Run strCommand, 0, True
                              Else
                                    WScript.Echo "No other process is holding onto " & Mid(strFileToDelete, InStrRev(strFileToDelete, "\") + 1)
                                    boolProcHold = False
                              End If
                              objProcHold.Close
                              Set objProcHold = Nothing
                              If objFSO.FileExists(strProcHoldFile) = True Then
                                    On Error Resume Next
                                    objFSO.DeleteFile strProcHoldFile, True
                                    intRetry = 0
                                    While Err.Number <> 0 And intRetry < 10
                                          WScript.Sleep 1000
                                          objFSO.DeleteFile strProcHoldFile, True
                                          intRetry = intRetry + 1
                                    Wend
                                    Err.Clear
                                    On Error GoTo 0
                                    If intRetry = 10 Then WScript.Echo "Failed to delete " & strProcHoldFile & " after 10 attempts."
                              End If                                          
                        Wend
                        'WScript.Echo "Attempting to delete " & "\\" & strUserComputer & "\" & Replace(strFileToDelete, ":", "$")
                        On Error Resume Next
                        objFSO.DeleteFile "\\" & strUserComputer & "\" & Replace(strFileToDelete, ":", "$"), True
                        If Err.Number <> 0 Then
                              WScript.Echo strUserComputer & ": ERROR deleting " & strFileToDelete & " : " & Err.Number & " - " & Err.Description
                              strLog = strLog & VbCrLf & strUserComputer & ": ERROR deleting " & strFileToDelete & " : " & Err.Number & " - " & Err.Description
                              Err.Clear
                              On Error GoTo 0
                        Else
                              Err.Clear
                              On Error GoTo 0
                              WScript.Echo strUserComputer & ": Deleted " & strFileToDelete
                              strLog = strLog & VbCrLf & strUserComputer & ": Deleted " & strFileToDelete
                        End If
                  Else
                        WScript.Echo strFileToDelete & " does not exist on " & strUserComputer
                        strLog = strLog & VbCrLf & strFileToDelete & " does not exist on " & strUserComputer
                  End If
            Wend
            objFileNamesFile.Close
            Set objFileNamesFile = Nothing
      Else
            WScript.Echo "Could not ping " & strUserComputer
            strLog = strLog & VbCrLf & "Could not ping " & strUserComputer
      End If
Wend
objInputFile.Close
Set objInputFile = Nothing

Set objOutputFile = objFSO.CreateTextFile("Delete_File_Results.txt", True)
objOutputFile.Write strLog
objOutputFile.Close
Set objOutputFile = Nothing

WScript.Echo ""
WScript.Echo "Done"
MsgBox "Done"

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

Regards,

Rob.
I get this for the latest script...
C:\Delete all files from all computes.vbs(62, 31) Microsoft VBScript runtime err
or: Permission denied
I tried the first script..The place it gets stuck i put
TASKLIST /V | FINDSTR /I "ProcHold.txt"
It displays nothing..
Can you give me a results file of that success and failures...To know if the script has deleted anything...
Here is another

(Failed to delete C:\ProcHold.txt after 10 attempts.)

 does not exist on DEV-CHEN-PC1755
Checking if any other process has a handle on this file.
Failed to delete C:\ProcHold.txt after 10 attempts.
C:\Delete all files from all computes.vbs(62, 31) Microsoft VBScript runtime err
or: Permission denied
Hmmmm, do the DOS commands
TaskList /V
and
findstr /I "computers" c:\*.*

work on your computer?  Maybe those command aren't completing?

Rob.
Yes both the command show results...
Any help on this Rob....
Please try this.  I have changed the output file from "ProcHold.txt" to a randomly generated temporary file that will go into your Temp folder.

'===========
Set objShell = CreateObject("Wscript.Shell")
If LCase(Right(Wscript.FullName, 11)) = "wscript.exe" Then
      strPath = Wscript.ScriptFullName
      strCommand = "%comspec% /k cscript  """ & strPath & """"
      objShell.Run(strCommand), 1, True
      Wscript.Quit
End If

Set objFSO = CreateObject("Scripting.FileSystemObject")

strComputersFile = "Computers.txt"
strFileNamesFile = "FilesToDelete.txt"
' Please specify the full path to PSList.exe here
strPSListPath = "n:\utilities\pstools\pslist.exe"
strPSListPath = objFSO.getfile(strPSListPath).ShortPath

Const intForReading = 1
Set objInputFile = objFSO.OpenTextFile(strComputersFile, intForReading, False)

strLog = "Deleting files from remote computers"

Const TemporaryFolder = 2
Set objFolder = objFSO.GetSpecialFolder(TemporaryFolder)

While Not objInputFile.AtEndOfStream
      strUserComputer = objInputFile.ReadLine

      If Ping(strUserComputer) = True Then
            Set objFileNamesFile = objFSO.OpenTextFile(strFileNamesFile, intForReading, False)
            While Not objFileNamesFile.AtEndOfStream
                  'strFileToDelete = "C:\program files\Nokia\nokia pc suite 6\PcSync2.exe"
                  strFileToDelete = objFileNamesFile.ReadLine
                  If objFSO.FileExists("\\" & strUserComputer & "\" & Replace(strFileToDelete, ":", "$")) = True Then
                        strProcessName = Mid(strFileToDelete, InStrRev(strFileToDelete, "\") + 1)
                        If InStr(strProcessName, ".") > 0 Then strProcessName = Left(strProcessName, InStrRev(strProcessName, ".") - 1)
                        'PSList gives the following return values
                        ' 1 : process does not exist
                        ' 0 : successful - process found
                        strResponse = objShell.Run(strPSListPath & " -accepteula -e \\" & strComputer & " " & strProcessName, 0, True)
                        While strResponse = 0
                              WScript.Echo strProcessName & " is running."
                              strCommand = "TASKKILL /S " & strUserComputer &  " /F /IM " & Mid(strFileToDelete, InStrRev(strFileToDelete, "\") + 1) & " /T"
                              objShell.Run strCommand, 0, True
                              strResponse = objShell.Run(strPSListPath & " -accepteula -e \\" & strComputer & " " & strProcessName, 1, True)
                        Wend
                        ' Now look for any process that is not the same name, but may have the file open
                        boolProcHold = True
                        While boolProcHold = True
                              WScript.Echo "Checking if any other process has a handle on this file."
                              strTempFile = objFSO.GetTempName    
                              strProcHoldFile = objFolder.Path & "\" & strTempFile
                              If objFSO.FileExists(strProcHoldFile) = True Then
                                    On Error Resume Next
                                    objFSO.DeleteFile strProcHoldFile, True
                                    intRetry = 0
                                    While Err.Number <> 0 And intRetry < 10
                                          WScript.Sleep 1000
                                          objFSO.DeleteFile strProcHoldFile, True
                                          intRetry = intRetry + 1
                                    Wend
                                    Err.Clear
                                    On Error GoTo 0
                                    If intRetry = 10 Then WScript.Echo "Failed to delete " & strProcHoldFile & " after 10 attempts."
                              End If                                          
                              Set objOutputFile = objFSO.CreateTextFile(strProcHoldFile, True)
                              objOutputFile.Close
                              Set objOutputFile = Nothing
                              strProcHoldFile = objFSO.GetFile(strProcHoldFile).ShortPath
                              strCommand = "cmd /c TASKLIST /S " & strUserComputer &  " /V | FINDSTR /I """ & Mid(strFileToDelete, InStrRev(strFileToDelete, "\") + 1) & """ > " & strProcHoldFile
                              objShell.Run strCommand, 0, True
                              Set objProcHold = objFSO.OpenTextFile(strProcHoldFile, intForReading, False)
                              If Not objProcHold.AtEndOfStream Then
                                    boolProcHold = True
                                    strHoldingProcess = Trim(Left(objProcHold.ReadLine, 28))
                                    WScript.Echo strHoldingProcess & " has a handle on " & Mid(strFileToDelete, InStrRev(strFileToDelete, "\") + 1)
                                    strCommand = "TASKKILL /S " & strUserComputer &  " /F /IM " & strHoldingProcess & " /T"
                                    objShell.Run strCommand, 0, True
                              Else
                                    WScript.Echo "No other process is holding onto " & Mid(strFileToDelete, InStrRev(strFileToDelete, "\") + 1)
                                    boolProcHold = False
                              End If
                              objProcHold.Close
                              Set objProcHold = Nothing
                              If objFSO.FileExists(strProcHoldFile) = True Then
                                    On Error Resume Next
                                    objFSO.DeleteFile strProcHoldFile, True
                                    intRetry = 0
                                    While Err.Number <> 0 And intRetry < 10
                                          WScript.Sleep 1000
                                          objFSO.DeleteFile strProcHoldFile, True
                                          intRetry = intRetry + 1
                                    Wend
                                    Err.Clear
                                    On Error GoTo 0
                                    If intRetry = 10 Then WScript.Echo "Failed to delete " & strProcHoldFile & " after 10 attempts."
                              End If                                          
                        Wend
                        'WScript.Echo "Attempting to delete " & "\\" & strUserComputer & "\" & Replace(strFileToDelete, ":", "$")
                        On Error Resume Next
                        objFSO.DeleteFile "\\" & strUserComputer & "\" & Replace(strFileToDelete, ":", "$"), True
                        If Err.Number <> 0 Then
                              WScript.Echo strUserComputer & ": ERROR deleting " & strFileToDelete & " : " & Err.Number & " - " & Err.Description
                              strLog = strLog & VbCrLf & strUserComputer & ": ERROR deleting " & strFileToDelete & " : " & Err.Number & " - " & Err.Description
                              Err.Clear
                              On Error GoTo 0
                        Else
                              Err.Clear
                              On Error GoTo 0
                              WScript.Echo strUserComputer & ": Deleted " & strFileToDelete
                              strLog = strLog & VbCrLf & strUserComputer & ": Deleted " & strFileToDelete
                        End If
                  Else
                        WScript.Echo strFileToDelete & " does not exist on " & strUserComputer
                        strLog = strLog & VbCrLf & strFileToDelete & " does not exist on " & strUserComputer
                  End If
            Wend
            objFileNamesFile.Close
            Set objFileNamesFile = Nothing
      Else
            WScript.Echo "Could not ping " & strUserComputer
            strLog = strLog & VbCrLf & "Could not ping " & strUserComputer
      End If
Wend
objInputFile.Close
Set objInputFile = Nothing

Set objOutputFile = objFSO.CreateTextFile("Delete_File_Results.txt", True)
objOutputFile.Write strLog
objOutputFile.Close
Set objOutputFile = Nothing

WScript.Echo ""
WScript.Echo "Done"
MsgBox "Done"

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

Regards,

Rob.
Rob as this script searches each file with all the machines it takes a long time to finish and gets stuck some places Is it possible that i a single file ui ahve details as below seperated by a ":" Colon.So that the script can go to the machine and delete the file next to it.

Machine name                    Filepath
DEV-CHEN-PC2227           F:\autorun.inf
DEV-CHEN-PC2534           C:\WINDOWS\system32\DWRCS.EXE
DEV-CHEN-PC1755            F:\setup.exe

After the script scanned for a long time its stuck here.

C:\WINDOWS\system32\DWRCS.EXE does not exist on DEV-CHEN-PC1567
Checking if any other process has a handle on this file.


I don't think we'll try the different format of the text file just yet, we'd probably have similar issues anyway.....

Above each line that reads:
objShell.Run strCommand, 0, True

Please put
WScript.Echo "Running: " & strCommand

so I can see what the command is that gets executed.

Regards,

Rob.
I get this...
C:\WINDOWS\system32\DWRCS.EXE does not exist on DEV-CHEN-PC1567
Checking if any other process has a handle on this file.
Running: cmd /c TASKLIST /S DEV-CHEN-PC1567 /V | FINDSTR /I "DWRCS.EXE" > C:\DOC
UME~1\ADMINI~1.DEV\LOCALS~1\Temp\rad3E7E3.tmp
OK, so if you run
cmd /c TASKLIST /S DEV-CHEN-PC1567 /V | FINDSTR /I "DWRCS.EXE"

and if that works, then run
cmd /c TASKLIST /S DEV-CHEN-PC1567 /V | FINDSTR /I "DWRCS.EXE" > C:\SomeTempFile.txt

does that work?

Regards,

Rob.
I get this...
C:\>cmd /c TASKLIST /S DEV-CHEN-PC1567 /V | FINDSTR /I "DWRCS.EXE"
ERROR: Logon failure: unknown user name or bad password.
Sharath, I would expect that all of these "Logon Failures" or "Access Denied" errors that you are getting seem to be the result of either
a) Simple File Sharing is enabled
    - Disable via Windows Explorer --> Tools --> Folder Options --> View tab --> untick Use Simple File Sharing

or b) DCOM Permission Issues or WMI Corruption
Try running the WMI diagnostic tool:
http://www.microsoft.com/downloads/details.aspx?familyid=D7BA3CD6-18D1-4D05-B11E-4C64192AE97D&displaylang=en

You can also try a few other steps, which the diagnostic tool should pick up anyway....
On the remote machine, please check that DCOM is enabled, and make sure it has the correct permissions to allow COM objects to run.  Click Start --> Settings --> Control Panel --> Administrative Tools --> Component Services
Then expand Component Services --> Computers --> My Computer
Right click My Computer, go to Properties.  On the Default Properties tab, check the Enable Distributed COM on this computer box.
Then on the COM Security tab, click Edit Default... under Launch and Activation Permissions and make sure the Local Administrators group has Full Access.
Then make sure that your domain account is in the Local Administrators group in Computer Management.  Restart the system and try again.



So, essentially, make sure all this is healthy on the target system, then run
cmd /c TASKLIST /S DEV-CHEN-PC1567 /V | FINDSTR /I "DWRCS.EXE"

again.

Regards,

Rob.
Rob any machines that have such problems can it skip them and continue...

I am checking the Wmi
OK, that means we'll have to read the output of the TASKLIST command, so I'll work on that...

Regards,

Rob.
Rob how can i find the issues in the logs files that are created
WMI diagnostic tool:
I can send the zip file if you can give your email id if possible...
Reminder..
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Rob this goes on without any error.
I shall close this Q and raise a new one.
Can you help changing the way the script works.
I have the Machine name and filetodelete next to each other.
Any way that it deletes the file in the exact machine
Say i have 200 computers and 300 files.At present this script takes each file and scans every computers for just 1 file to delete in 1 computer.
Yeah, that should be fine....if you have specific files to delete for specific computers, then that makes sense anyway.

Raise another question, and I'll change the way it takes the input, and get rid of the need for two input files.

Regards,

Rob.