script which will delete files from client machine picked up from a file

I need a script which will delete files from client machine
This script I will run manually
I have the list of Windows 7 64 bit machines
So the script should be able to pull computer names form a file
A script which will pull computer names from a file and delete the specified file from those machines
ADTADatacenterAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

skrgaCommented:
Will you run script directly on client computer or from some other ps ? If so, are administrative shares enabled ?
0
ADTADatacenterAuthor Commented:

Well, I will run the script from my administration machine
All machines are members of the same domain
I have admin rights on the all machines
Admin shares are enabled as default
I need to delete some files on the machines
I will give the machine names as a text file
The script should be able to pick up these machines names from the text file and run the Del command on each machine
The script should also give a feedback: -
Which machine the file was deleted on
Which machines it could not access (permission denied)
Which machines it could not reach (shutdown)
Which machine the file was not present
0
skrgaCommented:
In what format/structure are computer names  /  file names in your list file(s) ?
0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

ADTADatacenterAuthor Commented:
I have exported them from active directory
Text file
I can convert to excel file also if required
The names are listed one name in a line
0
skrgaCommented:
Do you have one file with computer names and other with full filename (C:\temp\tmp.txt) ?
0
ADTADatacenterAuthor Commented:
The file name I can give in the script itself
I want to delete only one file
File is one
Computers are many
So no need to give file name in a separate file
Only computers as they are many need to be given in a separate file
0
RobSampsonCommented:
Hi, try this script.

Change these three lines as needed:
strComputers = "computers.txt"
arrFilesToDelete = Array("C:\Windows\Temp\File1.txt", "C:\Windows\Temp\File2.txt")
strLogFile = "Delete_File_Results.txt"

Regards,

Rob.
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")
 
strComputers = "computers.txt"
arrFilesToDelete = Array("C:\Windows\Temp\File1.txt", "C:\Windows\Temp\File2.txt")
strLogFile = "Delete_File_Results.txt"
 
Const intForReading = 1
Set objInputFile = objFSO.OpenTextFile(strComputers, intForReading, False)

strLog = "Deleting files from remote computers"
 
