Hello all:
I'd like to append a string to a filename when I save a file. For example, I want to save a file of test.tk to another directory, and I give a new name for the old file which calls newtest.tk. After I save the file, I want to the new file has a string append it, format like "newtest.tk
|stewang|14/03"
Could anyone help me, please.
Thanks
rgds
strewang
I'd like to append a string to a filename when I save a file. For example, I want to save a file of test.tk to another directory, and I give a new name for the old file which calls newtest.tk. After I save the file, I want to the new file has a string append it, format like "newtest.tk
|stewang|14/03"
Could anyone help me, please.
Thanks
rgds
strewang
Code:
package require Tk
# the menu bar
menu .mbar
. configure -menu .mbar
menu .mbar.file -tearoff 0
.mbar add cascade -label "File" -underline 0 -menu .mbar.file
.mbar.file add command -label "Open" -underline 0 -command openFile
.mbar.file add command -label "Save" -underline 0 -command saveFile
.mbar.file add separator
# the list box
set ::names {}
listbox .l -listvar ::names -width 40
pack .l -expand 1 -fill both
# the actions
# get some file names
# add the new ones to the listbox
# do something with the content of each new file
array set ::pathes {}
proc openFile {} {
# get some file names
# set filenames [tk_getOpenFile -multiple 1];
foreach filenames [tk_getOpenFile -multiple 1 ] {
foreach fn $filenames {
# get simple name
set name [file tail $fn]
# check if its a new one
if {![info exists ::pathes($name)]} {
# add the name to the array
puts "set ::pathes($name) $fn"
set ::pathes($name) $fn
# add the name to the listbox
lappend ::names $name
# get the file content and do something with it
set thefile [open $fn r]
#puts "the file : $thefile"
set filecontent [read $thefile]
close $thefile
}
}
# sort the listbox content
set ::names [lsort $::names]
}
}
# get the currently selected name
# copy the corresponding file somewhere
# remove the name from the listbox
proc saveFile {} {
# get the currently selected name
set n [.l curselection]
if {$n != ""} {
# get the old name
set source [lindex $::names $n]
# get the new name
set target [tk_getSaveFile]
if {$target != ""} {
# copy the file content to the new position
file copy $::pathes($source) $target
}
}
}