Link to home
Start Free TrialLog in
Avatar of epicazo
epicazoFlag for United States of America

asked on

delete file using vbs

used the code below to copy a PDF file to about 150 XP machines.  However, I wanted to delete the file instead.  What is the syntax to delete the files.  

I appreciate it in advance!!!!


Const ComputersList = "C:\sysadmin\MSRoster\CopyFilesComputers_MHG.txt"
Const SourceFile = "C:\sysadmin\MSRoster\MSRosterBySpecialty_201108.lnk"
Const TargetFolder = "C:\Documents and Settings\All Users\Desktop\"
Const ReportFile = "C:\sysadmin\MSRoster\CopyFilesLog.txt"

If Right(TargetFolder, 1) <> "\" Then TargetFolder = TargetFolder & "\"	
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objFile : Set objFile = objFSO.CreateTextFile (ReportFile)
arrComputers = Split(objFSO.OpenTextFile(ComputersList).ReadAll, vbNewLine)
 
For Each PC in arrComputers
	If Trim(PC) <> "" Then
		If Ping(PC) = True Then
			objFile.WriteLine "Attempting copy to " & PC
			On Error Resume Next
			objFSO.CopyFile SourceFile, "\\" & PC & "\" & Replace(TargetFolder, ":\","$\")
			If Err.Number <> 0 Then
				objFile.WriteLine vbTab & "Failed to copy to \\"  & PC & "\" & Replace(TargetFolder, ":\","$\")
			Else
				objFile.WriteLine vbTab & "File successfully copied to " & PC & "\" & Replace(TargetFolder, ":\","$\")
			End If
			Err.Clear
			On Error Goto 0
		Else
			objFile.WriteLine vbTab & PC & " is offline"
		End If
	End If
Next
 
objFile.Close

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

Avatar of josika
josika
Flag of United States of America image

You would use
objFSO.DeleteFile "c:\whatever.pdf"

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bill Prew
Bill Prew

Sorry, forgot one change after the cut and paste.  Replace this line:

objFSO.DeleteFile "\\" & PC & "\" & Replace(TargetFolder, ":\","$\")

with:

objFSO.DeleteFile "\\" & PC & "\" & Replace(TargetFile, ":\","$\")

~bp
Avatar of epicazo

ASKER

Thank you much!!!  :D
Welcome.

~bp