Notifications
Clear all

[Closed] sorting files according dates in MXS

Any one knows how to sort files according date in MaxScript

2 Replies

Here’s a function through DotNet to get a “comparable value” of file dates:

	fn fileTime file =
	(
		dotnetFile = dotnetClass "System.IO.File" 
		fileDate = dotnetFile.GetLastWriteTime file
		timeZero = (dotnetobject "system.datetime" 1980 1 1 0 0 0)
		timeLapse = fileDate.subtract timeZero
		ElapsedSeconds = timeLapse.TotalSeconds
		ElapsedSeconds as integer	
	)
				

Now you can sort your files with any sorting method. For example, through maxLINQ

	files = getFiles @"c:\foo\*.max"

	sortedFiles = (_From Files).Sort(#'file=>fileTime file')
	sortedFilesDescending = (_From Files).Sort(#'d:file=>fileTime file')
		

(maxLINQ thread: http://forums.cgsociety.org/showthread.php?f=98&t=1438311 )

I’ve edited the function to return an integer (if not, maxLINQ.Sort does nothing).

With qsorting:


(
	fn fileTime file =
	(
		dotnetFile = dotnetClass "System.IO.File" 
		fileDate = dotnetFile.GetLastWriteTime file
		timeZero = (dotnetobject "system.datetime" 1980 1 1 0 0 0)
		timeLapse = fileDate.subtract timeZero
		ElapsedSeconds = timeLapse.TotalSeconds
		ElapsedSeconds as integer	
	)
				
	fn dateComparer x y DES:OFF =
	(
		dir = if DES then -1 else 1
		xdate = fileTime x
		ydate = fileTime y
		(xdate-ydate) *dir
	)
	
	files = getFiles @"c:\foo\*.max"
	qsort files dateComparer DES:ON
	files
)