Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script to DELETE file IF present

Status
Not open for further replies.

JROY2011

IS-IT--Management
Aug 24, 2011
12
0
0
US
I have this script setup to delete an unattend file in windows after I sysprep a pc. I want the script to delete the file, and it does. BUT if the file isn't there for whatever reason, I want the script to skip the delete process and end without an error.

Set obj = CreateObject("Scripting.FileSystemObject")
obj.DeleteFile("C:\Windows\System32\sysprep\unattend.xml")
Set obj = CreateObject("Scripting.FileSystemObject")
obj.DeleteFile("C:\Windows\Panther\unattend.xml")

Dim objFSO
'Create a File System Object

Set objFSO = CreateObject("Scripting.FileSystemObject")
'Delete the currently executing script

objFSO.DeleteFile WScript.ScriptFullName

 
Simply use the FileExists method.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I'm not good at writing scripts. I kind of pieced that one together. Would it look something like this?

Set obj = CreateObject("Scripting.FileSystemObject")
If obj.FileExists("C:\Windows\System32\sysprep\unattend.xml") Then
obj.DeleteFile("C:\Windows\System32\sysprep\unattend.xml")
Set obj = CreateObject("Scripting.FileSystemObject")
If obj.FileExists("C:\Windows\Panther\unattend.xml") Then
obj.DeleteFile("C:\Windows\Panther\unattend.xml")

Dim objFSO
'Create a File System Object

Set objFSO = CreateObject("Scripting.FileSystemObject")
'Delete the currently executing script

objFSO.DeleteFile WScript.ScriptFullName


End If
 
close. You need to END your IF's and you need to create the FileSystemObject only once.

Code:
set objFSO = CreateObject("Scripting.FileSystemObject")

if (objFSO.FileExists("C:\Windows\system32\sysprep\unattend.xml")) then
   objFSO.DeleteFile("C:\Windows\system32\sysprep\unattend.xml")) 
end if

if (objFSO.FileExists("C:\Windows\panther\unattend.xml")) then
   objFSO.DeleteFile("C:\Windows\panther\unattend.xml")) 
end if

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
This is a simple script that will delete a file, if the file is not there it will quit.

'******Start Script*********
Dim objFileSys
Set objFileSys = CreateObject("Scripting.FileSystemObject")
If objFileSys.FileExists("C:\Test\test.txt") Then
objFileSys.DeleteFile "C:\Test\test.txt"
WScript.Echo ("File found and deleted")

Else
WScript.Echo ("Not found")
WScript.Quit
End If
'******End Script********

you can comment out the echo, used just for testing.

HTH
Gavin...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top