Tag Archives: tcl

file and directory selector with gui for tcl – tk

Quick Code: Here is a sample file and directory selector procedure written in tcl – tk. The code is added in my public git repo at git://github.com/rvguiman/tcl-tk.git or https://github.com/rvguiman/tcl-tk.git. Or check the code below.

#-------------------------- gFileDirSelector ------------------------------------------------#
# what: procedure for selecting a file or directory, run it using wish/tk
# arg: optional arg type, valid values none/anything for file selection or "dir" if you want to choose directory instead of file
# return: if type is file, return the full path of file or 0 if cancelled
#--------------------------------------------------------------------------------------------#

proc gFileDirSelector { { type "file" } } {
     if { $type ne "dir" } {
          set types {
          {{text files} {.txt} TEXT}
          {{tcl scripts} {.tcl} }
          {{all files} * }
          }
          set filename [tk_getOpenFile -filetypes $types]
          if {$filename ne ""} {
               puts "file selected -> $filename"
               return $filename
               } else {
               puts "file selection cancelled"
               return 0
               }
          } else {
          set dirSel [tk_chooseDirectory]
          if {$dirSel ne ""} {
              puts "dir selected -> $dirSel"
              return $dirSel
              } else {
              puts "directory selection cancelled"
              return 0
              }
          }
     }


simple email procedure for tcl in linux or unix system

Quick Code:
Here is a sample procedure for tcl that you can use to send email. This basically call the mail function built in linux or unix like system.
Usage: gQuickMail “subject” “sendToEmailAdress” “bodyMessage” “ccToEmail” “bccToEmail”
Git: git clone https://github.com/rvguiman/tcl.git (added in g_misc_utility.tcl)
Or Check the code below

proc gQuickMail { subject sendTo bodyMsg ccTo bccTo } {
        set qmsgHan [open "tempMsg.qmsg" w]
        puts $qmsgHan $bodyMsg
        close $qmsgHan
        puts "sending mail...."
        if { $ccTo != "" && $bccTo != "" } {
                catch { exec mail -s $subject -c $ccTo -b $bccTo $sendTo < tempMsg.qmsg } msg
                } elseif { $ccTo != "" && $bccTo == "" } {
                catch { exec mail -s $subject -c $ccTo $sendTo < tempMsg.qmsg } msg
                } elseif { $ccTo == "" && $bccTo != "" } {
                catch { exec mail -s $subject -b $bccTo $sendTo < tempMsg.qmsg } msg
                } else {
                catch { exec mail -s $subject $sendTo < tempMsg.qmsg } msg
                }
        puts $msg
        catch { exec rm -rf tempMsg.qmsg } msg
        puts $msg
        puts "closing gQMail"
        }

get file date in tcl

Quick Code:
This is a simple tcl script procedure to get the date of a file. The return value is a list containing time in seconds and human readable time in string.

Usage:¬†getFileDate “/docs/sample/test.txt”
Git: git clone https://github.com/rvguiman/tcl.git (added in g_file_utility.tcl)
Or check the code below

proc gGetFileDate { filePass } {
    set secTime [file mtime $filePass]
    set humanTime [clock format $secTime -format "%d_%b_%y_%H:%M:%S"]
    return [list $secTime $humanTime]
    }