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!

space in file mtime

Status
Not open for further replies.

rommeq

Programmer
Sep 26, 2011
6
0
0
UA
Hello.
I wrote a function to sort a list:
Code:
proc sortl {first second} {

	set f1 [glob -nocomplain -directory "F:/Tcl_project/images" $first]
	puts f1=$f1
	set t1 [clock format [file mtime "$f1"] -format %H%M%S]
	set f2 [glob -nocomplain -directory "F:/Tcl_project/images" $second]
	set t2 [clock format [file mtime "$f2"] -format %H%M%S]
	#puts $f2
	set res [string compare "$t1" "$t2"]
   if {$res != 0} {
		  return $res
	   } else {
		  return [string compare $t1 $t2]
   }
}
but if the filename contains a space, then error:
could not read "{F:/Tcl_project/images/DESERT SPIRIT150X300_PRE.JPG}": no such file or directory
while executing
"file mtime "$f2""
(procedure "sortl" line 7)
invoked from within
"sortl BBWG0902_PRE.JPG {DESERT SPIRIT150X300_PRE.JPG}"
(-compare command)
invoked from within
"lsort -command sortl $img"
invoked from within
"set im [lsort -command sortl $img]"
(file "iim.tcl" line 79)

help me please
 
The problem is not with spaces in the file name (if that's what you're suggesting). Consider:
Code:
% set fnam "x:/syseng/mus registry and file system requirements.doc"
x:/syseng/mus registry and file system requirements.doc
% file mtime $fnam
1098723166
%
I can only conclude that, as the error message says, the file or path does not exist.

_________________
Bob Rashkin
 
The file exists. Since the if you remove the spaces from the file name, it's all right.

Example I create a list
Code:
foreach val [glob -nocomplain -directory "F:/Tcl_project/images/" *] {

	
	lappend img [file tail $val]
	
puts $img
	
}

and call the sort function

Code:
set im [lsort -command sortl $img]
 
Wait a minute. What are f1 and f2 (which I suppose depends on what "first" and "second" are)? You can't apply "file mtime .." to a list. glob returns a list.

_________________
Bob Rashkin
 
If I can't apply "file mtime .." to a list, how then do I sort the list by file creation time? And why it works, if the file name, no space?
 
First of all, I'm pretty sure "it" doesn't work regardless of spaces in file names or not, whatever "it" is:
Code:
e:\>tclsh
% set lstA [glob e:/tcl/*.*]
e:/tcl/ah2ab.tcl e:/tcl/bmptst.tcl e:/tcl/cdoll.tcl e:/tcl/clientTst.tcl e:/tcl/cmdScrub.tcl e:/
tcl/cnvTH.tcl e:/tcl/cnvTH2.tcl e:/tcl/fixUPL.tcl e:/tcl/fndrpl.tcl e:/tcl/HxBnProcs.tcl e:/tcl/
imgtest.tcl e:/tcl/lis1245.tcl e:/tcl/LIS2MUS.tcl e:/tcl/lstbxTst.tcl e:/tcl/mde2mus.tcl e:/tcl/
mkAEHFgrdimg.tcl e:/tcl/mkDmp2.tcl e:/tcl/mkSimDmp.tcl e:/tcl/ms2date.tcl e:/tcl/rbtest.tcl e:/t
cl/rcImg.tcl e:/tcl/rnmpix.tcl e:/tcl/scrTbl.tcl e:/tcl/serverTst.tcl e:/tcl/setMcrI.tcl e:/tcl/
setSB.tcl e:/tcl/xflds.tcl
% file mtime $lstA
could not read "e:/tcl/ah2ab.tcl e:/tcl/bmptst.tcl e:/tcl/cdoll.tcl e:/tcl/clientTst.tcl e:/tcl/
cmdScrub.tcl e:/tcl/cnvTH.tcl e:/tcl/cnvTH2.tcl e:/tcl/fixUPL.tcl e:/tcl/fndrpl.tcl e:/tcl/HxBnP
rocs.tcl e:/tcl/imgtest.tcl e:/tcl/lis1245.tcl e:/tcl/LIS2MUS.tcl e:/tcl/lstbxTst.tcl e:/tcl/mde
2mus.tcl e:/tcl/mkAEHFgrdimg.tcl e:/tcl/mkDmp2.tcl e:/tcl/mkSimDmp.tcl e:/tcl/ms2date.tcl e:/tcl
/rbtest.tcl e:/tcl/rcImg.tcl e:/tcl/rnmpix.tcl e:/tcl/scrTbl.tcl e:/tcl/serverTst.tcl e:/tcl/set
McrI.tcl e:/tcl/setSB.tcl e:/tcl/xflds.tcl": no such file or directory
%
I think I would use the -command option in lsort:
Code:
% proc timesort {a b} {
if {[file mtime $a]<[file mtime $b]} {return -1} else {return 1}
}
% set lstB [lsort -command timesort $lstA]
e:/tcl/ms2date.tcl e:/tcl/setSB.tcl e:/tcl/setMcrI.tcl e:/tcl/cmdScrub.tcl e:/tcl/HxBnProcs.tcl
e:/tcl/ah2ab.tcl e:/tcl/mkAEHFgrdimg.tcl e:/tcl/mkSimDmp.tcl e:/tcl/mkDmp2.tcl e:/tcl/LIS2MUS.tc
l e:/tcl/lis1245.tcl e:/tcl/fixUPL.tcl e:/tcl/rcImg.tcl e:/tcl/cnvTH.tcl e:/tcl/mde2mus.tcl e:/t
cl/xflds.tcl e:/tcl/clientTst.tcl e:/tcl/serverTst.tcl e:/tcl/lstbxTst.tcl e:/tcl/cnvTH2.tcl e:/
tcl/scrTbl.tcl e:/tcl/fndrpl.tcl e:/tcl/rbtest.tcl e:/tcl/cdoll.tcl e:/tcl/rnmpix.tcl e:/tcl/bmp
tst.tcl e:/tcl/imgtest.tcl
%

_________________
Bob Rashkin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top