473,385 Members | 1,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Check and rename file variable

Hi, I’ve written a c# program to compact certain msaccess databases. The way
this works is to compact say C:\test1.mdb to C:\temp.mdb, then delete
C:\test1.mdb and rename C:\temp.mdb as C:\test1.mdb.

My problem occurs if there is already a C:\temp.mdb file. I don’t want to
just delete this, as it could have been created separately by a user.

In my code, I set a constant as follows:

private const string tempMdb = @"C:\temp.mdb";

To check that the file exists, I can use

if (File.Exists(tempMdb))...

but, if it is present, how do I change the tempMdb to a unique name?

I.e. if C:\temp.mdb is present, then rename as C:\temp1.mdb,
if C:\temp1.mdb is present, then rename as C:\temp2.mdb,
if C:\temp2.mdb is present, then rename as C:\temp3.mdb.....

I realise some sort of looping maybe required here but I’m not sure on the
best approach.

Thanks in advance.

Nov 16 '05 #1
5 2724
beaware that simply just using a File.Exists to check that a file exists
before you attempt to create a file can suffer from a race condition if some
other process creates the file between you checking for the existence of the
file and you create an instance of a file with the required filename.

HTH

Ollie Riches

"jez123456" <je*******@discussions.microsoft.com> wrote in message
news:91**********************************@microsof t.com...
Hi, I've written a c# program to compact certain msaccess databases. The way this works is to compact say C:\test1.mdb to C:\temp.mdb, then delete
C:\test1.mdb and rename C:\temp.mdb as C:\test1.mdb.

My problem occurs if there is already a C:\temp.mdb file. I don't want to
just delete this, as it could have been created separately by a user.

In my code, I set a constant as follows:

private const string tempMdb = @"C:\temp.mdb";

To check that the file exists, I can use

if (File.Exists(tempMdb))...

but, if it is present, how do I change the tempMdb to a unique name?

I.e. if C:\temp.mdb is present, then rename as C:\temp1.mdb,
if C:\temp1.mdb is present, then rename as C:\temp2.mdb,
if C:\temp2.mdb is present, then rename as C:\temp3.mdb.....

I realise some sort of looping maybe required here but I'm not sure on the
best approach.

Thanks in advance.

Nov 16 '05 #2
When going a file a unique name, I typically format the current date/time as
a string (yyyymmdd-hhmmss) and then append that to the file name. This
naming convention would be both unique and meaningful. For example:
temp_20050215-113045.mdb

"jez123456" <je*******@discussions.microsoft.com> wrote in message
news:91**********************************@microsof t.com...
Hi, I've written a c# program to compact certain msaccess databases. The way this works is to compact say C:\test1.mdb to C:\temp.mdb, then delete
C:\test1.mdb and rename C:\temp.mdb as C:\test1.mdb.

My problem occurs if there is already a C:\temp.mdb file. I don't want to
just delete this, as it could have been created separately by a user.

In my code, I set a constant as follows:

private const string tempMdb = @"C:\temp.mdb";

To check that the file exists, I can use

if (File.Exists(tempMdb))...

but, if it is present, how do I change the tempMdb to a unique name?

I.e. if C:\temp.mdb is present, then rename as C:\temp1.mdb,
if C:\temp1.mdb is present, then rename as C:\temp2.mdb,
if C:\temp2.mdb is present, then rename as C:\temp3.mdb.....

I realise some sort of looping maybe required here but I'm not sure on the
best approach.

Thanks in advance.

Nov 16 '05 #3
Check out System.IO.Path.GetTempFileName().

As this call will also create the file, you may have to delete it prior to
compacting.
Ex:

string dbPath = "myDatabase.mdb";
string tempPath = Path.GetTempFileName();
File.Delete(tempPath); //0 byte file created by GetTempFileName

//compact database

File.Delete(dbPath);
File.Move(tempPath, dbPath);

--
Adam Clauss
ca*****@tamu.edu
"jez123456" <je*******@discussions.microsoft.com> wrote in message
news:91**********************************@microsof t.com...
Hi, I've written a c# program to compact certain msaccess databases. The
way
this works is to compact say C:\test1.mdb to C:\temp.mdb, then delete
C:\test1.mdb and rename C:\temp.mdb as C:\test1.mdb.

My problem occurs if there is already a C:\temp.mdb file. I don't want to
just delete this, as it could have been created separately by a user.

In my code, I set a constant as follows:

private const string tempMdb = @"C:\temp.mdb";

To check that the file exists, I can use

if (File.Exists(tempMdb))...

but, if it is present, how do I change the tempMdb to a unique name?

I.e. if C:\temp.mdb is present, then rename as C:\temp1.mdb,
if C:\temp1.mdb is present, then rename as C:\temp2.mdb,
if C:\temp2.mdb is present, then rename as C:\temp3.mdb.....

I realise some sort of looping maybe required here but I'm not sure on the
best approach.

Thanks in advance.

Nov 16 '05 #4
Thanks for the help. I tried to use the GetTempFileName(), however the file
that is created is tmp450.tmp and I think it needs to be an mdb file.Is there
some way to use the GetTempFileName() method to create an access mdb file?

