Rename file to have filename and date appneded to filename

Clovertech Forums Read Only Archives Cloverleaf Cloverleaf Rename file to have filename and date appneded to filename

  • Creator
    Topic
  • #49340
    Henry Bauer
    Participant

      I need to rename a file either in tcl or within a shell script to include the filename with the date appended to the end of the filename. Thanks for any help.

    Viewing 2 reply threads
    • Author
      Replies
      • #61561
        Jim Kosloskey
        Participant

          Henry,

          In Tcl:

          For Date look at the clock command. Use clock seconds to get the current epoch time, and clock format to get the date format you want.

          For file rename look at the file command. It has a rename option.

          Once you get the file name you want and the date you want (via the clock command), concatenate the two together and use the filre rename to do the rename.

          Jim Kosloskey

          email: jim.kosloskey@jim-kosloskey.com 30+ years Cloverleaf, 60 years IT – old fart.

        • #61562

          Here’s a script I wrote to do something similar in a tps proc. This script takes a list of files passed by tpsScanDir. You can modify it or use some of the lines to do what you need.

          Code:

          #
          # Purpose: Rename the files picked up and passed by tpsScanDir.tcl
          #   and sent to tpsPgpSend. This script was created originally to
          #   remove underscores in file names being sent to PGP for
          #   encryption. The underscores are not allowed by PGP.
          # Author: Max Drown
          # Date: 05/02/2006
          # Args: tps keyedlist containing:
          #       MODE    run mode (”start” or “run”)
          #       MSGID   message handle
          # Returns: list containing new file names
          #

          proc tpsRenameFiles { args } {
             global ibdir HciProcess
             keylget args MODE mode

             switch -exact — $mode {
                 start {
                     echo “[whereAmI] Shutdown mode'”
                 }

                 run {
                     keylget args MSGID mh
                     set fileList [msgget $mh]
                     set dispList “{CONTINUE $mh}”

                     set newFileList {}

                     puts “[whereAmI] Begin.”
                     foreach inFile $fileList {
                         if {[regexp {tpsPgpSend} $inFile] == 0} {
                             # Convert underscores to dashes
                             puts “[whereAmI] Converting underscores to dashes in $inFile”
                             regsub -all {_} $inFile {-}  newFile
                             #regsub {.} $newFile {-}  newFile

                             # Create a new list of files with the new file names
                             lappend newFileList $newFile

                             # Rename and move the file
                             if {[catch {exec mv $ibdir/$inFile $ibdir/$newFile} catch_return]} {
                                puts “[whereAmI] Error: Moving $inFile to $newFile failed: $catch_return”
                                puts “[whereAmI] End.”
                                catch {exec hcienginestop -p $HciProcess > /dev/null &} catch_return
                                return “”
                             } else {
                                 puts “[whereAmI] Moved $inFile to $newFile (underscores converted to dashes)”
                             }
                         } else {
                             # The file has already been renamed
                             lappend newFileList $inFile
                         }
                     }

                     puts “[whereAmI] DEBUG: newFileList= $newFileList”
                     puts “[whereAmI] DEBUG: dispList= $dispList”
                     puts “[whereAmI] End.”
                     msgset $mh “$newFileList”
                     return $dispList
                 }

                 shutdown {
                     echo “[whereAmI] Shutdown mode'”
                 }

                 default {
                     echo “[whereAmI] Unknown mode: $mode”
                     return “”
                 }
             }
          }

          Here’s one way to get a time stamp into a variable.

          Code:

          set messageTs [clock format [clock seconds] -format %Y%m%d%H%M%S]

          -- Max Drown (Infor)

        • #61563
          Russ Ross
          Participant

            Here is an example of how to include a date in a filename using KSH:

            Code:

            mv   dump_011_idx_charges.post-processed   CHGA`date +”%m%d%y”`.DAT

            Here is a standalone TCL script I wrote that is along the same trend of thought:

            Code:

            #!/usr/bin/ksh
            # this line to escape the next line from the tcl interpreter
            exec hcitcl “$0” “$@”

            # Begin Module Header ==============================================================================
            #
            #——
            # Name:
            #——
            #
            # file_mtime.tcl
            #
            #———
            # Purpose:
            #———
            #
            # Display to sdtout the mtime (last modification time) of the specified file
            # In the following format:
            #
            # CCYY.MM.DD__HH.MM.SS
            #
            #——–
            # Inputs:
            #——–
            #
             set name [lindex $argv 0]
             if {[cequal $name “”]} then {exit}
            #
            #——-
            # Notes:
            #——-
            #
            # Example of normal usage
            #
            #     file_mtime.tcl   file_name_of_interest
            #
            #———
            # History:
            #———
            #
            # 2001.02.16 Russ Ross
            #          – wrote initial version
            #
            # End of Module Header =============================================================================

            set type [file type $name]
            if [cequal “$type” “file”] {
               set mtime [file mtime $name]
               puts stdout [fmtclock $mtime “%Y.%m.%d__%H.%M.%S”]
            }

            Russ Ross
            RussRoss318@gmail.com

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