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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replace file contents

Status
Not open for further replies.

nivparsons

Technical User
May 13, 2009
4
GB
I have sets of files in numbered directories, one set is copied to a working directory & used as needed. However, I would like to somehow store the number of the directory, so if the user requests the same index as is currently in the working directory, no copy is performed; or, if a new set is selected, then that new number is stored in place of the old one.
At the moment, I open a file (in r mode) with just a 3 digit value in it, which I open to read, check if it's equal to the selected one, skip copying if same, else close the file, open it again (in w mode) and write the new index before closing it agian, finally doing the file copy.

This seems a very untidy way of storing/updating the index, any suggestions for a better way please?
 
I'm doing all this in a tcl/tk gui wrapper, so I need the right files in the right directory before the tcl script calls more procs.
 
Maybe I don't understand you right, because you didn't post any code.
But as I understand, you have a number of files and in every file is only one 3-digits number?
Would it not be better to have all the numbers in one file, then read the file in an list /or hash/, compare the list with something, reorganize it and write the updated list back to the file?
 
No, I have "N" sets of 8 files of various sizes, each set has the same files, but the files have different contents between sets. I want to copy a particular set to a working directory, where I also want to store the set number, which is part of the directory name for the set.

see code snippet below:

set ip_mif_file [tk_chooseDirectory -initialdir $proj_top/mem_init_lib]

set full_name [file rootname $ip_mif_file] ;# Fullpath & file-name
set file_tail [file tail $ip_mif_file] ;# just the filename with extension
set file_head [file rootname $file_tail] ;# filename with extension stripped
set headlen [string length $file_head]
set m_id_num [string range $file_head [expr {$headlen - 3}] $headlen]
# prefix ID number with "m" to identify as Memory init index.

# Now, test the init_index.txt file for value (& existence first!),
# if the value in the file matches the selected directory, report already
# set as mem_ini_files, else copy all mem_init_lib/init_lib_nnn to the
# mem_init_files directory, create a NEW init_index.txt file and write it
# to the mem_init_files directory, overwriting any previous version, if existing.

set f_index [open $proj_top/mem_init_files/init_index.txt r]
while {[gets $f_index line] >= 0} {
set init_index $line
}
if {$init_index == $m_id_num} {

set answer [tk_messageBox \
-icon info \
-type ok \
-message "This is already the MIF selection.\n\
No files will be overwritten."\
-title "MIF Selection" \
-parent .]

close $f_index

} else { ;# index NOT the same as current, so overwrite with alternate mem-init files

# Overwrite mem-init files with alternate versions:
close $f_index
set f_index [open $proj_top/mem_init_files/init_index.txt w]
puts $f_index $m_id_num
close $f_index

}
 
Either you left out too much for me to follow or I'm just being dense. Anyway, let's break this down a little. First of all tk_chooseDirectory will only return a directory name, not a file name. So all you want to do is get the last step on the path:
Code:
set dirnm [tk_chooseDirectory]
set dlast [file tail $dirnm]
So now I gather that the 3-digit code is the last 3 characters of the directory (now $dlast). If you could be sure that all of the other characters were not digits you could use a simple regexp for this but that may not be a good assumption:
Code:
set intID [string range $dlast end-2 end]
Note that you needn't do all the expr stuff for indexing.
Now let's say your working directory is $dirWork. So unless the files are already in $dirWork, you want to copy them there:
Code:
file copy -force $dirnm/*.* $dirWork
Now, rather than have a file that contains the index number, I'd put the number in the file name, say $intID.id:
Code:
set fid [open $dirWork/$intID.id w]
close $fid
If ".id" is possibly an existing extension, then choose something that is not.

Then you only need to query the $dirWork directory to see if the current contents are the ones you chose:
Code:
if {file exists $dirWork/$intID.id} {....}


_________________
Bob Rashkin
 
Thanks Bob,similar to what I'm doing now, but yours is a a bit simpler & clearer. I'll give your ideas a go, ta.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top