Link to home
Create AccountLog in
Avatar of bsharath
bsharathFlag for India

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
Avatar of Ashish Patel
Ashish Patel
Flag of India image

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 .
Avatar of bsharath

ASKER

I want a way to do it from my machine...
Delete the file...Kill the process if any...
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
Avatar of RobSampson
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.
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
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.
I get an error on 24,13
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Thanks a lot Rob...

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.
Thanks a lot Rob...
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
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.
Ok Rob thanks
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
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...
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.
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