Script BASIC

Script BASIC => Tutorials => Topic started by: support on September 24, 2008, 05:49:31 PM

Title: Reading a directory
Post by: support on September 24, 2008, 05:49:31 PM
Q. How do I get a list of file names in a given directory.

A. Check out the ScriptBasic wiki for directory open & close and the nextfile command to get to get a list of file names. (optionally based on a pattern)

Directory Read Information (http://www.scriptbasic.org/wiki/index.php?title=ScriptBasic:UsersGuide_12.14)

Code: [Select]
OPEN DIRECTORY dir_name PATTERN pattern_value OPTION option_value AS dir_number

file_name = NEXTFILE

x = EOD(dir_number)

RESET DIRECTORY #dir_number

CLOSE DIRECTORY #dir_number

OPTIONS

Note: Use AND if more then one option is needed with the OPEN DIRECTORY directive.

Here is a bit of code I snagged from my MLS utility that removes old photos of properties that have gone off market. My directory structure is made up of 1000 sub-directories numbered from 000 to 999. The last three digits of the MLS # determines which sub-directory the photo(s) resides in. The following code would read all 1000 directories selecting only .jpg files and print the file names.

Code: [Select]
fn = FREEFILE

FOR d = 0 TO 999
  DName = FORMAT("%~000~",d)
  OPEN DIRECTORY DName PATTERN "*.jpg" OPTION sbCollectFiles AS #fn
  RESET DIRECTORY #fn
  GOSUB READ_DIR
  CLOSE DIRECTORY #fn
NEXT d

END

READ_DIR:

FName = NEXTFILE(fn)
IF FName = undef THEN RETURN
PRINT FName & "\n"
GOTO READ_DIR