TCL – Storing Input FileName into a variable

Clovertech Forums Read Only Archives Cloverleaf Cloverleaf TCL – Storing Input FileName into a variable

  • Creator
    Topic
  • #48830
    Femina Jaffer
    Participant

      Hello,

      Could anyone advise me on how I can capture in TCL  the name of the input file and store it into a variable?  I need to read a file from a directory, strip out somethings and then write the file out (using the same filename)?

      Thank you.

      Fj

    Viewing 6 reply threads
    • Author
      Replies
      • #59835
        David Gordon
        Participant

          I’ve done this in a Directory Parse proc, but not in any other contexts.

          Is this where you will be calling your TPS?

        • #59836
          Femina Jaffer
          Participant

            Hi David,

            Not too familiar with a directory parse proc.  Is that any different for a tcl proc?  The code that I have is written for an x12 file, in TCL.  A bit of the syntax is added below:

            proc Facets_x12_div { 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 msg [msgget $mh]

                       set dir /hci/FTP/EOB/FACETS/OUT

                       set count 0

               set segCount 0

                       set segID “”

                       set elSep [string index $msg 104]

                       set ISAHdr [string range $msg 0 [string first “${elSep}” $msg]]

                       set splitMsg [split $msg ~]

                         foreach seg $splitMsg {

                 if {$segCount > 0} {

                     set preSegInd $segID

                                     

                 }

                           set segID [string range $seg 0 2]

               incr segCount +1

                             switch -exact $segID {

                               ISA     {

                                         lappend outMsg $seg }

                                       

                               GS*     {  if {$count > 0} {

                                      set outFile [open $dir/PHP$outFileName.DAT w]

                              set outMsg “[join $outMsg ~]~”

                                      puts $outFile $outMsg

                              flush $outFile

                              set outMsg “”

                                              lappend outMsg $ISAHdr

                           

            Thank you David.

          • #59837
            David Gordon
            Participant

              Directory Parse is just another context.  If you are setting up a fileset protocol, you have the option to specify a directory parse proc.

              We use one to normalize some data that we get in HRL format, that is new-line delimited between segments.  It reformats the data into a properly-delimited HL7 message so that Cloverleaf can use it properly.  

              Basically, a Dir Parse proc is passed a list of file names as the message.  You can then open, edit, close those files using TCL file commands.  You then pass back a list of files that you want the protocol file to read in.  

              Here is the top of ‘run’ segment of one dirparse we use.  Sorry for the lack of comments, but all it does is read a list of all the files in the inbound directory, opens the file, reads it in, and closes the file.  You can then use that data to make new files, overwrite the data in the existing files, etc…

              This proc needs the directory the files are in passed as an argument, since it’s not included in the data passed by Cloverleaf.

              Code:

              run {
                 # ‘run’ mode always has a MSGID; fetch and process it
                         keylget args MSGID mh
                         keylget args ARGS inDir
                         echo “MH is $mh”
                         set inFileList [msgget $mh]
                         echo “inFileList is $inFileList”
                         foreach inboundFile $inFileList {
                          set inboundDirectoryFile “$inDir$inboundFile”
                            set currentFileID [open $inboundDirectoryFile r]
                          set fileContents [read $currentFileID]
                          close $currentFileID
                         

            • #59838
              garry r fisher
              Participant

                Hi,

                You could get the names of the files using glob to read the directory and then use the ‘file’ commands to extract the relevant information.

                set fileDir E:Target

                cd $fileDir #use catch to trap any errors

                for each filename [glob -nocomplain *] {

                   set fname [file root $filename]

                }

                There are other commands such as file tail, file extension etc which may be of use to you.

                Regards

                Garry

              • #59839
                Femina Jaffer
                Participant

                  Thank you all for your responses.  I am able to do almost what I need now.  

                  Garry, you indicated that I could use the file extension, how do I use that and what is the syntax for that?  I am able to loop in the directory and see the contents of all the files.

                  Thank you.

                  fj

                • #59840
                  Femina Jaffer
                  Participant

                    Garry,

                    I did figure out the file extension and I did get my script to work finally and do what it was to do.

                    Again, cannot thank you all enough!

                  • #59841
                    garry r fisher
                    Participant

                      Hi fj,

                      Glad to be of help.

                      Garry

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