Counting Files in a Folder

Moderators: LCNeil, heatherlaine, kevinmiller, elanorb

Post Reply
pTom
Posts: 40
Joined: Wed Sep 18, 2013 10:44 pm

Counting Files in a Folder

Post by pTom » Fri Oct 18, 2013 8:43 pm

Another question...

I see in Livecode Lessons, it talks about listing the files in the folder. How do I actually get the number of files (how many files) are in a folder. I want to use that number to create new names for each of my files.

For example, I am uploading a file (text or video) and want to count the number of files in the destination folder, and then +1 to the number and use that new number as the name of the file. Makes sense?

I'll keep looking and let you know if I discover the answer.

Also looking forward to getting a response on this!

Thanks!

Tom

LCNeil
Livecode Staff Member
Livecode Staff Member
Posts: 1223
Joined: Wed Oct 03, 2012 4:07 pm

Re: Counting Files in a Folder

Post by LCNeil » Mon Oct 21, 2013 12:59 pm

Hi Tom,

The way that you return the list of files from a web directory is slightly different compared to the desktop environment.

I have attached a sample stack that shows one method of doing this with files hosted on on-rev.

The script used is within the "Get File List From Web Directory" button

Kind Regards,

Neil Roger

--
RunRev Support Team ~ http://www.runrev.com
--
Attachments
Tom Web Directory.livecode.zip
(1.3 KiB) Downloaded 79 times

pTom
Posts: 40
Joined: Wed Sep 18, 2013 10:44 pm

Re: Counting Files in a Folder

Post by pTom » Mon Oct 21, 2013 1:14 pm

Hi Neil

I looked at your code, but not quite sure it is doing what I am thinking. I want to count the number of files in the folder, not list them. I want to use that total number (+1) as part of the name for the new folder. So, for example, if there are 6 files in the folder, the new file would be named "7file.mov". And then the next file I would load would be named "8file.mov" and then the next file "9file.mov" and so on.

Here is the code I used after searching the forum last Friday:

Code: Select all

--Get # of files and use that to rename uploaded file to appropriate namign convention
   put url "http://lifetree.on-rev.com/public_html/3M4G/3M4GVid" into tFolderToSearch 
   
   put the defaultfolder into tOldDefaultFolder -- to restore it later
   set the defaultfolder to tFolderToSearch
   put the files into tAllFiles -- gives you all the files of the folder
   -- you may want to filter the files for the type you want; look up filter with or filter without
   --filter tAllFiles without ".*"  -- on a mac there are invisible files that start with a dot
   --filter tAllFiles with "*.rev" -- to just get rev stacks
   --filter tAllFiles with "*.pdf" -- to just get pdfs
   set the defaultfolder to tOldDefaultFolder -- restore the default folder, not necessary but nice
   put 0 into tSoMany -- just in case there are none
   put the number of lines of tAllFiles into tSoMany
   
   --set the value of totalNumber to
   
   --set the startValue of addNumber to 1
   --put 1 into addNumber
   --set the value of addNumber to 1 (problem with this expression)
   --add addNumber to tSoMany
   --add 1 to tSoMany
   
   --Load file to server
   put field "fileField" into tLoadFile
   set the itemdel to "."
   put the last item of tLoadFile into tFileName
   
   put "ftp://" & "Username"  & ":" & "Password" & "@" & "lifetree.on-rev.com/public_html/3M4G/3M4GVid/" & tSoMany & "3M4GV" & "." & tFileName into tDestination
   libURLSetStatusCallback "uploadProgress", the long ID of me
   libURLftpUploadFile loadFile, tDestination, "uploadComplete"
I want to increment the name of each new file but just 1 digit. Hope that makes sense! lol

Thanks!

Tom

LCNeil
Livecode Staff Member
Livecode Staff Member
Posts: 1223
Joined: Wed Oct 03, 2012 4:07 pm

Re: Counting Files in a Folder

Post by LCNeil » Mon Oct 21, 2013 1:54 pm

Hi Tom,

The method you are using will not work as I believe you cannot set the "defaultfolder" to reference an online directory. As this is the case you would need to use the method In the stack I provided to get a list of the items on a web directory.

With that being said, you could still use the naming convention that you want to implement via this mehtod. e.g. you could get the number of lines in the returned file list, the number of lines in this list the number of files in the directory, so you could add 1 to this number and use it when uploading a new file to your on-rev account.

I have updated my example stack to show this.

In the example stack, there are 9 items returned from my online directory. I retrieve this value from the list field, add 1 to it and then place this into the "name field"

I also notice that you are using http://lifetree.on-rev.com/public_html/3M4G/3M4GVid for your directory. I would recommend that you change this to http://lifetree.on-rev.com/3M4G/3M4GVid as this will otherwise result in a URL not found message.

Kind Regards,

Neil Roger

--
RunRev Support Team ~ http://www.runrev.com
--
Attachments
Tom Web Directory.livecode 2.zip
(1.64 KiB) Downloaded 77 times

