Solved

delete file using vbs

Posted on 2011-09-09
5
287 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 55

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 55

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 55

Expert Comment

by:Bill Prew
ID: 36512468
Welcome.

~bp
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Can I legally transfer my OEM version of Windows to another PC?  (AKA - Can I put a new systemboard in my OEM PC?) Few of us are both IT and legal experts but we all have our own views of Microsoft's licensing rules and how they apply.  There are…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
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…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…

728 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