Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hello all: I try to load mutiple f

Status
Not open for further replies.

stewang

Programmer
Aug 21, 2003
77
0
0
NZ
Hello all:
I try to load mutiple file name from a folder to a listbox, and save mutiple file name from list box to a directory,
But I failed for that, I trid to use foreach,and a option -mutiple for getOpenFile and getSaveFile, but it does nothing.

Could anyone help me please.
Thanks
rgds
stewang

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
 .mbar.file add command -label "Quit" -underline 0 -command exitApp;
  wm protocol . WM_DELETE_WINDOW exitApp
  menu .mbar.list -tearoff 0
  .mbar add cascade -label "List" -underline 0 -menu .mbar.list
  .mbar.list add command -label "Remove" -underline 0 -command removeName
  .mbar.list add command -label "Clear" -underline 0 -command clearList

  menu .mbar.acc -tearoff 0
  .mbar add cascade -label "Acc" -underline 0 -menu .mbar.acc
  # the list box
  set ::names {}
  listbox .l -listvar ::names -width 40 -selectmode multiple
  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 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    
        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 -mutiple 1]
      foreach target [tk_getSaveFile -mutiple 1] {
        if {$target != ""}         {       
          # copy the file content to the new position       
          file copy $::pathes($source) $target
          # remove the name from the listbox
          removeName
        }
      }
    }
  }
    # get the currently selected name
    # remove the name from the listbox
  proc removeName {}   {
    # get the currently selected name
    set n [.l curselection]
    set name [lindex $::names $n]
    set n [lsearch $::names $name]
   if {$n != -1}    {
      set ::names [lreplace $::names $n $n]
    }
    unset ::pathes($name)
  }
    # clear the listbox
  proc clearList {}   {
    set ::names {}
    array unset ::pathes
    array set ::pathes {}
  }
    # exit
  proc exitApp {} { exit }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top