FindFile function

Post Reply
User avatar
Dutchman
Posts: 907
Joined: Mon May 06, 2013 9:21 am
My devices: iMac, iPad Air, iPhone
Location: Netherlands
Flag: Netherlands

FindFile function

Post by Dutchman » Sun Nov 04, 2018 8:04 pm

I needed a function that can find a file in a folder.
I made FindFile$(Dir$, Search$, Ext$, Upsort)
• Searches file in folder 'Dir$' which name contains 'Search$' and ends with 'Ext$'.
• Search is case-insensitive
• It returns the latest matching name from the sorted folder listing
• Upsort = 1 results in Ascending sort, Upsort = 0 in Descending
The following code includes the test program:

Code: Select all

'Find file.sb by Dutchman, November 2018
'
'==== test variables
In$="Fractal" ' part of filename
Last$="txt" ' extension
Folder$="/Examples/Games" ' folder where to search

'==== Main
File$=FindFile$(Folder$,In$,Last$,1)
IF File$<>"" THEN
  PRINT "Found in ascending order: """&File$&"""." 
  File$=FindFile$(Folder$,In$,Last$,0)
  PRINT "Found in descending order: """&File$&"""." 
ELSE
  PRINT "No match found."
ENDIF
END

'=========== Function ===========
DEF FindFile$(Dir$,Search$,Ext$,Upsort) ' by Dutchman
'Search is case-insensitive
'Searches file in folder 'Dir$'
'   which name contains 'Search$'
'     and ends with 'Ext$'.
'It returns the latest matching name
'  from the sorted folder-listing
'Upsort=1 is Ascending, Upsort=0 is Descending
ob=OPTION_BASE()
Search$=LOWSTR$(Search$)
Ext$=LOWSTR$(REVERSE$(Ext$))'reverse search
IF Dir$="" THEN Dir$="."
File$=""
DIR Dir$ LIST FILES A$,n
OPTION SORT INSENSITIVE
IF Upsort THEN OPTION SORT ASCENDING ELSE OPTION SORT DESCENDING
SORT A$
FOR i=ob TO n-1+ob
  IF INSTR(LOWSTR$(a$(i)),Search$,ob)>=ob THEN
    IF Ext$<>"" THEN
      IF INSTR(LOWSTR$(REVERSE$(a$(i))),Ext$)=ob THEN File$=a$(i)
    ELSE ! File$=a$(i) ! ENDIF
  ENDIF
NEXT i
OPTION BASE ob
RETURN File$
END DEF
Output.JPG
Output.JPG (29.84 KiB) Viewed 1342 times
We consider it a refreshing mental exercise to figure out ways to circumvent foolishness (Quote from Richard)

User avatar
rbytes
Posts: 1963
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone X
iPad 4
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: FindFile functi

Post by rbytes » Sun Nov 04, 2018 10:47 pm

It is getting harder to remember where I put files. That could be a sign of advancing age, or just that my files and folders never stop increasing! This is a much-needed tool. Now if I can just remember what I named them! :lol:
Zzzzz

User avatar
GeorgeMcGinn
Posts: 495
Joined: Sat Sep 10, 2016 6:37 am
My devices: IPad Pro 10.5in
IMac
Location: Venice, FL
Flag: United States of America
Contact:

Re: FindFile functi

Post by GeorgeMcGinn » Wed Nov 14, 2018 1:43 pm

No, it's not old age. I don't know how many files I have that have more than one copy, but my documents and data size is 2.03GB!

I know that I have multiple copies of many programs (I re downloaded a lot just in case Mr. K. does go ahead and removes Dropbox access), I wanted to make sure most if not all my code is on my iPad. I still back everything to Dropbox as I have my alternates to Dropbox working (I have more than one place to save code, and for some of my Stackflow posts, I added a way to just capture other people's code).

It is not polished as when I presented this 4 times in the forum, no one cared to respond with suggestions or priorities to what I should provide, or even if they wanted this system!

This is something I was currently adding to the new version of your File Compare program (you input the whole or partial file name, it creates a file with the entire path and file name). Right now I'm looking at whether it makes sense to have the compare run all Files and produce a report, or allow you to select two files and run them.

I'll look at this tool as it also sounds what I need to get control of my files.

Thanks Ton for posting this program.

The
rbytes wrote:
Sun Nov 04, 2018 10:47 pm
It is getting harder to remember where I put files. That could be a sign of advancing age, or just that my files and folders never stop increasing! This is a much-needed tool. Now if I can just remember what I named them! :lol:
George McGinn
Computer Scientist/Cosmologist/Writer/Photographer
Member: IEEE, IEEE Computer Society
IEEE Sensors Council & IoT Technical Community
American Association for the Advancement of Science (AAAS)
https://www.georgemcginn.com
https://www.cosmologyandspace.blog

Post Reply