[SOLVED] list of files with foldername on files in subfolder

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: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
tusa
Posts: 36
Joined: Sun Jan 31, 2016 10:30 pm

[SOLVED] list of files with foldername on files in subfolder

Post by tusa » Tue Oct 24, 2017 5:51 am

Hello

I have an open button, and when I click on it I can choose a folder and get a list of all the files in it, including subfolders.

Code: Select all

 # Pops up a dialog box letting the user select a folder
 answer folder "Vælg mappe"
 
 # Set the default folder to the folder selected by the user
 # (this is stored in the 'it' variable
 set the defaultfolder to it
 put the files into field "FileList"
 
 # Filter the lines in the list of all the files
 filter lines of field "FileList" with regex pattern "(?i)\.(ft|html)$"
If I want to show the folder and a slash on the files in a subfolder, how to do this?

Brg Tue.
Last edited by tusa on Tue Oct 24, 2017 10:38 pm, edited 1 time in total.

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: list of files with foldername on files in subfolder

Post by Klaus » Tue Oct 24, 2017 10:17 am

Hi tusa,

sincde version 8 (or maybe since version 7?) you do not need to set "the defaultfolder" anymore!
This will do:
...
answer folder "Vælg mappe"
put files(it) into field "FileList"
...

But I do not understand your actual question?
If I want to show the folder and a slash on the files in a subfolder,
Can you give a little example?


Best

Klaus

tusa
Posts: 36
Joined: Sun Jan 31, 2016 10:30 pm

Re: list of files with foldername on files in subfolder

Post by tusa » Tue Oct 24, 2017 2:23 pm

Right now my List i showing all files like this:
file_1.ft
file_2.ft
file_3.ft
file_in_subfolder_2.ft
file_in_subfolder_2.ft
file_in_subfolder_2.ft

I wan't it to show this:
file_1.ft
file_2.ft
file_3.ft
subfolder/file_in_subfolder_2.ft
subfolder/file_in_subfolder_2.ft
subfolder/file_in_subfolder_2.ft

Brg Tue.

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: list of files with foldername on files in subfolder

Post by Klaus » Tue Oct 24, 2017 2:35 pm

AHA! :D

Well, then you will need to loop throught hes folders in the "main" folder:

Code: Select all

on mouseUp 
   answer folder "Vælg mappe"
   
   ## ALWAYS check if user cklicked CANCEL!!
   if it = EMPTY then
      exit mouseup
   end if
   
   ## Only use IT once, since IT will change when you least exspect IT :-)
   put it into tMainFolder
   
   ## Now first collect all data before putting them into a field
   put files(tMainFolder) into tMainFiles
   put folders(tMainFolder) into tFolders
   
   ## Loop through all subfolders
   repeat for each line tFolder in tFolders
      put files (tMainFolder & "/" & tFolder) into tFiles
      
      ## Now loop through all files and create your pathname
      repeat for each line tFile in tFiles
         put tFolder & "/" & tFile & CR after tMainFiles
      end repeat
   end repeat
   
   ## Get rid of last CR
   delete char -1 of tMainFiles
   put tMainFiles into field "FileList"
end mouseUp
Tested and works! 8)

Best

Klaus

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: list of files with foldername on files in subfolder

Post by jmburnod » Tue Oct 24, 2017 2:43 pm

Hi tusa,
I use this:

Code: Select all

on getMyFiles
   answer folder "Select a folder"
   if it = empty then  exit getMyFiles
   put empty into rMyFiles
   put it into tPathFol
   put  LesFilesInFol(tPathFol) into tAllFiles
   put LesFolInFol(tPathFol) into tAllSubFol
   repeat for each line tOneFile in tAllFiles
      put tPathFol & "/" & tOneFile  & cr after rMyFiles
   end repeat
   repeat for each line tOneFol in tAllSubFol
      put tPathFol & "/" & tAllSubFol  into tPathFol
      put  LesFilesInFol(tPathFol) into tAllFiles
      repeat for each line tOneFile in tAllFiles
         put tPathFol & "/" & tOneFile  & cr after rMyFiles
      end repeat
   end repeat
   put rMyFiles
end getMyFiles

--•• -- folders in a folder
function LesFolInFol tFol 
   put empty into rLesFol
   put the defaultFolder into tOldDF
   set the defaultFolder to tFol
   put the folders into rLesFol
   set the defaultFolder to tOldDF
   filter rLesFol without ".*"
   return rLesFol
end LesFolInFol

function LesFilesInFol tFol -- files in a folder without ".*"
   put empty into rLesFilesInFol
   if there is a folder tFol then
      put the defaultFolder into tOldDF
      set the defaultFolder to tFol
      put the files into rLesFilesInFol
       set the defaultFolder to tOldDF
      filter rLesFilesInFol without ".*"
   end if
   return rLesFilesInFol
