[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

How do I over write a CSV file without getting errors?

Posted on 2008-01-24
9
Medium Priority
?
858 Views
Last Modified: 2013-12-25
When I try to rename a file I get an error that says 'file already exists'. Is there any way that I can force it to over write the existing file?
I know I can do it by using this code:
newFileName = "C:\Program Files\Microsoft Visual Studio\VB98\Projects\SystemIDs\filename.csv"
Kill newFileName
But I would prefer to just force it to over write it by using the code below.

Thanks
oldFileName = "C:\Program Files\Microsoft Visual Studio\VB98\Projects\SystemIDs\SystemName.txt"
newFileName = "C:\Program Files\Microsoft Visual Studio\VB98\Projects\SystemIDs\SystemName.csv"
    
Name oldFileName As newFileName

Open in new window

0
Comment
Question by:rfgraham
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 10

Expert Comment

by:dis1931
ID: 20738992
Hello rfgraham,

what function are you using to create the file?

Thanks,

dis1931
0
 
LVL 5

Expert Comment

by:23637269
ID: 20739001
What operating system and rights for the folder containing the file you want to rename?
0
 
LVL 29

Expert Comment

by:chilternPC
ID: 20739007
you have to check for its existance (the file that is) and open it and overwrite it.

so I assume the code will  test for it being there, if not then create the file otherwise open the file and overwrite
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 10

Expert Comment

by:dis1931
ID: 20739052
I can't remember the functions I've used but some built in file creation functions can be passed a parameter to overwrite file if it exists...
0
 

Author Comment

by:rfgraham
ID: 20739152
Basically I open a text file and write to it in csv format, I then close it and rename it as a csv file.
The function I'm using to create is: Open "C:\filename.txt" For Append As #1
finalData = rs(0) & "," & rs(1)
Print #1, finalData
rs.MoveNext
Wend
Close #1
rs.Close
Set rs = Nothing
oldFileName = "C:\filename.txt"
newFileName = "C:\filename.csv"     'I get error at this point, 'file already exists'.

I'm running XP Pro with admin priv

Thanks
0
 
LVL 29

Expert Comment

by:chilternPC
ID: 20739198
well, on error delete file thne carry on.
0
 
LVL 5

Accepted Solution

by:
23637269 earned 1000 total points
ID: 20740020
If the error is what is the problem and the files are updated properly.
You could write your own custom error trap and control if the program stops or not and how it handles errors.

When VB6 was new many people had problems with File I/O and wrote custom wrappers to handle Opening, Closing, Deleting, etc..  If you want a copy of the one I wrote let me know.

Although it is not considered good code you could use the 'On Error Resume Next' which tells VB to go until the Blue Screen of Death sez different.  If you do that make sure you remember to add 'On Error Goto 0' before the procedure or function ends to turn error checking back on.
0
 

Author Comment

by:rfgraham
ID: 20740575
I went ahead and created an error trap, this is the code I used:

Dim fso As New FileSystemObject, fil1

On Error GoTo ErrorHandler
Set fil1 = fso.GetFile("C:\filename.csv")
fil1.Delete
oldFileName = "C:\filename.txt"
newFileName = "C:\filename.csv"
Name oldFileName As newFileName
Exit Sub

ErrorHandler:
Dim strError As String
strError = ("filename.csv file must exist in working directory ")
MsgBox strError

End Sub

Thanks for your help - 23637269:
0
 
LVL 10

Expert Comment

by:dis1931
ID: 20744365
"Open "C:\filename.csv" For Output As #1"

should also have worked...you can write to a .txt extension just as you can a .csv extension no need to rename just write to the filename.csv file with Output instead of Append.  Output will overwrite existing file, while append will write to the next line of the file if it exists or create a new one if it doesn't exist.

I know you accepted already but this could help someone else...


0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month8 days, 11 hours left to enroll

613 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