PC Review


Reply
Thread Tools Rate Thread

Check and rename file variable

 
 
=?Utf-8?B?amV6MTIzNDU2?=
Guest
Posts: n/a
 
      15th Feb 2005
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.

 
Reply With Quote
 
 
 
 
Ollie Riches
Guest
Posts: n/a
 
      15th Feb 2005
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" <(E-Mail Removed)> wrote in message
news:91EFE650-AE5F-4AA6-9BB6-(E-Mail Removed)...
> 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.
>



 
Reply With Quote
 
JohnnyAppleseed
Guest
Posts: n/a
 
      15th Feb 2005
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" <(E-Mail Removed)> wrote in message
news:91EFE650-AE5F-4AA6-9BB6-(E-Mail Removed)...
> 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.
>



 
Reply With Quote
 
Adam Clauss
Guest
Posts: n/a
 
      15th Feb 2005
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
(E-Mail Removed)
"jez123456" <(E-Mail Removed)> wrote in message
news:91EFE650-AE5F-4AA6-9BB6-(E-Mail Removed)...
> 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.
>



 
Reply With Quote
 
=?Utf-8?B?amV6MTIzNDU2?=
Guest
Posts: n/a
 
      16th Feb 2005
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
> (E-Mail Removed)
> "jez123456" <(E-Mail Removed)> wrote in message
> news:91EFE650-AE5F-4AA6-9BB6-(E-Mail Removed)...
> > 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.
> >

>
>
>

 
Reply With Quote
 
=?Utf-8?B?amV6MTIzNDU2?=
Guest
Posts: n/a
 
      16th Feb 2005
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
> > (E-Mail Removed)
> > "jez123456" <(E-Mail Removed)> wrote in message
> > news:91EFE650-AE5F-4AA6-9BB6-(E-Mail Removed)...
> > > 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.
> > >

> >
> >
> >

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cannot create work file. Check temp environment variable Mauro Microsoft Outlook Discussion 1 21st May 2010 12:01 PM
Outlook 2007 cannot create temp file, please check tmp variable RadjaVista Microsoft Outlook Discussion 1 3rd Jan 2010 02:10 PM
Check for file in folder, if not there, open folder to rename file Don M. Microsoft Excel Programming 9 22nd Oct 2008 07:34 PM
Rename errorin Vista - multiple file rename James136 Windows Vista File Management 3 4th Apr 2008 10:01 AM
check and rename a incrementing txt file from a function brad Microsoft Access VBA Modules 1 22nd Dec 2003 06:52 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:42 PM.