Liberty BASIC Community Forum
« File existence checking in FOR..NEXT loop »

Welcome Guest. Please Login or Register.
Dec 12th, 2017, 08:11am


Rules|Home|Help|Search|Recent Posts|Notification


« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: File existence checking in FOR..NEXT loop  (Read 90 times)
flotul
New Member
Image


member is offline

Avatar




Homepage PM

Gender: Male
Posts: 32
xx File existence checking in FOR..NEXT loop
« Thread started on: Dec 4th, 2017, 12:22pm »

Hi,

I'm trying to check the existence of multiple files, all in the same directory, where only a number changes in their name.

The files list looks like this:
MyFile01test.bmp
MyFile02test.bmp
..
MyFile08test.bmp
MyFile09test.bmp

Basically, what I want to do is this:
Code:
IF fileExists(DefaultDir$, "MyFile01test.bmp") = 0 THEN [ALERT]
IF fileExists(DefaultDir$, "MyFile02test.bmp") = 0 THEN [ALERT]
..
IF fileExists(DefaultDir$, "MyFile08test.bmp") = 0 THEN [ALERT]
IF fileExists(DefaultDir$, "MyFile09test.bmp") = 0 THEN [ALERT] 



My actual non-working code is here and for any reason, the "index" will not increment:
Code:
FOR index = 1 TO 9
   IF fileExists(DefaultDir$, "MyFile0" + STR$(index) + "test.bmp") = 0 THEN [ALERT]
NEXT index 


Well, the question is: why is "index" not incrementing?

User IP Logged

Roger
Brandon Parker
Moderator
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1120
xx Re: File existence checking in FOR..NEXT loop
« Reply #1 on: Dec 4th, 2017, 12:58pm »

When I run this code below (slightly modified from what you have) the filename is incriminating correctly with index on my system....

Code:
Dim Info$(10, 10)

For index = 1 To 9
    Print index
    IF fileExists(DefaultDir$, "MyFile0" + STR$(index) + "test.bmp") = 0 Then
        Print "Does Not Exist"
    Else
        Print "Exists"
    End If
Next index

Function fileExists(path$, filename$)
    Print filename$
    Files path$, filename$, Info$()
    fileExists = Val(Info$(0, 0))
    ReDim Info$(0, 0)
End Function 



{:0)

Brandon Parker
« Last Edit: Dec 4th, 2017, 1:01pm by Brandon Parker » User IP Logged

Windows 7 Home Premium 64-bit Intel(R) Quad Core(TM) i5 CPU M 430 @ 2.27GHz 4GB DDR3 RAM
flotul
New Member
Image


member is offline

Avatar




Homepage PM

Gender: Male
Posts: 32
xx Re: File existence checking in FOR..NEXT loop
« Reply #2 on: Dec 4th, 2017, 1:32pm »

Thanks Brandon,

Yup, your code works too on my system. I'll try to adapt it to my needs wink

Two questions now:

1.- any idea why my original code doesn't work?

2.- the fileExists function I found on the web doesn't have the ReDim statement. What is it meant for please?
User IP Logged

Roger
flotul
New Member
Image


member is offline

Avatar




Homepage PM

Gender: Male
Posts: 32
xx Re: File existence checking in FOR..NEXT loop
« Reply #3 on: Dec 4th, 2017, 1:56pm »

After around 20 (successful) launches, I get a "No room" error message.

I notice something strange with my system's memory usage.

Each time I launch my program, the memory's usage will increase by around 10MB and doesn't go down when the program is stopped.

It looks like the fileExists function doesn't "release" memory after been used.

Possible?
User IP Logged

Roger
Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5585
xx Re: File existence checking in FOR..NEXT loop
« Reply #4 on: Dec 4th, 2017, 2:09pm »

You seem to be jumping out a loop conditionally. For next needs exit for to perform correctly.
User IP Logged

flotul
New Member
Image


member is offline

Avatar




Homepage PM

Gender: Male
Posts: 32
xx Re: File existence checking in FOR..NEXT loop
« Reply #5 on: Dec 4th, 2017, 2:59pm »

Thanks Rod,

I corrected accordingly and now it is fine wink
User IP Logged

Roger
Brandon Parker
Moderator
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1120
xx Re: File existence checking in FOR..NEXT loop
« Reply #6 on: Dec 5th, 2017, 2:40pm »

Great catch Rod!!

I didn't even pay attention to the [ALERT] Branch Label.

As Rod mentioned you should NEVER jump out of any loop using a Branch Label. Always exit the loop using the approved Exit xxxxx command. Jumping out of a loop corrupts the stack.

Also, with respect to the ReDim statement for the Info$() array within the fileExists() function. I always redimension the Info$() array to (0, 0) after the function serves its purpose. This performs two functions; first it helps lower the the amount of memory that LB might be consuming (albeit a very small gain for this) and second this ensure that the Info$() array will not have random information in it at some other point in my programs where I might be checking other elements of the Info$() array for various file attributes. I hope that makes sense.

{:0)

Brandon Parker
User IP Logged

Windows 7 Home Premium 64-bit Intel(R) Quad Core(TM) i5 CPU M 430 @ 2.27GHz 4GB DDR3 RAM
Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

Rules|Home|Help|Search|Recent Posts|Notification

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls