Preventing a data file from being overwritten

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Josh
Posts: 46
Joined: Sat Jan 12, 2008 6:42 am

Preventing a data file from being overwritten

Post by Josh » Mon Feb 04, 2008 8:21 am

Hi all,

I have code like this:

put true into idNotValid
repeat while idNotValid
repeat until it is an integer
ask "What is the student ID number?"
end repeat
put It into ident
if ident is not empty then answer "Is"&&ident&&"the correct ident
number?" with "No" or "Yes"
if It is "Yes" then put false into idNotValid
end repeat

Up to here works fine. However, I want to ensure that data files do not get overwritten. I don't want to make new files read only. I just want to do a simple search to determine if there is a file with the same name already in the same directory (which will always be the same path as the running application).

I tried something like this, but to no avail...


if there is a file "R" & ident & ".txt" then answer "File already exits! Overwrite?" with "yes" or "no"
if it is "yes" then put false into fileExists
if it is "no" then put true into fileExists
if there is not a file "R" & ident & ".txt" then put false into FileExists
if fileExists then go to card "begin"

put R & ident & ".txt" into filename

Any help would be appreciated.

Josh

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Mon Feb 04, 2008 11:37 am

Hi Josh,

It would be better to post the actual script rather than "code like this". The following should always work:

Code: Select all

on foo
  put "some data" into myData
  ask file "Choose a file" 
  put it into myFile
  if there is a file myFile then
    answer "Really?" with "Okay" or "Cancel"
    if it is "Cancel" then
      exit foo
    end if
  else
    write myData to file myFile
  end if
end foo
Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

Post Reply