bsharath
asked on
How can i delete all files that are in a file no matter with what it is associated...
Hi,
I have a file which has the file name like.
'C:\program files\Nokia\nokia pc suite 6\PcSync2.exe'
Is there a way that i remote delete all the files listed like this.
Regards
Sharath
I have a file which has the file name like.
'C:\program files\Nokia\nokia pc suite 6\PcSync2.exe'
Is there a way that i remote delete all the files listed like this.
Regards
Sharath
yes you can if your user has rights, and before that you will have to kill the file if its running using command tskill and then delete it using FileSystemObject .
ASKER
I want a way to do it from my machine...
Delete the file...Kill the process if any...
Delete the file...Kill the process if any...
ASKER
What i mean is i have the machine name from which the files need to delete so can i do this from my machine...I have the admin rights on all machines in the network...
Try using WSrcipt which will give you command to execute on remote machine.
http://www.microsoft.com/technet/scriptcenter/guide/sas_wsh_crve.mspx
http://www.microsoft.com/technet/scriptcenter/guide/sas_wsh_crve.mspx
Sharath, try this:
'================
Set objShell = CreateObject("Wscript.Shel l")
If LCase(Right(Wscript.FullNa me, 11)) = "wscript.exe" Then
  strPath = Wscript.ScriptFullName
  strCommand = "%comspec% /c cscript  """ & strPath & """"
  objShell.Run(strCommand), 1, True
  Wscript.Quit
End If
strComputersFile = "Computers.txt"
Set objFSO = CreateObject("Scripting.Fi leSystemOb ject")
Const intForReading = 1
Set objInputFile = objFSO.OpenTextFile(strCom putersFile , intForReading, False)
strFileToDelete = "C:\program files\Nokia\nokia pc suite 6\PcSync2.exe"
While Not objInputFile.AtEndOfStream
      strUserComputer = objInputFile.ReadLine
     Â
      If Ping(strUserComputer) = True Then
           objShell.Run "TASKKILL /S " & strUserComputer & " /F /IM " & Mid(strFileToDelete, InStrRev(strFileToDelete, "\") + 1) & " /T", 0, True
           objFSO.DeleteFile "\\" & strUserComputer & Replace(strFileToDelete, ":", "$"), True
           WScript.Echo strUserComputer & ": Deleted " & strFileToDelete
      Else
           WScript.Echo ""
           WScript.Echo "Could not ping " & strUserComputer
      End If
Wend
WScript.Echo ""
WScript.Echo "Done"
MsgBox "Done"
Function Ping(strComputer)
      Dim objShell, boolCode
      Set objShell = CreateObject("WScript.Shel l")
      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.
'================
Set objShell = CreateObject("Wscript.Shel
If LCase(Right(Wscript.FullNa
  strPath = Wscript.ScriptFullName
  strCommand = "%comspec% /c cscript  """ & strPath & """"
  objShell.Run(strCommand), 1, True
  Wscript.Quit
End If
strComputersFile = "Computers.txt"
Set objFSO = CreateObject("Scripting.Fi
Const intForReading = 1
Set objInputFile = objFSO.OpenTextFile(strCom
strFileToDelete = "C:\program files\Nokia\nokia pc suite 6\PcSync2.exe"
While Not objInputFile.AtEndOfStream
      strUserComputer = objInputFile.ReadLine
     Â
      If Ping(strUserComputer) = True Then
           objShell.Run "TASKKILL /S " & strUserComputer & " /F /IM " & Mid(strFileToDelete, InStrRev(strFileToDelete, "\") + 1) & " /T", 0, True
           objFSO.DeleteFile "\\" & strUserComputer & Replace(strFileToDelete, ":", "$"), True
           WScript.Echo strUserComputer & ": Deleted " & strFileToDelete
      Else
           WScript.Echo ""
           WScript.Echo "Could not ping " & strUserComputer
      End If
Wend
WScript.Echo ""
WScript.Echo "Done"
MsgBox "Done"
Function Ping(strComputer)
      Dim objShell, boolCode
      Set objShell = CreateObject("WScript.Shel
      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.
ASKER
Rob when i am running this remotely will it go through the remote machines c$ and then delete the files.
Will i have a report of success or failure
Will i have a report of success or failure
Yes, it deletes a file via the UNC path of
\\COMPNAME\C$\program files\Nokia\nokia pc suite 6\PcSync2.exe
This will also log to a text file:
'===========
Set objShell = CreateObject("Wscript.Shel l")
If LCase(Right(Wscript.FullNa me, 11)) = "wscript.exe" Then
  strPath = Wscript.ScriptFullName
  strCommand = "%comspec% /c cscript  """ & strPath & """"
  objShell.Run(strCommand), 1, True
  Wscript.Quit
End If
strComputersFile = "Computers.txt"
Set objFSO = CreateObject("Scripting.Fi leSystemOb ject")
Const intForReading = 1
Set objInputFile = objFSO.OpenTextFile(strCom putersFile , intForReading, False)
strFileToDelete = "C:\program files\Nokia\nokia pc suite 6\PcSync2.exe"
strLog = "Deleting files from remote computers"
While Not objInputFile.AtEndOfStream
   strUserComputer = objInputFile.ReadLine
  Â
   If Ping(strUserComputer) = True Then
      objShell.Run "TASKKILL /S " & strUserComputer & " /F /IM " & Mid(strFileToDelete, InStrRev(strFileToDelete, "\") + 1) & " /T", 0, True
      objFSO.DeleteFile "\\" & strUserComputer & Replace(strFileToDelete, ":", "$"), True
      WScript.Echo strUserComputer & ": Deleted " & strFileToDelete
      strLog = strLog & VbCrLf & strUserComputer & ": Deleted " & strFileToDelete
   Else
      WScript.Echo ""
      WScript.Echo "Could not ping " & strUserComputer
      strLog = strLog & VbCrLf & "Could not ping " & strUserComputer
   End If
Wend
Set objOuputFile = objFSO.CreateTextFile("Del ete_File_R esults.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.Shel l")
   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.
\\COMPNAME\C$\program files\Nokia\nokia pc suite 6\PcSync2.exe
This will also log to a text file:
'===========
Set objShell = CreateObject("Wscript.Shel
If LCase(Right(Wscript.FullNa
  strPath = Wscript.ScriptFullName
  strCommand = "%comspec% /c cscript  """ & strPath & """"
  objShell.Run(strCommand), 1, True
  Wscript.Quit
End If
strComputersFile = "Computers.txt"
Set objFSO = CreateObject("Scripting.Fi
Const intForReading = 1
Set objInputFile = objFSO.OpenTextFile(strCom
strFileToDelete = "C:\program files\Nokia\nokia pc suite 6\PcSync2.exe"
strLog = "Deleting files from remote computers"
While Not objInputFile.AtEndOfStream
   strUserComputer = objInputFile.ReadLine
  Â
   If Ping(strUserComputer) = True Then
      objShell.Run "TASKKILL /S " & strUserComputer & " /F /IM " & Mid(strFileToDelete, InStrRev(strFileToDelete, "\") + 1) & " /T", 0, True
      objFSO.DeleteFile "\\" & strUserComputer & Replace(strFileToDelete, ":", "$"), True
      WScript.Echo strUserComputer & ": Deleted " & strFileToDelete
      strLog = strLog & VbCrLf & strUserComputer & ": Deleted " & strFileToDelete
   Else
      WScript.Echo ""
      WScript.Echo "Could not ping " & strUserComputer
      strLog = strLog & VbCrLf & "Could not ping " & strUserComputer
   End If
Wend
Set objOuputFile = objFSO.CreateTextFile("Del
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.Shel
   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.
ASKER
I get an error on 24,13
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Thanks a lot Rob...
Even when i put d: or e: \ paths will it delete
Even when i put d: or e: \ paths will it delete
Yeah, should do, although, given in the other post we've just deleted the admin default shares of D, E, F, and G, then, no, it would stop working....
Rob.
Rob.
ASKER
Thanks a lot Rob...
ASKER
Rob with the related to this Q...
Actually why i wanted this script is many time i get mails from Sophos stating some virus or hacking tool is installed and not able to remove because of permissions/Access denied...
I get mails as such...
In my Mailbox
Machine: DEV-CHEN-PC206
Process 'C:\program files\common files\Real\update_ob\reals ched.exe' exhibiting suspicious behavior pattern 'HIPS/RegMod-001'.
Is there a way a script can scan all mails in that folder and get me just the machinenames "DEV-CHEN-PC206" in columA
and the Process "'C:\program files\common files\Real\update_ob\reals ched.exe' exhibiting suspicious behavior pattern 'HIPS/RegMod-001" Â in Colum B
So that i can sort and use this script to easier the task...
Any way...
If required i can save all msg file in a folder in D:\foldername
Actually why i wanted this script is many time i get mails from Sophos stating some virus or hacking tool is installed and not able to remove because of permissions/Access denied...
I get mails as such...
In my Mailbox
Machine: DEV-CHEN-PC206
Process 'C:\program files\common files\Real\update_ob\reals
Is there a way a script can scan all mails in that folder and get me just the machinenames "DEV-CHEN-PC206" in columA
and the Process "'C:\program files\common files\Real\update_ob\reals
So that i can sort and use this script to easier the task...
Any way...
If required i can save all msg file in a folder in D:\foldername
Hmmm, I'm not sure about that....that would be complicated....however, after we've finished some of these others, raise a new question, and I'll try to look into it....
Rob.
Rob.
ASKER
Ok Rob thanks
ASKER
Rob just tryed this with many machines but get error on line 28,
If the machine is off or not available with it go to the next machine
If the machine is off or not available with it go to the next machine
ASKER
One mor help.
Can all the machine be in one file and all the files that need to delte in another can a script with the same concept be done.
I shall raise a new Q...If you say yes...
Can all the machine be in one file and all the files that need to delte in another can a script with the same concept be done.
I shall raise a new Q...If you say yes...
Sharath, try this.
I have added strPSListPath, which uses PSList.exe (from Microsoft's PSTools) to find out if more than one instance of the process is running.
I have also put error checking on the deletion of the file which will log to the log file as well.
'===========
Set objShell = CreateObject("Wscript.Shel l")
If LCase(Right(Wscript.FullNa me, 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.Fi leSystemOb ject")
strComputersFile = "Computers.txt"
strFileNamesFile = "FilesToDelete.txt"
' Please specify the full path to PSList.exe here
strPSListPath = "n:\utilities\pstools\psli st.exe"
strPSListPath = objFSO.getfile(strPSListPa th).ShortP ath
Const intForReading = 1
Set objInputFile = objFSO.OpenTextFile(strCom putersFile , intForReading, False)
strLog = "Deleting files from remote computers"
While Not objInputFile.AtEndOfStream
      strUserComputer = objInputFile.ReadLine
      If Ping(strUserComputer) = True Then
           Set objFileNamesFile = objFSO.OpenTextFile(strFil eNamesFile , intForReading, False)
           While Not objFileNamesFile.AtEndOfSt ream
                 '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("Del ete_File_R esults.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.Shel l")
      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 have added strPSListPath, which uses PSList.exe (from Microsoft's PSTools) to find out if more than one instance of the process is running.
I have also put error checking on the deletion of the file which will log to the log file as well.
'===========
Set objShell = CreateObject("Wscript.Shel
If LCase(Right(Wscript.FullNa
      strPath = Wscript.ScriptFullName
      strCommand = "%comspec% /k cscript  """ & strPath & """"
      objShell.Run(strCommand), 1, True
      Wscript.Quit
End If
Set objFSO = CreateObject("Scripting.Fi
strComputersFile = "Computers.txt"
strFileNamesFile = "FilesToDelete.txt"
' Please specify the full path to PSList.exe here
strPSListPath = "n:\utilities\pstools\psli
strPSListPath = objFSO.getfile(strPSListPa
Const intForReading = 1
Set objInputFile = objFSO.OpenTextFile(strCom
strLog = "Deleting files from remote computers"
While Not objInputFile.AtEndOfStream
      strUserComputer = objInputFile.ReadLine
      If Ping(strUserComputer) = True Then
           Set objFileNamesFile = objFSO.OpenTextFile(strFil
           While Not objFileNamesFile.AtEndOfSt
                 '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
                      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
                      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("Del
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.Shel
      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.
ASKER
Rob i have already raise a Q hee can you check there.I have posted the message i get
https://www.experts-exchange.com/questions/22881484/Need-to-delete-files-from-all-machines-in-the-file.html
https://www.experts-exchange.com/questions/22881484/Need-to-delete-files-from-all-machines-in-the-file.html