pTom
Posts: 40
Joined: Wed Sep 18, 2013 10:44 pm

Re: Counting Files in a Folder

Post by pTom » Mon Oct 21, 2013 3:34 pm

Quick question:
In your code, you are deleting lines 1-8 and -3to-1...why?

And why is itemdel set to quote?
(I am used to itemdel being set to "/" or "." but not quote)

I am not following the logic behind both of those steps...

pTom
Posts: 40
Joined: Wed Sep 18, 2013 10:44 pm

Re: Counting Files in a Folder

Post by pTom » Mon Oct 21, 2013 3:35 pm

Also, I would like to avoid the step of putting anything in to a list...just want to extract number of files in folder....??

LCNeil
Livecode Staff Member
Livecode Staff Member
Posts: 1223
Joined: Wed Oct 03, 2012 4:07 pm

Re: Counting Files in a Folder

Post by LCNeil » Mon Oct 21, 2013 3:53 pm

Hi Tom,

You do not need to put anything in a list and you could use a variable for the same function. I was only using a list as a visual representation.

I am deleting lines 1-8 & -3 to -1 as this is data that we do not need to use that is returned from the put URL "http://techsupport.on-rev.com/cloud/AudioAssets/" call.

When we use PUT URL we are returning the source of the webpage we are accessing. Removing this data just makes it easier to work with.

I also set the item delimiter to quote as this is how the files are separated in the webpage source. This allows me to extract a list of these files from the source.

The attached picture shows the source of "http://lifetree.on-rev.com/3M4G/3M4GVid/ and how my script extracts the data-
Screen Shot 2013-10-21 at 15.45.33.png
The red blocks are the deleting lines 1-8 and -3 to -1 ( however after posting this it should be -2 to -1) and the arrow indicates the item delimiter of quote.

Kind Regards,

Neil Roger

--
RunRev Support Team ~ http://www.runrev.com
--

pTom
Posts: 40
Joined: Wed Sep 18, 2013 10:44 pm

Re: Counting Files in a Folder

Post by pTom » Mon Oct 21, 2013 3:55 pm

Ok, so I reworked the code for my situation (not listing the files anywhere) and tried it out. It worked the first time (I think) but it won't do it a 2nd time.

The folder I was loading into was empty. I selected a jpg to load and it loaded it & renamed it "2file.jpg". Not sure why it gave it the number "2" instead of "1" since there was not any files in there but nonetheless, I thought it may be working. So I loaded another file and nothing happened. Either it didn't load the file or it gave the file the exact same name ("2file.jpg") as the first file I loaded...ideas as to what is happening here?

LCNeil
Livecode Staff Member
Livecode Staff Member
Posts: 1223
Joined: Wed Oct 03, 2012 4:07 pm

Re: Counting Files in a Folder

Post by LCNeil » Mon Oct 21, 2013 4:02 pm

Hi Tom,

Could you send me your stack and I will have a look at it.? Having your stack and scripts in front of me will help answer your question.

Kind Regards,

Neil Roger

--
RunRev Support Team ~ http://www.runrev.com
--

pTom
Posts: 40
Joined: Wed Sep 18, 2013 10:44 pm

Re: Counting Files in a Folder

Post by pTom » Tue Oct 22, 2013 4:47 am

Hi Neil

I figured it out! Woohoo!

Now, my issue is that I would like the assigned number to format using 6 digits (xxxxxx3M4GV.mov).
For example 0000013M4GV.mov 0000023M4GV.mov 0000033M4GV.mov 0000043M4GV.mov and so on like that

I don't see anything in the forums on this. Any ideas?

pTom
Posts: 40
Joined: Wed Sep 18, 2013 10:44 pm

Re: Counting Files in a Folder

Post by pTom » Tue Oct 22, 2013 4:55 am

Actually, I realized I don't need to do that. So, we are all good on this one.

Thanks for your help, Neil!

hanson
Livecode Staff Member
Livecode Staff Member
Posts: 44
Joined: Thu Jun 16, 2011 9:55 am

Re: Counting Files in a Folder

Post by hanson » Wed Nov 06, 2013 7:22 pm

Hi Tom,

try the following code. This code should run from a button, but you can update it to run in other places too.
Change "/Users/me" to the path you are wanting to list. You can also add a filter by replacing "empty" with something like "*video*", if there us "video" in the file name.
We also filter out . and .., just in case you pass in empty rather than use a particular filter. The code is pretty flexible, so you should be able to tweak it to your needs.


on mouseUp
local tFiles
put listFiles ("/Users/me",empty) into tFiles
filter tFiles without "."
filter tFiles without ".."
answer the number of lines in tFiles
end mouseUp

function listFiles pFolder, pFilter
local tOldFolder
if there is a folder pFolder then
put the folder into tOldFolder
set the folder to pFolder
get the files
if pFilter is not empty then filter it with pFilter
set the folder to tOldFolder
return it
else
return empty
end if
end listFiles


Kind Regards,

Hanson

Post Reply

Return to “idea2app and Coding School”