DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: creating a file if it does not exist, but not wipe it if it does?

  1. #1
    Join Date
    Nov 2004
    Location
    Lincoln, England
    Posts
    100

    Lightbulb creating a file if it does not exist, but not wipe it if it does?

    Hey Guys,

    I'm making a programme at the minute that requires a password to be stored in a text file. When the form to enter the password loads, it runs a length check on the file to see if there's actually a password in it. My problem is that I retrieve the data with the input statement, but if the file doesnt actually exist then this brings up the error (i know why thats not a problem).
    To get around this error i have the code as follows:

    Code:
    Dim filenumber as integer
    Filenumber = FreeFile()
    Open app.path & "\file.ext" for append as #filenumber
    close #Filenumber
    open app.path & "\file.ext" for input as #filenumber
    ***Code to extract data goes here***
    Now I have to add the append line so that if the file does not exist, it is created, but if it does exist, the file is not altered. Now to me, despite the fact it does what it's supposed to, it looks stupid. Is there an easier method to create a file that doesnt exist, but if it does exist, not wipe it?

    Many Thanks in advance,

    el__sid
    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

  2. #2
    Join Date
    Dec 2004
    Posts
    110
    You could just use an 'if' statement.

    There is some code on this site in the freevbcode section that has a function 'pathexists', which will tell you if the file you are trying to open already exits. Then you and if or select case.

    I think you could probably do the same with an FileLen() statement, and see if it returns an error??

  3. #3
    Join Date
    Feb 2004
    Location
    Sydney, Australia
    Posts
    498
    See Dir - returns zero length string if file does not exist

  4. #4
    Join Date
    Nov 2004
    Location
    Lincoln, England
    Posts
    100
    will look into this, thanks
    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center