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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to overwrite file

Status
Not open for further replies.

josie2007

Technical User
Apr 14, 2007
90
US
I am getting The file 'C:\Expedite\gen\pass2ebs.txt' already exists error message and I would like to overwrite the old file with a new one ....any help is appreciated


Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim FileToCopy As String
Dim NewCopy As String

FileToCopy = "W:\io\qebsfnam\pass2ebs"
NewCopy = "C:\Expedite\gen\pass2ebs.txt"

If System.IO.File.Exists(FileToCopy) = True Then

System.IO.File.Copy(FileToCopy, NewCopy)

MsgBox("New file has been Copied Over too Expedite folder")
End If

End Sub
 
Just delete it right before you copy it over in this case...

Code:
If System.IO.File.Exists(FileToCopy) = True Then

            System.IO.File.Delete(NewCopy)
            System.IO.File.Copy(FileToCopy, NewCopy)

            MsgBox("New file has been Copied Over too Expedite folder")
        End If

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB/Access Programmer
 
You could check if the destination file exists and delete it if it does:

Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim FileToCopy As String
        Dim NewCopy As String

        FileToCopy = "W:\io\qebsfnam\pass2ebs"
        NewCopy = "C:\Expedite\gen\pass2ebs.txt"

        If System.IO.File.Exists(FileToCopy) = True Then

            If System.IO.File.Exists(NewCopy ) Then
               System.IO.File.Delete(NewCopy)
            End If
            System.IO.File.Copy(FileToCopy, NewCopy)

            MsgBox("New file has been Copied Over to Expedite folder")
        End If

    End Sub
 
You could do:

Code:
...Exists(FileToCopy)...

If System.IO.File.Exists(NewCopy) Then
   System.IO.File.Delete(NewCopy)
End If

...Copy...
 
I like that, every one has answered this post at the same time :-D
 
Using System.IO.File.Delete will throw an exception if the file does not exist. To use that method you would have to do this:

Code:
        If System.IO.File.Exists(FileToCopy) = True Then

            If System.IO.File.Exists(NewCopy) = True Then
                    System.IO.File.Delete(NewCopy)
            End If
            System.IO.File.Copy(FileToCopy, NewCopy)

            MsgBox("New file has been Copied Over too Expedite folder")
        End If
 
Wow, thank you all for your quick response. You all got my star!
 
My intention was to convert this code written in vb6 to vb.net as i struggle learning vb.net can someone take a look at this code and help me how i can convert it to vb.net


Private Sub Command1_Click()
Dim p1 As Integer
Dim L As Integer
Dim I As String
Dim FileString As String



'copy file from the server to c:drive
FileCopy "J:\IO\qebsfnam\pass2ebs", "C:\pass2ebs.txt"


Open "C:\pass2ebs.txt" For Input As #1
Open "C:\pass.txt" For Output As #2

Do While Not EOF(1)
Line Input #1, FileString
L = Len(FileString)
p1 = InStr(FileString, "/")
I = LTrim(Left(FileString, 6))

If (I = "ITEM01" And p1) Then
If Mid(FileString, p1 - 4, 1) <> "." Then
FileString = Left(FileString, p1 - 4) & "." & Right(FileString, L - p1 + 4)
Print #2, FileString
Else: Print #2, FileString
End If
Else: Print #2, FileString
End If
Loop
Close #1
Close #2

Kill "C:\pass2ebs.txt"
Name "C:\pass.txt" As "C:\pass2ebs.txt"

'copy file from c-drive to gen folder
FileCopy "C:\pass2ebs.txt", "C:\Expedite\gen\pass2ebs.txt"
End Sub


Private Sub Command2_Click()
'Shell "C:\windows\notepad.exe"
Shell "c:\gen.exe", vbNormalFocus
End Sub
 
josie2007

Didn't I see that code in a different post somewhere? Did you solve this yet?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top