Notifications
Clear all

[Closed] open the last scene found by modification date?

–Hello, does someone knows why this code doesn’t open the last scene by modification date?
–by advance, thanks, i have no idea why!

			SaveASPath = "C:\\Users\\Path\\" 		
			SaveFile = "_SceneName_v000.max"  														-- default extention
			saveScene = SaveASPath + SaveFile														--  path + SceneName 
			filesArr = getFiles  (SaveASPath+SceneName+"*.max") 							-- test if a scene already exist in folder
			filesModDateArr = for f in filesArr collect (getFileModDate f)					
			 lastModifiedDate = amin  filesModDateArr
			--lastModifiedDate = amax  filesModDateArr
			index = findItem filesModDateArr lastModifiedDate
			--lastModifiedFile = filesArr[index+1]
			lastModifiedFile = filesArr[index]
			format "_indx: % 

” index
format “filesArr: %
” filesArr[index]
format “filesModDateArr: %
” filesModDateArr[index]
loadMAXFile lastModifiedFile quiet:true

11 Replies

hope this’ll help


theFiles= getfiles ((GetDir #autoback)+ "\\*.max") --- change it to your directory
dateFiles= for i in theFiles collect #(i,getFileModDate i)

fn compareString str1 str2 x:1 ascend:true =  -- this is important function when using qsort later
(
	local sc= stricmp str1[x] str2[x]
	case of 
	(
		(sc < 0.): (if ascend then 1 else -1)
		(sc > 0.): (if ascend then -1 else 1)
		default: 0
	)
)

qSort dateFiles compareString x:2	
format "Newest file : % 
File Date :% 
" dateFiles[1][1] dateFiles[1][2]

-- if you use ascend == true then you get the latest file in first array else you get oldest file 
-- last  : loadMAXFile dateFiles[1][1] quiet:true

here is how i would probably search for the most recently modified max file


(
	fn sortByDateTime v1 v2 = -(v1[3].Compare v1[3] v2[3])
	
	files = undefined
	f = getdir #maxData + "RecentDocuments.xml"
	if doesfileexist f do
	(
		doc = dotNetObject "system.xml.xmlDocument"
		if (try(doc.Load f) catch(#failed)) != #failed then
		(
			if (recent = doc.selectnodes "//MaxApp.RecentDocument").count > 0 do
			(
				files = for k=1 to recent.count collect
				(
					max_file = recent.item[k-1].ChildNodes.item[3].InnerText 
					if not doesfileexist max_file then dontcollect else 
					(
						#(k, max_file, (dotnetobject "System.IO.FileInfo" max_file).LastWriteTime)
					)
				)
				qsort files sortByDateTime
			)
		)
		doc.Normalize()
	)
	files
)


Hello K4noe,
thanks for your answer, but if i put the path where the scenes max files are, the answer is:
line: 18 (format “Newest file : %
File Date :%
” dateFiles[1][1] dateFiles[1][2])
– No ““get”” function for undefined
line: 20 (loadMAXFile dateFiles[1][1] quiet:true)
– No ““get”” function for undefined
Else, where did you mean i must use ascend == ?
(if you use ascend == true then you get the latest file in first array)
Do you know where i’m totallly false?

theFiles= getfiles (“C:\manue ests\108_132*.max”)
dateFiles= for i in theFiles collect #(i,getFileModDate i)

fn compareString str1 str2 x:1 ascend:true = – this is important function when using qsort later
(
local sc= stricmp str1[x] str2[x]
case of
(
(sc < 0.): (if ascend then 1 else -1)
(sc > 0.): (if ascend then -1 else 1)
default: 0
)
)

qSort dateFiles compareString x:2
format “Newest file : %
File Date :%
” dateFiles[1][1] dateFiles[1][2]

loadMAXFile dateFiles[1][1] quiet:true

Sorry K4noe,
i’m so stupid that i’d forget to put the double slashs in the path…
anyway, sometimes when there’s more than 3 scenes, he don’t find the last but 1 or 2 scenes before, even if i i try getFileCreateDate… any idea?
Anyway, thanks!!

Hello denisT,
Thanks too for your reply, when i run it; i’ve got this answer:
sortByDateTime()
undefined
#(“C:\manue ests\108_132\108_132.max”, “C:\manue ests\108_132\108_132_BLK_v000 (2).max”, “C:\manue ests\108_132\108_132_BLK_v000.max”, “C:\manue ests\108_132\108_132_BLK_v001.max”, “C:\manue ests\108_132\108_132_BLK_v002.max”, “C:\manue ests\108_132\108_132_BLK_v003.max”, “C:\manue ests\108_132\108_132_BLK_v004.max”)
undefined
undefined
– Error occurred in anonymous codeblock; filename: C:\manue\openLastModifiedDate2.ms; position: 738; line: 27
– Unable to convert: undefined to type: FileName
OK
Can you explain to me why we are searching for xml files?

fn sortByDateTime v1 v2 = -(v1[3].Compare v1[3] v2[3])

files = undefined
filesArr = getFiles  ("C:\\manue\	ests\\108_132\\*.max")
if filesArr.count &gt; 1 do
(
	doc = dotNetObject "system.xml.xmlDocument"
	if (try(doc.Load f) catch(#failed)) != #failed then
	(
		if (recent = doc.selectnodes "//MaxApp.RecentDocument").count &gt; 0 do
		(
			files = for k=1 to recent.count collect
			(
				max_file = recent.item[k-1].ChildNodes.item[3].InnerText 
				if not doesfileexist max_file then dontcollect else 
				(
					#(k, max_file, (dotnetobject "System.IO.FileInfo" max_file).LastWriteTime)
				)
			)
			qsort files sortByDateTime
		)
	)
	doc.Normalize()
)
files

loadMAXFile files quiet:true

my function works for my code.

i parse xml file to get a list of most recently opened max files. after that i sort them by last writing in time

ok i see, but where do you put the path or folder to search?

@manue, please use code wrapping before you writte the code, like this
“[-code]–your code[-/code]”… Without strip…

To make the code look “beautiful” in this forum… Also what do you mean by 3 scenes? I dont get i what do you mean by it, since theres only one recent (newest) file per directory (in your case max files) … Its little different if theres 3 folder, you can get 3 newest file at those dir. Maybe you could take screen shot and doodle in it to describe what do you want to achieve.

For denis code it should work if you know how to use it… But it look into max xml recent history, outside it, it wouldnt work, like for custom folder thats max never open before… Or in extreme case you just install new 3dsmax, or you reinstall windows…

Thanks for the code wrapping, i understand now how it’s possible to send clean code
The problem was; if he find many find files in the folder, he did’nt open the last one by modification date,
but one before last,
I need to test it more on other folders and for the moment i can’t as i’m working on an 32 bits!
denisT’s code seam very interresting but too hard for me

Page 1 / 2