Arrays & Filenames

Clovertech Forums Read Only Archives Cloverleaf Cloverleaf Arrays & Filenames

  • Creator
    Topic
  • #49638
    Femina Jaffer
    Participant

      Hello,

      Does anyone know of or have a code snippet on how one would read filenames from a directory and store each of these filenames in an array, and then match the filenames in another directory against the filenames stored in the array? Any advice or tips or code would be most helpful.

      Thank you.

      Femina Jaffer

    Viewing 7 reply threads
    • Author
      Replies
      • #62816
        James Cobane
        Participant

          Femina,

          Here are some simple tcl commands:

          cd /dir1  ;#change directory to first directory

          set filelist1

        • #62817
          Charlie Bursell
          Participant

            Jim:

            I had not thouht about the intersect command.  The intersect command is provided by extended Tcl but still a very useful command.

            But you can make it even more simple by using the full power of the glob command.  No need to change directories.

            set dir1

            set dir2

            set fileList1 [glob -nocomplain -directory $dir1  -types f -tails *]

            set fileList2 [glob -nocomplain -directory $dir2  -types f -tails *]

            set matched [intersect $fileList1 $fileList2]

            If you really wanted to be succinct  ðŸ˜‰

            set matched [intersect

                    [glob -nocomplain -directory $dir1  -types f -tails *]

                    [glob -nocomplain -directory $dir2  -types f -tails *]]

          • #62818
            James Cobane
            Participant

              Charlie,

              Thanks for the info.  More stuff to add to the toolbox!

              – Jim

            • #62819
              Femina Jaffer
              Participant

                Thank you all.  I will give these solutions a try and get back to you thanks.

                Femina

              • #62820
                Femina Jaffer
                Participant

                  Thanks Charlie and James, this did it and was exactly what I wanted.  One more question, what is the syntax to find unmatched files?

                  Thanks again!

                  Here is the code I finally put together.

                  ######################################################################

                  # Name: billing_dupfilecheck

                  # Purpose:

                  # UPoC type: tps

                  # Args: tps keyedlist containing the following keys:

                  #       MODE    run mode (“start”, “run” or “time”)

                  #       MSGID   message handle

                  #       ARGS    user-supplied arguments:

                  #              

                  #

                  # Written By:  Femina Jaffer on Nov. 11, 07

                  #  This proc checks for duplicate files before processing files.

                  #

                  # Returns: tps disposition list:

                  #          

                  #

                  proc billing_dupfilecheck { args } {

                     keylget args MODE mode               ;# Fetch mode

                     set dispList {} ;# Nothing to return

                     switch -exact — $mode {

                         start {

                             # Perform special init functions

                     # N.B.: there may or may not be a MSGID key in args

                         }

                         run {

                     # ‘run’ mode always has a MSGID; fetch and process it

                             keylget args MSGID mh

                             # set directory paths of files to compare

                             set dir /home/hci/

                             set in_dir /home/hci/temp/

                             

                             # find the list of files in the first directory

                               set fileList1 [glob -nocomplain -directory $dir  -types f -tails *.DAT]

                             # find the list of files in the second directory

                               set fileList2 [glob -nocomplain -directory $in_dir  -types f -tails *.DAT]

                             # match files from both directories  

                               set matched [intersect $fileList1 $fileList2]

                               if {$matched !=””} {

                                    set fname [file root $matched]

                                    echo matched $matched

                                    # change directory

                                     cd $dir

                                    # copy the $matched file to another directory

                                     echo copying matched file

                                     file copy $dir/$matched $matched.DAT

                               

                              }            

                         }

                         time {

                             # Timer-based processing

                     # N.B.: there may or may not be a MSGID key in args

                         }

                  shutdown {

                     # Doing some clean-up work

                  }

                         default {

                     error “Unknown mode ‘$mode’ in blank”

                         }

                     }

                     return $dispList

                  }

                • #62821
                  Charlie Bursell
                  Participant

                    for unmatched files you could do something like this:

                    set fileList1 [glob ….

                    set fileList2 glob ….

                    set matched [intercept ….

                    foreach fn $matched {

                        set loc1 [lsearch -exact $fileList1 $fn]

                        set loc2 [lsearch -exact $fileList2 $fn]

                        lvarpop fileList1 $loc1

                        lvarpop fileList2 $loc2

                    }

                    fileList1 now contains a list of unmatched files from dir1 and

                    fileList2 contains a list of unmatched files from dir2

                    You could combine into a single list with the concat command

                  • #62822
                    James Cobane
                    Participant

                      Femina,

                      If you use the intersect3 command (in lieu of the intersect command) you can get what is unmatched in each directory.  The  intersect3 is similar to intersect, but returns a list of three results. The first value returned is what was in the first list but not in the second, and the 3rd value is what is in the second list but not in the first.   The second value returned is what was common to both lists.  All three result lists are will be sorted.

                      i.e.

                      intersect3 {a b c} {x a a a c a}

                      => b {a c} x

                      Jim Cobane

                      Henry Ford Health

                    • #62823
                      Femina Jaffer
                      Participant

                        Thanks!  I can’t tell you how much I have learned from this forum.  

                        fj

                    Viewing 7 reply threads
                    • The forum ‘Cloverleaf’ is closed to new topics and replies.