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 » Wed Jan 13, 2016 9:39 am

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: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: How to list the files inside a folder

Post by jmburnod » Wed Jan 13, 2016 10:08 am

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: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: How to list the files inside a folder

Post by Klaus » Wed Jan 13, 2016 11:49 am

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: 10052
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: How to list the files inside a folder

Post by FourthWorld » Thu Jan 14, 2016 2:13 am

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: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: How to list the files inside a folder

Post by Klaus » Thu Jan 14, 2016 12:18 pm

Ah, good point, thanks Richard!

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

Re: How to list the files inside a folder

Post by jmburnod » Fri Jan 15, 2016 7:02 pm

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