› Clovertech Forums › Read Only Archives › Cloverleaf › Cloverleaf › Rename file to have filename and date appneded to filename
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.
#
# 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.
set messageTs [clock format [clock seconds] -format %Y%m%d%H%M%S]
-- Max Drown (Infor)
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:
#!/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