While Not objInputFile.AtEndOfStream
	strComputer = Trim(objInputFile.ReadLine)
	If Ping(strUserComputer) = True Then
      	For Each strFileToDelete In arrFilesToDelete
            If objFSO.FileExists("\\" & strComputer & "\" & Replace(strFileToDelete, ":", "$")) = True Then
				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
		Next
	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(strLogFile, True)
objOutputFile.Write strLog
objOutputFile.Close
Set objOutputFile = Nothing
 
WScript.Echo ""
WScript.Echo "Done. Please see " & strLogFile
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

Open in new window

0
ADTADatacenterAuthor Commented:
Right
Looks good
Will try and update case
Many thanks
0
Bill PrewCommented:
Something like this should work in a BAT script approach.

@echo off
set ListFile=c:\temp\list.txt
set LogFile=c:\temp\logfile.txt
set DeleteFile=C$\dir1\file1.txt
for /F "usebackq tokens=*" %%A in ("%ListFile%") do (
  echo %DATE% %TIME% - Deleting from computer [%%A]
  del /F "\\%%A\%DeleteFile%" >>"%LogFile%" 2>>&1
)

Open in new window

~bp
0
ADTADatacenterAuthor Commented:
Please forgive me for not updating the case
Was not in office since the past 4 days
Will update today
0
ADTADatacenterAuthor Commented:

Dear Rob, your script give the below error
I can ping and connect to the computers listed in the input file name computers.txt
I have listed the computer names one after the other in the input file
At the moment I am testing with two computer names

Deleting files from remote computers
Could not ping
0
ADTADatacenterAuthor Commented:
Dear billprew

I think maybe your script has a closing bracket missing
It echo's in the log file that it is deleting the files form remote machine but does not delete the file
I think this is because it takes the deletion command also as text to be echoed in the output file
The output looks like below: -

13:47:19.32 - Deleting from computer [dt1-000itd-xp-6.adta.uae] del /F "\\dt1-000itd-xp-6.adta.uae\c$\windows\system32\file1.txt"  
0
Bill PrewCommented:
The code I posted looks okay as I reread it, are you sure you didn't accidentally delete the line break between the ECHO and the DEL command and they ended up on the same line in the script?

~bp
0
RobSampsonCommented:
Sorry, mine had a typo.  Try this.

Regards,

Rob.
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")
 
strComputers = "computers.txt"
arrFilesToDelete = Array("C:\Windows\Temp\File1.txt", "C:\Windows\Temp\File2.txt")
strLogFile = "Delete_File_Results.txt"

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

strLog = "Deleting files from remote computers"
 
While Not objInputFile.AtEndOfStream
	strComputer = Trim(objInputFile.ReadLine)
	If Ping(strComputer) = True Then
      	For Each strFileToDelete In arrFilesToDelete
            If objFSO.FileExists("\\" & strComputer & "\" & Replace(strFileToDelete, ":", "$")) = True Then
				On Error Resume Next
				objFSO.DeleteFile "\\" & strComputer & "\" & Replace(strFileToDelete, ":", "$"), True
				If Err.Number <> 0 Then
					WScript.Echo strComputer & ": ERROR deleting " & strFileToDelete & " : " & Err.Number & " - " & Err.Description
					strLog = strLog & VbCrLf & strComputer & ": ERROR deleting " & strFileToDelete & " : " & Err.Number & " - " & Err.Description
					Err.Clear
					On Error GoTo 0
				Else
					Err.Clear
					On Error GoTo 0
					WScript.Echo strComputer & ": Deleted " & strFileToDelete
					strLog = strLog & VbCrLf & strComputer & ": Deleted " & strFileToDelete
				End If
			Else
				WScript.Echo strFileToDelete & " does not exist on " & strComputer
				strLog = strLog & VbCrLf & strFileToDelete & " does not exist on " & strComputer
			End If
		Next
	Else
		WScript.Echo "Could not ping " & strComputer
		strLog = strLog & VbCrLf & "Could not ping " & strComputer
	End If
Wend
objInputFile.Close
Set objInputFile = Nothing
 
Set objOutputFile = objFSO.CreateTextFile(strLogFile, True)
objOutputFile.Write strLog
objOutputFile.Close
Set objOutputFile = Nothing
 
WScript.Echo ""
WScript.Echo "Done. Please see " & strLogFile
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

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ADTADatacenterAuthor Commented:
Dear Billprew,
Will retry the script making sure the line break exists

Dear Rob,
Will try your updated script and revert back too

Many thanks to both for replying
0
ADTADatacenterAuthor Commented:
Dear billprew:
You were right
I had deleted the line break
Your script runs smoothly and deletes the file
Short and sweet
But it does not insert a success log in the output log file
The log file is generated but nothing shows up inside the log file
It is empty
But if it cannot find the file which is to be deleted it puts up an error in the log file
So successes not getting reported but failures are highlighted
Just like in real life
Any suggestions to make it the perfect short and sweet script?
0
Bill PrewCommented:
Give this a try.

@echo off
set ListFile=c:\temp\list.txt
set LogFile=c:\temp\logfile.txt
set DeleteFile=C$\dir1\file1.txt
for /F "usebackq tokens=*" %%A in ("%ListFile%") do (
  echo %DATE% %TIME% - Deleting from computer [%%A]
  del /F "\\%%A\%DeleteFile%" && echo File deleted. || Errors deleting file.
) >>"%LogFile%" 2>>&1

Open in new window

~bp
0
ADTADatacenterAuthor Commented:
Dear Rob,
Wooooof,
Your script runs smooth as butter man
Just double click; it also opens a command prompt window to show the "real time processing"
Logs the results in a log file
Cold not ask for more
Many thanks man
0
ADTADatacenterAuthor Commented:
Dear billprew,
I see you have tamed down your script
But it still misbehaves
On machines where it does not find the file it first logs that file was not found but on the second line it again says file deleted!
As can be seen below
I think just a little bit of tweaking required to make it the shortest working file deletion script

Mon 12/19/2011 17:34:00.58 - Deleting from computer [d01-win7-32-1.adta.uae]
Could Not Find \\d01-win7-32-1.adta.uae\C$\windows\system32\file1.txt
File deleted.

Also when it cannot find a machine it first gives the right error that network path was not found but on the second line says Errors is not a command
Any idea, am I doing something wrong this time too?


Mon 12/19/2011 17:37:09.80 - Deleting from computer [d01uu]
The network path was not found.
'Errors' is not recognized as an internal or external command,
operable program or batch file.
0
Bill PrewCommented:
Okay, adding a bit more code to check for these conditions, see how this goes.

@echo off
set ListFile=c:\temp\list.txt
set LogFile=c:\temp\logfile.txt
set DeleteFile=C$\dir1\file1.txt
for /F "usebackq tokens=*" %%A in ("%ListFile%") do (
  echo %DATE% %TIME% - Deleting from computer [%%A]
  if exist "\\%%A\%DeleteFile%" (
    del /F "\\%%A\%DeleteFile%"
    if exist "\\%%A\%DeleteFile%" (
      echo File was NOT deleted on this server.
    ) else (
      echo File was deleted on this server.
    )
  ) else (
    echo File does not exist on this server.
  )
) >>"%LogFile%" 2>>&1

Open in new window

~bp
0
RobSampsonCommented:
Good to hear.

@Bill, as you know, I'm not crash hot with batch, but on line 7 of the code in comment ID:37308577, do you need this:
 || Errors deleting file.

to be
 || echo Errors deleting file.

to avoid the "Errors" is not recognised message?

Anyway, I like it better with the notification of whether the file exists or not ;-)

Rob.
0
Bill PrewCommented:
@Rob,

Hello Rob, yes, that was definitely a problem, causing the error about "Errors". But in researching further it looked like the DEL command doesn't set the typical normal exit versus abnormal exit code, so checking for it didn't seem to be the way to go.  So I dropped back to a bit more brute force approach, but arguably safer.

Thanks for the feedback, appreciate it.

~bp
0
ADTADatacenterAuthor Commented:
Dear billprew, will check your new version and revert back
0
ADTADatacenterAuthor Commented:
Dear billprew,
Sorry for the late reply
Multiple issues yesterday
Today also same story continues

But I have checked your script
It's perfect
Short and sweet
Just double click and it does its job
It opens a command prompt blank window until it has finished
So we know when to check the log file for details
Very nice
Many thanks to you and Rob for helping to this gigantic task of deleting old files from client machines
As our users do not restart their machines running a group policy to do the same was not a fast solution, as the file existed in the windows directory it could have been accessed only by a computer group policy which runs at restart only
Many thanks again
I will close this case
0
ADTADatacenterAuthor Commented:
Many Thanks to the experts who worked and gave the soltuions
0
RobSampsonCommented:
No problem at all.  Glad to help.

Regards,

Rob.
0
Bill PrewCommented:
Welcome, glad that was useful / educational, thanks for the feedback.

~bp
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Legacy OS

From novice to tech pro — start learning today.