Solved

delete file using vbs

Posted on 2011-09-09
5
285 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 54

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 54

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 54

Expert Comment

by:Bill Prew
ID: 36512468
Welcome.

~bp
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We have adopted the strategy to use Computers in Student Labs as the bulletin boards. The same target can be achieved by using a Login Notice feature in Group policy but it’s not as attractive as graphical wallpapers with message which grabs the att…
Sometimes people don't understand why download speed shows differently for Windows than Linux.Specially, this article covers and shows the solution for throughput difference for Windows than a Linux machine. For this, I arranged a test scenario.I…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

749 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