"Adam Clauss" wrote:
Check out System.IO.Path.GetTempFileName().

As this call will also create the file, you may have to delete it prior to
compacting.
Ex:

string dbPath = "myDatabase.mdb";
string tempPath = Path.GetTempFileName();
File.Delete(tempPath); //0 byte file created by GetTempFileName

//compact database

File.Delete(dbPath);
File.Move(tempPath, dbPath);

--
Adam Clauss
ca*****@tamu.edu
"jez123456" <je*******@discussions.microsoft.com> wrote in message
news:91**********************************@microsof t.com...
Hi, I've written a c# program to compact certain msaccess databases. The
way
this works is to compact say C:\test1.mdb to C:\temp.mdb, then delete
C:\test1.mdb and rename C:\temp.mdb as C:\test1.mdb.

My problem occurs if there is already a C:\temp.mdb file. I don't want to
just delete this, as it could have been created separately by a user.

In my code, I set a constant as follows:

private const string tempMdb = @"C:\temp.mdb";

To check that the file exists, I can use

if (File.Exists(tempMdb))...

but, if it is present, how do I change the tempMdb to a unique name?

I.e. if C:\temp.mdb is present, then rename as C:\temp1.mdb,
if C:\temp1.mdb is present, then rename as C:\temp2.mdb,
if C:\temp2.mdb is present, then rename as C:\temp3.mdb.....

I realise some sort of looping maybe required here but I'm not sure on the
best approach.

Thanks in advance.


Nov 16 '05 #5
It's ok, I've sussed it with
Path.ChangeExtension(Path.GetTempFileName(),"mdb") ;

"jez123456" wrote:
Thanks for the help. I tried to use the GetTempFileName(), however the file
that is created is tmp450.tmp and I think it needs to be an mdb file.Is there
some way to use the GetTempFileName() method to create an access mdb file?

"Adam Clauss" wrote:
Check out System.IO.Path.GetTempFileName().

As this call will also create the file, you may have to delete it prior to
compacting.
Ex:

string dbPath = "myDatabase.mdb";
string tempPath = Path.GetTempFileName();
File.Delete(tempPath); //0 byte file created by GetTempFileName

//compact database

File.Delete(dbPath);
File.Move(tempPath, dbPath);

--
Adam Clauss
ca*****@tamu.edu
"jez123456" <je*******@discussions.microsoft.com> wrote in message
news:91**********************************@microsof t.com...
Hi, I've written a c# program to compact certain msaccess databases. The
way
this works is to compact say C:\test1.mdb to C:\temp.mdb, then delete
C:\test1.mdb and rename C:\temp.mdb as C:\test1.mdb.

My problem occurs if there is already a C:\temp.mdb file. I don't want to
just delete this, as it could have been created separately by a user.

In my code, I set a constant as follows:

private const string tempMdb = @"C:\temp.mdb";

To check that the file exists, I can use

if (File.Exists(tempMdb))...

but, if it is present, how do I change the tempMdb to a unique name?

I.e. if C:\temp.mdb is present, then rename as C:\temp1.mdb,
if C:\temp1.mdb is present, then rename as C:\temp2.mdb,
if C:\temp2.mdb is present, then rename as C:\temp3.mdb.....

I realise some sort of looping maybe required here but I'm not sure on the
best approach.

Thanks in advance.


Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Dino | last post by:
dear all, i've created an application for a customer where the customer can upload ..csv-files into a specified ftp-directory. on the server, a php-script, triggered by a cronjob, reads all the...
12
by: Anders Eriksson | last post by:
Hello! I'm trying to create a program that will watch a directory and when a file is created print that file. I have used FileSystemWatcher for watching the directory and I get an created event....
1
by: Jay at SCA | last post by:
I've been having the following issue with a windows service I've created in vb.net (.net fw 1.1): Basically, my service monitors a folder for the creation of any new files of a particular type...
5
by: Tony Meyer | last post by:
On Windows, if I do os.rename(old, new) where old is a file that is in-use (e.g. python itself, or a dll that is loaded), I would expect that an error would be raised (e.g. as when os.remove is...
8
by: Merlin | last post by:
Ok.... I feel really dumb on this one, because I had previously figured it out, and now don't have a clue. I'm trying to get the user to input a new filename (via an integer variable) and...
9
by: Mark | last post by:
Hi all, This is something which has been bugging me for ages. How can I check if a file is already in use by a different program? It doesn't seem to matter which mode I pass to fopen, it will...
9
by: sc_wizard29 | last post by:
Hi everyone, Maybe these questions will sound strange to you, but I sometime have a hard time switching from Java to Python ;-) Let's say I have a function like this : def show_lines(file):...
1
by: spacehopper_man | last post by:
no "rename" operation in C# !!! - this has been covered in this group before, but I can't find any good answers. what I am trying to do is refresh the content in a file with minimum...
1
by: jonathan184 | last post by:
trying to rename filenames and extensions then add a header in line1 of each file if the header existed in line 1 to go to the next file. but i am getting error explciti errors Here is my...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.