Checking whether external image file exists

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: heatherlaine, Klaus, FourthWorld, kevinmiller

Post Reply
montymay
Posts: 106
Joined: Thu Jul 18, 2013 5:23 am

Checking whether external image file exists

Post by montymay » Wed Jul 18, 2018 11:47 am

Hello LC developers:

I have having trouble writing the code that checks whether an image file exists in a folder. Using Windows 10 and LC 8.1.1. Let's say the image is "cat.png" and the image object on my card is image "animal." The following code for checking the existence of a folder works great:

Code: Select all

put "cat" into tImage
put "c:/Users/myName/Documents/Images/" into tFolder
if there is a folder tFolder then
set the filename of img "animal" to tFolder&tImage&".png"
end if
but substituting "file" for "folder" does not work:

Code: Select all

put "cat" into tImage
put "c:/Users/myName/Documents/Images/" into tFolder
if there is a file tFolder&tImage&".png" then
set the filename of img "animal" to tFolder&tImage&".png"
end if
Is it my code or a glitch in my system? Experimented with substituting "binfile" for "file" without success. Online dictionary entry for "there is a" didn't help. If referencing images doesn't work, I guess I will have to import them. Thank you for your response(s).

Monty

Klaus
Posts: 10291
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Checking whether external image file exists

Post by Klaus » Wed Jul 18, 2018 11:57 am

Hi Monts,

the magic word is BRACKETS! :D

Either do this, no brackets required:

Code: Select all

...
put "cat" into tImage
put "c:/Users/myName/Documents/Images/" into tFolder
put tFolder & tImage & ".png" into tImageFile
if there is a file tImageFile then
   set the filename of img "animal" to tImageFile
...
Or, with brackets:

Code: Select all

...
put "cat" into tImage
put "c:/Users/myName/Documents/Images/" into tFolder
if there is a file (tFolder & tImage & ".png") then
   set the filename of img "animal" to (tFolder & tImage & ".png")
...
This way the engine is forced to evaluate the "expression" in brackets first,
which results in a valid pathname.

Best

Klaus

montymay
Posts: 106
Joined: Thu Jul 18, 2013 5:23 am

Re: Checking whether external image file exists

Post by montymay » Wed Jul 18, 2018 12:27 pm

Darn! I now remember I had the same problem months ago and learned the need for brackets! I even noted somewhere that the dictionary entry for "there is a " should be revised to mention the the need for them (unless you use the alternative method). Thanks for your help, Klaus.

Monty

bogs
Posts: 2080
Joined: Sat Feb 25, 2017 10:45 pm

Re: Checking whether external image file exists

Post by bogs » Wed Jul 18, 2018 3:02 pm

Hm... if Monty already has the folder part working, couldn't he just look for the file in 'the Files' list for that folder?

Code: Select all

(set the folder in the path to the current folder)
put ".png" after tmpImage
if tmpImage is among the lines of the files then
	// your code to deal with the image
end if
Image

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”