GFL - in subdir

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
BlackWisdom
Pro Scripter
Posts: 58
Joined: Thu Oct 16, 2003 12:53 am

GFL - in subdir

Post by BlackWisdom » Wed Dec 14, 2005 5:37 pm

Ho guys, :?:

Is it possible to use GFL to get the file list in not just the folder but the subfolders as well I have been trying for a day now with no luck - thanks in advance...

User avatar
JRL
Automation Wizard
Posts: 3529
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Wed Dec 14, 2005 6:01 pm

I always use DOS

Let>RP_WINDOWMODE=2
Let>RP_WAIT=1
Run>cmd /c dir [path]\*.* /s /b > %TEMP_DIR%~temp_dir_list~
ReadFile>%TEMP_DIR%~temp_dir_list~,dir_list


Separate the variable dir_list by %CRLF% and you have your list all assigned to variables. This has the added attraction that the full path will be included with each file name.

If you're running an older version of windows you may need to replace CMD with COMMAND.

Hope this helps,
Dick

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Wed Dec 14, 2005 6:31 pm

Hi,

Try the following script which I adapted from the one posted at:
http://www.mjtnet.com/forum/viewtopic.php?t=785

[code]//set your path here ...
Let>path=C:\Programs and Settings\marcus\My Documents\

VBSTART
Function GetFolderList(folderspec)
Dim fso, f, f1, fc, s, sf
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(folderspec)
Set fc = f.SubFolders
For Each f1 in fc
s = s & f1.name & ";"
sf = GetFolderList(folderspec & "\" & f1.name)
if sf "" then
s = s & f1.name & "\" & sf & ";"
end if
Next
If s "" then
s = Mid(s,1,Len(s)-1)
End If
GetFolderList = s
End Function
VBEND

//list files in path
GetFileList>%path%*.*,filelist
Separate>filelist,;,files
If>files_count>0
Let>f=0
Repeat>f
Let>f=f+1
MessageModal>files_%f%
Until>f,files_count
Endif

//Enumerate files in all subfolders in path
VBEVAL>GetFolderList("%path%"),folderList
Separate>folderList,;,folders

Let>k=0
Repeat>k
Let>k=k+1
Let>subfolder=folders_%k%
Let>subfolderpath=%path%%subfolder%
MessageModal>Now for files in %subfolderpath%
GetFileList>%subfolderpath%\*.*,filelist
Separate>filelist,;,files
If>files_count>0
Let>i=0
Repeat>i
Let>i=i+1
Let>thefile=files_%i%
MessageModal>thefile
Until>i,files_count
Endif
Until>k,folders_count
[/code]

The VBScript function is recursive and gets a list of all subfolders and subfolders of subfolders etc in Path. The following code then loops through each of these subfolder paths to get a list of files within them.

Enjoy ...
Last edited by Marcus Tettmar on Tue Jul 31, 2007 6:05 am, edited 4 times in total.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

cstockton
Newbie
Posts: 2
Joined: Mon Jul 30, 2007 11:48 pm

Post by cstockton » Tue Jul 31, 2007 12:09 am

The script posted earlier doesn't work as intended. It seems to find only the first file in the child directory immediately under the specified path, and then disregards all subsequent files until it gets to the next child directory immediately under the specified path. I'm having difficulty understanding why it does this, although I suspect it is because of the last condition:

Until>k,folders_count

Would the variable folders_count would be the number of directories immediately under the specified path, and would thus need to be otherwise set to include subfolders?? Please, anyone, help. Thanks.

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Tue Jul 31, 2007 6:06 am

Works fine for me. folders_count is to do with the number of folders, not the files. Make sure the original path declared at the top ends with a slash - I've added a comment to that effect.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
JRL
Automation Wizard
Posts: 3529
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Tue Jul 31, 2007 12:41 pm

I'm always amazed at most people's reluctance to use DOS. 5 lines to get this done with DOS versus 20 lines to do it in VBScript. However the great news is that beginning in November of 2006, this can be accomplished without the need for either. GetFileList> now has the system variable GFL_TYPE, which when set to 1 will create a list of directories rather than a list of files. From help for GetFileList>
GetFileList>filespec,result[,delimiter]

GetFileList returns a list of the files found matching the specified filespec. For instance, to return the list of files in the temp directory specify c:\temp\*.* as the filespec.

If delimiter is ommitted each filename is separated by a semicolon. Otherwise the filenames are separated by the delimiter specified.

By default GetFileList returns files. To return directories only set GFL_TYPE to 1 (Let>GFL_TYPE=1).

Abbreviation : GFL

Example

GetFileList>c:\temp\*.*,files
Separate>files,;,file_names
MessageModal>Num Files: %file_names_count%

Let>k=0
Repeat>k
Let>k=k+1
Message>file_names_%k%
Until>k,file_names_count

cstockton
Newbie
Posts: 2
Joined: Mon Jul 30, 2007 11:48 pm

Post by cstockton » Tue Jul 31, 2007 7:56 pm

There seems to be a problem with the way the script assembles the paths... If you watch the messages that say "Now for files in %subfolderpath%" you will note that after the first message, it will stop putting child directories under the specified path, and only puts the remainder of the path, so anything more than two directories deep will not be an existing path, which causes it not to find many files more than "1-deep" under the specified path.
Example:
path=C:\1Deep\
Message1: Now for files in C:\1Deep\2Deep
Message2: Now for files in C:\1Deep\2Deep\3Deep
Message3: Now for files in C:\1Deep\3Deep\4Deep
Message4: Now for files in C:\1Deep\3Deep\4Deep\5Deep

Notice how the part of the path that is 2-deep is missing after the first two messages. Somehow the part of the script that puts that section of the path together is not being looped.

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts