curly
Full Member
Posts: 161
|
Post by curly on Apr 21, 2021 7:48:43 GMT -5
Hi, Really struggling to see if a file exists before trying to open it. Read all about the files command and assoiated help files, but still confused.
I've now deleted all the different bits of code I tried to check if a file existed in the target directory, but info$(0,0) returns 0 when I can see it exists?
The code I created was based upon this from the files help page
files "c:\", "config.bak", info$( - I've interpreted this as files "drive and directory", "filename.extention", info$( - I dimensioned info$(10,10) If val(info$(0, 0)) > 0, then the file exists. - the value of info$(0,0) always returns 0
|
|
|
Post by Brandon Parker on Apr 21, 2021 8:22:35 GMT -5
Try calling this function...
Sample call to function ...
Dim Info$(0,0) If fileExists("Your\Directory\Path\Here\", "Your_Filename_Here") Then 'Do something End If
Function ...
Function fileExists(path$, filename$) Files path$, filename$, Info$() fileExists = Val(Info$(0, 0)) ReDim Info$(0, 0) End Function
{:0)
Brandon Parker
|
|
|
Post by tsh73 on Apr 21, 2021 8:49:32 GMT -5
|
|
|
Post by Rod on Apr 21, 2021 9:26:31 GMT -5
As others have noted your .bac file is unlikely to be stored in c:\ That is the root directory. The file is likely in c:\basic or similar so you are missing the directory in the path statement.
|
|
|
Post by Walt Decker on Apr 21, 2021 13:40:54 GMT -5
Try this:
files StartupDir$, FileName$, info$()
or this
files DefaultDir$, FileName$, info$()
|
|
curly
Full Member
Posts: 161
|
Post by curly on Apr 22, 2021 3:58:16 GMT -5
Hi and thank you,I think my probem is that I do not fully understand the process of how the existance of a file is proven.
tsh73 and Rod,I'm pretty comfortable with filenames, extensions and directory paths, I just copied the text from the Liberty Basic University help page to indicate what I was trying to do. None of the variables in my programme are given values within the code for the programme. I have lots of small files that are opened after the programme starts and populates the many arrays used for calculations. This way I can edit the values if regulations change without touching the programme code to avoid accidentally changing something and introducing a bug. The problem is that if any one of the files is not where it should be, the programme will crash? All my data files are encrypted and work fine.
The filepath I am using to successfully open the files is, c:\At home\Liberty Basic\Data files and a typical filename is STG.dat. Each file is then unencrypted and loaded into its array.
Brandon, Does the Function part of your code need to be placed anywhere specific in my programme? e.g. I could create a subroutine called [Functions] to contain any other functions I find I need, and call the subroutine during booting up? I assume the first part is used immediately before each file is opened?
Again, many thanks for your continued patience, David
|
|
|
Post by Rod on Apr 22, 2021 6:55:33 GMT -5
Right, precision is everything. Instead of files use the print command to see what your arguments actually look like to Liberty. So print dir:\path\file name does it look exactly right?
|
|
|
Post by Rod on Apr 22, 2021 10:27:56 GMT -5
It isn’t black magic, if the dir\path\file spec you feed to the file command actually points to a file then info$(0,0) will be greater than zero. Info$(0,0) is filled with the number of files that match the dir\path\file criteria that you give files command. So if you give it an exact file spec and it exists you will get info$(0,0) equal to “1”
|
|
|
Post by tsh73 on Apr 22, 2021 12:33:42 GMT -5
Use same path to create file with similar name. Look in Explorer to see if new file appeared and does it look like you expect (yes I mean extension).
|
|
|
Post by Brandon Parker on Apr 22, 2021 16:54:33 GMT -5
Brandon, Does the Function part of your code need to be placed anywhere specific in my programme? e.g. I could create a subroutine called [Functions] to contain any other functions I find I need, and call the subroutine during booting up? I assume the first part is used immediately before each file is opened? Functions & Subroutines are separate pieces of code that you call routinely. They just need to be in your code-base somewhere; location is not terribly important, but as the code-base grows, I find it easier to have all Subroutines grouped together and all Functions grouped together. There's no need to place any Subroutine/Function inside a separate Subroutine. You can call a Subroutine/Function from anywhere in your code that you want to. Yes, you should check for a file's existence before attempting to do anything with it. {:0) Brandon Parker
|
|
|
Post by Rod on Apr 23, 2021 2:26:43 GMT -5
The point Tsh73 is making is that Windows can be set to hide file extensions. So if you use explorer to look at directories you don’t get shown the file extension. If this is set and you name a file my basic.bas it might actually be stored as my basic.bas.bas So one thing to do is check that you can see file extensions for everything that you see in explorer. If not use windows help to shoew all file extensions.
|
|
curly
Full Member
Posts: 161
|
Post by curly on Apr 24, 2021 6:23:57 GMT -5
EUREKA! I understand, I really understand, thank you.
Brandon, I misled you a little saying 'subroutine'. I'm now up to about 300 lines plus, and have been forced to have a sound structure to be able to understand my own program. I use the apostrophe to create labels, so have a label, 'start of #home GUI objects and 'end of #home GUI objects, 'start of general subroutines, etc, and have created lists of variables and arrays, and multiple flow charts. With all your help I'm feeling like I'm getting somewhere. Thank you all again, but I know I'll be back soon finding out how to create an installation module for users to install the program to their own PCs.
|
|
|
Post by mknarr on Apr 24, 2021 8:43:42 GMT -5
Curly, here is a little program I wrote some time ago. It will read your basic program and count the following: Arrays and size of each Filenames and number of times used Handles and number of times used Labels and number of times used Numeric variables and number of times used String variables and number of times used Gosub labels and number of times used Goto labels and number of times used Subs and Functions and variables used and the real number of working lines. You can display each and print each list if you want. It does have a help file and currently can handle a 50,000 line program which is easily changed. It works with any LB program 4.51 or earlier.
|
|
curly
Full Member
Posts: 161
|
Post by curly on Apr 26, 2021 3:42:26 GMT -5
Hi mknarr, thank you, I will be running it later, and regularly, to check I have everything listed.
You may all have noted my typo, I meant 3000 lines plus, not 300.
It's all going well at the moment, but........
|
|