Read thru a filelist array

Last post 09-14-2009 9:45 AM by Gordon. 2 replies.
Page 1 of 1 (3 items)
Sort Posts: Previous Next
  • 09-09-2009 5:35 PM

    Read thru a filelist array

    Hi,

    How can I read thru a filelist array and parse the current file name - rename and write out to a new directory

    I am attempting to do this with process designer - would I be better off trying to do this using java and executing

    that instead - I think I should be able to do it all in RIFL.

    Any suggestions would be appreciated.

     

    thanks,

     

      

     

  • 09-14-2009 9:20 AM In reply to

    • bvh
    • Top 75 Contributor
    • Joined on 05-16-2008
    • Austin, TX
    • Posts 82

    Re: Read thru a filelist array

     This code should provide an example of what you want to do.

    Dim temp_files()
    Dim tf
    Dim newFileName
    fileList(macroExpand("$(TEMP_DIR)*.*"),temp_files)

    for each tf in temp_files
        if IsFile(macroExpand("$(TEMP_DIR)" & tf)) then
            newFileName = "newFileName.txt"
            fileRename(macroExpand("$(TEMP_DIR)" & tf), macroExpand("$(OTHER_DIR)" & newFileName))
        end if
    next

    This reads a list of files into an array, then iterates through that array.  For each entry, it checks to see if it really is a file, (directories are picked up by the FileList() function), and then renames the files with a new name.  Take a look at this, and let us know if you've got any questions on how things work.

     

    The documentation for FileList() may also be helpful:  http://docs.pervasive.com/products/integration/di/wwhelp/wwhimpl/js/html/wwhelp.htm#href=rifl/FileList_Function.html

     

  • 09-14-2009 9:45 AM In reply to

    • Gordon
    • Top 25 Contributor
    • Joined on 08-30-2007
    • Delft, The Netherlands
    • Posts 324

    Re: Read thru a filelist array

    No nonsense approach:

    dim file_list()
    FileList( "*", file_list )
    i=0
    While 1
      ' Do some stuff with the current entry: FileRename( olddir & file_list(i), newdir & newname )
      ' placeholder:
      MsgBox( file_list(i) )
      i=i+1
    Wend

    This will lead to an error when all entries have been processed and you're trying to access an index outside the array boundaries. Now you can trap this expected error with an "On error" statement, but this will also trap unexpected errors (e.g. attempting to move a locked file). You might therefore want to use one of the following two variations:

    1. Grow (or shrink) the array to a fixed size

    dim file_list()
    FileList( "*", file_list )
    redim preserve file_list ( 0 to 999 )  ' keyword preserve leaves content inside
    i=0
    While ( ( i<1000 ) and len( file_list(i) ) )
      ....
      i=i+1
    Wend

     2. Use sort functions to determine the last entry and use that value to break out at the right time

    dim file_list()
    FileList( "*" , file_list )
    SortArray( file_list , 1 , 1 )  ' reverse sort places highest value at index 0
    lastfile=file_list( 0 )
    SortArray( file_list , 1 , 0 )  ' normal sort moves highest value to the last index
    i=0
    morefiles=-1
    While morefiles
      ...
      morefiles=compare( file_list(i) , lastfile )
      i=i+1
    Wend

    Or you can use "for each" Embarrassed

Page 1 of 1 (3 items)