how I can i delete a file if it exist with VBS

hello,

I have this code
Sub WriteDat(texte)
    On Error Resume Next
    Const ForAppending = 8
    Set fso = CreateObject("Scripting.FileSystemObject")
    set tf = fso.OpenTextFile("C:\TAS\file.dat", ForAppending, True)
    tf.writeline texte
    tf.Close

 On Error GOTO 0
End Sub

Open in new window


and i need to verify before writing the text if the file "file.dat " exist or not in my folder ,
if it exist i need to delete it then create and write on my file
i know that i need to add DeleteFile but i don't know how ..
someone can help me please ?
mell lian90Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewIT / Software Engineering ConsultantCommented:
Based on what you described I don't think you need to do as much as you think.  If you change the OpenTextFile to be ForWriting, then it will overwrite the file if it exists, or create it if it doesn't.  Sounds like you don't want Append mode...

Sub WriteDat(texte)
    On Error Resume Next
    Const ForReading = 1
    Const ForWriting = 2
    Const ForAppending = 8
    Set fso = CreateObject("Scripting.FileSystemObject")
    set tf = fso.OpenTextFile("C:\TAS\file.dat", ForWriting, True)
    tf.writeline texte
    tf.Close

    On Error GOTO 0
End Sub

Open in new window


»bp
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bill PrewIT / Software Engineering ConsultantCommented:
If you really do need to do a delete though, it would look like:

    If fso.FileExists("C:\TAS\file.dat") Then
        fso.DeleteFile("C:\TAS\file.dat")
    End If

Open in new window


»bp
0
mell lian90Author Commented:
hi @ Bill Prew ,

can you plese tel me how i can add the plow code
  If fso.FileExists("C:\TAS\file.dat") Then
        fso.DeleteFile("C:\TAS\file.dat")
    End If

Open in new window

in my original code .. because it didn't work for me
0
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Bill PrewIT / Software Engineering ConsultantCommented:
Sub WriteDat(texte)
    On Error Resume Next
    Const ForAppending = 8
    Set fso = CreateObject("Scripting.FileSystemObject")

    If fso.FileExists("C:\TAS\file.dat") Then
        fso.DeleteFile("C:\TAS\file.dat")
    End If

    set tf = fso.OpenTextFile("C:\TAS\file.dat", ForAppending, True)
    tf.writeline texte
    tf.Close

 On Error GOTO 0
End Sub

Open in new window


»bp
0
mell lian90Author Commented:
thanks for your help Bill Prew ..
it's work for me but with the use of 2 functions , the first for ForAppending and the second ForWriting
thanks ..
0
mell lian90Author Commented:
i use this solution to resolve my issue
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.