How to list the files inside a folder

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
shalu
Posts: 72
Joined: Fri Mar 20, 2015 1:05 pm

How to list the files inside a folder

Post by shalu »

Hi ALL,

How to list the files inside a folder without the choose the location.
eg:
put"D:\test\an" into content

I want to list the content of "D:\test\an" into a variable is it possible . I am using windows system :(
--
Thanks
Shalu S
jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2734
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: How to list the files inside a folder

Post by jmburnod »

Hi Shalu,

This function return the list of files in a folder without ".*" :

Code: Select all

function getFilesInFol tFol
   put empty into rFilesInFol
   if there is a folder tFol then
      set the defaultFolder to tFol
      put the files into rFilesInFol
      filter rFilesInFol without ".*" 
   end if
   return rFilesInFol
end getFilesInFol
Best regards
Jean-Marc
https://alternatic.ch
Klaus
Posts: 14324
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: How to list the files inside a folder

Post by Klaus »

Hi all,

to avoid eventual surprises, I would add two lines:

Code: Select all

function getFilesInFol tFol
   put empty into rFilesInFol
   if there is a folder tFol then
      put the folder into tOldDir
      set the folder to tFol
      put the files into rFilesInFol
      filter rFilesInFol without ".*" 
      set the folder to tOldDir
   end if
   return rFilesInFol
end getFilesInFol
8)


Best

Klaus
FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10103
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: How to list the files inside a folder

Post by FourthWorld »

A lot of folks (including myself) have been caught with unexpected recursion issues due to folder permissions. So this can be helpful to add:

Code: Select all

set the folder to tFolder
if the folder is tFolder then -- do stuff we expect....
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Klaus
Posts: 14324
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: How to list the files inside a folder

Post by Klaus »

Ah, good point, thanks Richard!
jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2734
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: How to list the files inside a folder

Post by jmburnod »

Thanks Klaus
If i understand correctly what Richard suggest, this function should be like that :

Code: Select all

function getFilesInFol tFol
   put empty into rFilesInFol
   if there is a folder tFol then
      put the folder into tOldDir
      set the folder to tFol
      if the folder is tFol then -- do stuff we expect.... 
         put the files into rFilesInFol
         filter rFilesInFol without ".*" 
         set the folder to tOldDir
      end if
   end if
   return rFilesInFol
end getFilesInFol
All the best
Jean-Marc
https://alternatic.ch
Post Reply