Solved

delete file using vbs

Posted on 2011-09-09
5
288 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 56

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 56

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 56

Expert Comment

by:Bill Prew
ID: 36512468
Welcome.

~bp
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup" or a blinking cursor with black screen. A loop for Auto repair will start but fix nothing.  You will be panic as there are no back…
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…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

617 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