end LesFilesInFol
Best regards
Jean-Marc
https://alternatic.ch

tusa
Posts: 36
Joined: Sun Jan 31, 2016 10:30 pm

Re: list of files with foldername on files in subfolder

Post by tusa » Tue Oct 24, 2017 8:56 pm

This was my solution, that works just the way I expect:

Code: Select all

     # Pops up a dialog box letting the user select a folder
     answer folder "Vælg mappe"
     
     ## Check if user cklicked CANCEL!!
     if it = EMPTY then
        exit menuPick
     end if
     
     # Set the default folder to the folder selected by the user
     # (this is stored in the 'it' variable
     put it into tMainFolder
     
     ## Now first collect all data before putting them into a field
     put files(tMainFolder) into tMainFiles
     put folders(tMainFolder) into tMainFolders
     
     ## Loop through all subfolders
   repeat for each line tFolder in tMainFolders
      put files (tMainFolder & "/" & tFolder) into tFiles
      
      ## Now loop through all files and create your pathname
      repeat for each line tFile in tFiles
         put tFolder & "/" & tFile & CR after tMainFiles
      end repeat
   end repeat
   
   put tMainFiles into field "FileList"
 
   # Filter the lines in the list of all the files
   filter lines of field "FileList" with regex pattern "(?i)\.(ft|html)$"
Now I just need to filter the files and then I want to remove the file extensions, on the files I show in the list
The filter part is working now, I only show .ft and .html files, but still missing the last part with the extensions.

Brg Tue.

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: list of files with foldername on files in subfolder

Post by Klaus » Tue Oct 24, 2017 9:25 pm

Hi tusa,

I would simply:

Code: Select all

...
replace ".html" with EMPTY in fld "FileList"
replace ".ft" with EMPTY in fld "FileList"
...
But only because I have no idea of regex :D


Best

Klaus

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

Re: list of files with foldername on files in subfolder

Post by bogs » Tue Oct 24, 2017 9:48 pm

Klaus wrote:
Tue Oct 24, 2017 9:25 pm
But only because I have no idea of regex :D
:shock:
Oh wait, what am I shocked about? Neither do I :P
I would think though that you could do something like (not tested)

Code: Select all

if (x) contains ".html" or ".ft" then etc etc.  
but I might be grossly oversimplifying it.
Image

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7389
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: list of files with foldername on files in subfolder

Post by jacque » Tue Oct 24, 2017 10:00 pm

Here are a couple of other solutions from my collected library.

Code: Select all

function walkDir dirPath
  -- This recursive function expects a folder path.
  -- It returns a file list for that folder and for each
  --  sub-folder it contains (pre-order search)
  -- Invisible files are excluded.
  -- David Vaughan's version; accounts for directory permissions

  if dirPath contains "//Network" then
    -- avoids specific case of alias looping; other aliases handled okay below
    return empty
  end if

  put empty into tList
  set defaultFolder to dirPath
  -- Dar's discovery. Check permissions were ok
  get the Result
  if it is not empty then
    -- subtract 1 from isDepth
    return empty
  end if
  put the long files into fList
  repeat for each line fLine in fList
    if char 1 of fLine <> "." then
      put item 1 of fLine & comma & last item of fLine into fData
      put dirPath & "/" & fData & return after tList
    end if
  end repeat
  get the folders
  repeat for each line x in it
    if char 1 of x <> "." then
      put walkDir(dirPath & "/" & x) after tList
    end if
  end repeat
  -- subtract 1 from isDepth
  return tList
end walkDir
-- Even better, use shell. This is very fast:

Code: Select all

on mouseUp
  put empty into field "result"
  answer folder "Pick a folder you want to walk:"
  if it is empty then exit mouseUp
  if last char of it <> "/" then put "/" after it
  put walkDir(it) into field "result"
end mouseUp

function walkDir dirPath
  set the directory to dirPath
  return shell("ls -R")   
end walkDir
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

tusa
Posts: 36
Joined: Sun Jan 31, 2016 10:30 pm

Re: list of files with foldername on files in subfolder

Post by tusa » Tue Oct 24, 2017 10:37 pm

Klaus wrote:
Tue Oct 24, 2017 9:25 pm
Hi tusa,

I would simply:

Code: Select all

...
replace ".html" with EMPTY in fld "FileList"
replace ".ft" with EMPTY in fld "FileList"
...
But only because I have no idea of regex :D


Best

Klaus
So simple, but it works!
Thank you.

Post Reply