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
LVL 11
bsharathAsked:
Who is Participating?
 
RobSampsonCommented:
Sorry, I forgot to make a check for whether the file exists in the first place or not....try this:

'===========
Set objShell = CreateObject("Wscript.Shell")
If LCase(Right(Wscript.FullName, 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.FileSystemObject")
Const intForReading = 1
Set objInputFile = objFSO.OpenTextFile(strComputersFile, 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
            If objFSO.FileExists("\\" & strUserComputer & "\" & Replace(strFileToDelete, ":", "$")) = True Then
                  strCommand = "TASKKILL /S " & strUserComputer &  " /F /IM " & Mid(strFileToDelete, InStrRev(strFileToDelete, "\") + 1) & " /T"
                  'WScript.Echo "Running command: " & strCommand
                  objShell.Run strCommand, 0, True
                  'WScript.Echo "Attempting to delete " & "\\" & strUserComputer & "\" & Replace(strFileToDelete, ":", "$")
                  objFSO.DeleteFile "\\" & strUserComputer & "\" & Replace(strFileToDelete, ":", "$"), True
                  WScript.Echo strUserComputer & ": Deleted " & strFileToDelete
                  strLog = strLog & VbCrLf & strUserComputer & ": Deleted " & strFileToDelete
              Else
                    WScript.Echo strFileToDelete & " does not exist on " & strUserComputer
                    strLog = strLog & VbCrLf & strFileToDelete & " does not exist on " & strUserComputer
              End If
      Else
            WScript.Echo "Could not ping " & strUserComputer
            strLog = strLog & VbCrLf & "Could not ping " & strUserComputer
      End If
Wend

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.
0
 
Ashish PatelCommented:
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 .
0
 
bsharathAuthor Commented:
I want a way to do it from my machine...
Delete the file...Kill the process if any...
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
bsharathAuthor Commented:
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...
0
 
Ashish PatelCommented:
Try using WSrcipt which will give you command to execute on remote machine.

http://www.microsoft.com/technet/scriptcenter/guide/sas_wsh_crve.mspx
0
 
RobSampsonCommented:
Sharath, try this:

'================
Set objShell = CreateObject("Wscript.Shell")
If LCase(Right(Wscript.FullName, 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.FileSystemObject")
Const intForReading = 1
Set objInputFile = objFSO.OpenTextFile(strComputersFile, 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.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.
0
 
bsharathAuthor Commented:
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
0
 
RobSampsonCommented:
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.Shell")
If LCase(Right(Wscript.FullName, 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.FileSystemObject")
Const intForReading = 1
Set objInputFile = objFSO.OpenTextFile(strComputersFile, 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("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.
0
 
bsharathAuthor Commented:
I get an error on 24,13
0
 
bsharathAuthor Commented:
Thanks a lot Rob...

Even when i put d: or e: \ paths will it delete
0
 
RobSampsonCommented:
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.
0
 
bsharathAuthor Commented:
Thanks a lot Rob...
0
 
bsharathAuthor Commented:
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\realsched.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\realsched.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
0
 
RobSampsonCommented:
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.
0
 
bsharathAuthor Commented:
Ok Rob thanks
0
 
bsharathAuthor Commented:
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
0
 
bsharathAuthor Commented:
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...
0
 
RobSampsonCommented:
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.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
'===========

Regards,

Rob.
0
 
bsharathAuthor Commented:
Rob i have already raise a Q hee can you check there.I have posted the message i get

http://www.experts-exchange.com/Programming/Languages/Scripting/Q_22881484.html
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.