Solved

delete file using vbs

Posted on 2011-09-09
5
282 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:epicazo
  • 3
5 Comments
 
LVL 12

Expert Comment

by:josika
ID: 36512277
You would use
objFSO.DeleteFile "c:\whatever.pdf"

Open in new window

0
 
LVL 51

Accepted Solution

by:
Bill Prew earned 250 total points
ID: 36512278
This should handle that.

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"
Const TargetFile = "C:\Documents and Settings\All Users\Desktop\MSRosterBySpecialty_201108.lnk"

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, ":\","$\")
                        objFSO.DeleteFile "\\" & 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

~bp
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 36512289
Sorry, forgot one change after the cut and paste.  Replace this line:

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

with:

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

~bp
0
 

Author Closing Comment

by:epicazo
ID: 36512434
Thank you much!!!  :D
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 36512468
Welcome.

~bp
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now