We help IT Professionals succeed at work.

How can i delete all files that are in a file no matter with what it is associated...

bsharath
bsharath asked
on
615 Views
Last Modified: 2012-06-27
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
Comment
Watch Question

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 .

Author

Commented:
I want a way to do it from my machine...
Delete the file...Kill the process if any...

Author

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...
Try using WSrcipt which will give you command to execute on remote machine.

http://www.microsoft.com/technet/scriptcenter/guide/sas_wsh_crve.mspx
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
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.

Author

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
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
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.

Author

Commented:
I get an error on 24,13
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks a lot Rob...

Even when i put d: or e: \ paths will it delete
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
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.

Author

Commented:
Thanks a lot Rob...

Author

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
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
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.

Author

Commented:
Ok Rob thanks

Author

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

Author

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...
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
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.

Author

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

https://www.experts-exchange.com/Programming/Languages/Scripting/Q_22881484.html
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.