Counting number of files in folder recursivley?

887 views
Skip to first unread message

Dave Weil

unread,
Jan 30, 2015, 2:48:54 PM1/30/15
to robotframe...@googlegroups.com
Aloha All,

I searched and searched before posting as I do not want to ask something that has already been asked, but i found nothing. Sorry if this has been asked previously.

I am attempting to perform file counts on folders so I can rename the folder with the number of files it contains.

So I have something like:
-Main Archive Folder
   -Regression Files (folder root contains 5 files)
      -Test 1 (contains 10 files)
      -Test 2 (contains 10 files)
      -Test 3 (contains 5 files)
   -Progression Files
      -Test 1 (contains 10 files)
      -Test 2 (contains 10 files)
      -Test 3 (contains 5 files)

I currently create a list of all folders in the 'Main Archive Folder' which is my @{sourcepath}

@{listoffolders}    Create list                   
@{listoffolders}    List Directories In Directory    ${sourcepath}               

I then loop through each folder counting the files in the root:
: FOR    ${folder}    IN    @{listoffolders}           
    Set test variable    ${folderfilecount}    0           
    ${filesinfolder}    Count Files In Directory    ${sourcepath}\\${folder}           
    ${folderfilecount}    Evaluate    ${folderfilecount}+${filesinfolder}           
    Set test variable    ${folderfilecount}    ${folderfilecount}           

So now I have a count for the 'Regression Files' folder. I then set the current folder to a variable and use that in a new keyword that looks for any sub-folders:
    Set test variable    ${subsourcepath}    ${sourcepath}\\${folder}           
    Run keyword    Test Data Repository Department Files (Count Sub-Folders)               

#Test Data Repository Department Files (Count Sub-Folders)
@{subfolder}    Create List           
@{subfolder}    List Directories In Directory    ${subsourcepath}       
: FOR    ${folder}    IN    @{subfolder}   
    ${filesinfolder}    Count Files In Directory    ${subsourcepath}\\${folder}   
    ${folderfilecount}    Evaluate    ${folderfilecount}+${filesinfolder}   
    Set test variable    ${folderfilecount}    ${folderfilecount}   

After all this I can rename the folder to   Regression Files (30 files).

This works great for folders with sub-folders that do not also contain folders. If the main folder has folders more than 1 deep I do not get any counts from those deeper folders. I am just hoping there is some way to just say give me the count of all the files in ${sourcepath} resursively.


We literally have folders like:

-Main Archive Folder
   -Archived Tests
       -2014
           -January
               -XML regression test (and this has many sub-folders)

So I would have to continue to check and check and check for sub-folders. I have only been using Robot for about a year so I still have a lot to learn.

Many thanks,
Dave

HelioGuilherme66

unread,
Jan 30, 2015, 7:20:11 PM1/30/15
to robotframe...@googlegroups.com
My usual approach is to use batch (or shell script) files to do such tasks. You could also do a Python script with similar functionality.

I would get the file count from the DIR command with the recursive option. For example:
${output} =    Run    DIR /S ${sourcepath} 

(Then process the output getting the filecount.)
C:\>dir /s windows
(...)
     Total files listed:
    1840 files              385,129,355 bytes
    1528 directories        983,191,552 bytes free

Note the Run command implies using: Library    OperatingSystem

Hélio

Dave Weil

unread,
Feb 6, 2015, 10:20:51 AM2/6/15
to robotframe...@googlegroups.com
I am just not sure how I would then grab just the file count from the output, but I will play around with it and figure it out.

Thanks,
Dave

Dave Weil

unread,
Feb 11, 2015, 11:52:53 AM2/11/15
to robotframe...@googlegroups.com
Thanks all. I ended up using DIR like this to pass back just the file count:

${folderfilecount} Run dir "${sourcepath}\\${folder}" /b /a-d /s | find /c /v ""

This returns just the count in the folder and all sub-folders. Works great.

Dave
Reply all
Reply to author
Forward
0 new messages