Notifications
Clear all

[Closed] create a new text if previuos one exist

Hi everybody

I did my best with this issue but i couldn’t go any further

rollout PS "Text_create" width:200 height:320
  (
  button text_Save "Save_Text" pos:[10,10] width:90 height:30
  listbox text_list "" pos:[10,92] width:142 height:12
  button text_clear "Clear_Text_list" pos:[10,260] width:80 height:20
  button lis_ren "Rename_Text" pos:[103,260] width:80 height:20
  edittext Text_name "" pos:[6,290] width:134 height:20 
 	 
  global PName= ()
 
 ----------------------------------------------------------------------button to rename	 
 on lis_ren pressed do
 	(
 		text_list.selected=Text_name.text
 	)
 
 ----------------------------------------------------------------------button to clear	 
  on text_clear  pressed do
 	(	
 	text_list.items = #()	 
 	)		  
 ---------------------------------------------------------------------button to create	 
  on text_Save pressed do 
 	 (		
 		 The_text =((GetDir #export)+"/MyText.txt")			
 		 ini_file =createfile The_text	
 		 ToThe_text = openFile The_text mode:"at"
 		 
 		 /*
 		 if ( doesFileExist	"C:\001\\folder\script.ms"+index as string 	== true)then 
 			( while (doesFileExist	"C:\001\\folder\script.ms"+index as string == true) do
 			 ( 
 				 index+=1
 			 )
 		 )
 		*/
 				  
 		 for o in selection do
 			 (
 			 if selection.count !=0 then 
 			  (
 			  format  "\"%
"  o.name  to:ToThe_text		
 			  )
 			 else (messageBox "Nothing selected !
 Please select something" title:"3ds Max")
 			 )
 		 close ToThe_text
 			 
 		if PName != "" then
 		(	
 		global PName = Text_name.text
 		text_list.items = append text_list.items PName
 		)	 
 	 )
 
  -----------------------------------------------------------------------------------------Listbox
  on text_list doubleClicked itm do
 		  (
 		fload=((GetDir #export)+"/MyText.txt")		 
 		ff=openfile fload								
 		for i=1 to selection.count do 					
 			(
 				print fload
 			)
 		close ff 
  )
  
  )
  createdialog PS

you have to select an object and then run the script
what i need is :
1_at each button press it creates a new text file and if the previous does exist the next doesn’t overweight it adds another text with number
2_and when rename button pressed the created text selected in listbox get renamed as well
3_Double click on listbox item will execute the selected text in listbox

Any help at any part is so appreciated

Thanks in advance

5 Replies

If you don’t know what files are on a given folder you could use a something like the following function to get any existing files which matches the prefix and suffix format you are using.

If you know what the last used index (suffix) is, you could store it in a variable and simply increment it instead of parsing all the files in that folder over and over again. 
(
    	fn SaveIncrementFile =
    	(
    		folderName = "C:/"
    		prefix = "myText_"
    		extension = ".txt"
    		
    		-- Get tthe files in the folder which matches the prefix
    		files = getfiles (folderName + "/" + prefix + "*" + extension)
    		
    		-- If there are any files, get the maximun index by converting
    		-- the sufix into an integer
    		index = amax (for j in files collect (filterstring j "_.")[2] as integer)
    		
    		-- If there are no files then set the index to 0 else increment it by 1
    		if index == undefined then index = 0 else index += 1
    		
    		-- Create the suffix with a format of 3 diggits ie:001, 030, 125
    		digits = formattedPrint index format:"03u"
    		
    		-- Build the new file name
    		newFilename = folderName + "/" + prefix + digits + extension
    		
    		-- CODE TO SAVE YOUR FILE HERE --
    	)
    	
    	 SaveIncrementFile()
    )

Or:

(
 	fn SaveIncrementFile =
 	(
 		folderName = "C:/"
 		prefix = "myText_"
 		extension = ".txt"
 		
 		files = getfiles (folderName + "/" + prefix + "*" + extension)
 		
 		index = -1
 		for j in files where (idx = (filterstring j "_.")[2] as integer) > index do index = idx
 		
 		digits = formattedPrint (index+=1) format:"03u"
 		
 		newFilename = folderName + "/" + prefix + digits + extension
 		
 		-- CODE TO SAVE YOUR FILE HERE --
 	)
 	
 	 SaveIncrementFile()
 )

Here is a simpler way. Assuming getfiles() function will always return a sorted array of files, you could use the following function which should be much faster. If its not sorted you can sort the array.

You could also use .Net, but I preferred to stick to Maxscipt. 
(
  	fn SaveIncrementFile folder: prefix: extension: =
  	(
  		files = getfiles (folder + "/" + prefix + "*" + extension)
  		sort files
  
  		index = -1
  		if files.count > 0 do index = (filterstring files[files.count] "_.")[2] as integer
  
  		digits = formattedPrint (index+=1) format:"03u"
  
  		newFilename = folder + "/" + prefix + digits + extension
  
  		-- CODE TO SAVE YOUR FILE HERE --
  		files
  	)
  
  	SaveIncrementFile folder:"C:/" prefix:"myText_" extension:".txt"
  )

Jorge Thanks so much for the reply

the second one is what I needed

Sorry if i act just like a lazy student because i really new with this kind of script searching in site and max help this was my best.

well we create a sequence of new text file, JUST for Example (forget about transform it is just an example),i want to put the .transform of object in the text to read later, when we move the object we need a new text to write new .transform in it,it is done.

Now i need the listbox understand the each created new text in order as its listitem double clicked on each item means new transform applied to the object

Don’t know maybe it is not possible this way, maybe we better use dropdownlist.

so i rewrite the script based on your correction up to now.


   
   rollout PS "Text_create" width:200 height:320
    (
    button text_Save "Save_Text" pos:[10,10] width:90 height:30
    listbox text_list "" pos:[10,92] width:142 height:12
    button text_clear "Clear_Text_list" pos:[10,260] width:80 height:20
    button lis_ren "Rename_Text" pos:[103,260] width:80 height:20
    edittext Text_name "" pos:[6,290] width:134 height:20 
   	 
    global PName= ()
   
   ----------------------------------------------------------------------button to rename	 
   on lis_ren pressed do
   	(
   	text_list.selected=Text_name.text
   	)
   ----------------------------------------------------------------------button to clear	 
    on text_clear  pressed do
   	(	
   	text_list.items = #()	 
   	)		  
   ---------------------------------------------------------------------button to create	 
    on text_Save pressed do 
   	 (		
   		 folderName = "C:/"
   		 prefix = "myText_"
   		 extension = ".txt" 		
   		 files = getfiles (folderName + "/" + prefix + "*" + extension) 		
   		 index = -1
   		 for j in files where (idx = (filterstring j "_.")[2] as integer) > index do index = idx 		
   		 digits = formattedPrint (index+=1) format:"03u" 		
   		 newFilename = folderName + "/" + prefix + digits + extension
   		ini_file =createfile newFilename	
   		ToMyLog = openFile newFilename mode:"at"
   		Listbox_item_name= prefix + digits	----text file name as string
   				  
   		for o in selection do
   			 (
   			 if selection.count !=0 then 
   			  (
   			  format  "\"%
"  o.name  to:ToMyLog 	
   			  )
   			 else (messageBox "Nothing selected !
 Please select something" title:"3ds Max")
   			 )
   		 close ToMyLog 
   			 
   		if PName != "" then
   		(	
   		global PName = Listbox_item_name
   		text_list.items = append text_list.items PName		---adding the text file name as listbox item
   		)	
   		 
   	 )
   
    ------------------------------------------------------------------------------------double click on Listbox
    on text_list doubleClicked itm do
   		  (
   		fload=((GetDir #export)+"/MyText.txt")		 
   		ff=openfile fload								
   		for i=1 to selection.count do 					
   			(
   				print fload
   			)
   		close ff 
    )
    
    )
    createdialog PS

All remain is double click on listbox item
I do my best on “text_list doubleClicked”

Thanks again

Listbox.item as string

Thanks Jorge now the script works fine

If I’m no wrong when listbox.item get double clicked it searches in the scene and look for a node but our text(listbox items) is a text file in a hard drive and it must be open
“openFile…” so i convert listbox.item (which is in array) to string then we run your files script line
sth like this

------------------------------------------------------------------------------------double click on Listbox
 ListBox_Array = (text_list.selected) as string
 print ListBox_Array -- it print "the listboxitem name without .txt "	 SO
 extension = ".txt" 
 folderName = "C:/"
 ff = openFile (folderName + "/" + ListBox_Array + extension)	-- Now we open the selected file
 ................
 

Now to complete the script:
1_How to rename the text file in hard drive with the Edittext
2_And how to delete the existing text file in hard drive by pressing the —Clear_Text_list– button

Any help is so appreciated

Thanks in advance

How to close an already opened file

makeDir @"c:/Test_Folder/" all:true
 
 My_File= createFile "c:\\Test_Folder\\My_test_text1.txt"	----It creats My_test_text.txt as a file in the folder
 format "First line
Second line
Third line" to:My_File			----we writs some lines in MyFile
 deletefile "c:\\Test_Folder\\My_test_text1.txt" -- it returns false
 -- When we Create a text it means it is still open so we can't delete it, we must close and delete or rename SO
 
 close My_File	--OK
 renamefile "c:\\Test_Folder\\My_test_text1.txt" "c:\\Test_Folder\\My_test_text01.txt" -- true
 deletefile "c:\\Test_Folder\\My_test_text01.txt"	--Now it gets true
 
 --Now look at this
 My_File= createFile "c:\\Test_Folder\\My_test_text23.txt"	
 close My_File
 My_File= createFile "c:\\Test_Folder\\My_test_text24.txt"	
 close My_File
 
 openFile "c:\\Test_Folder\\My_test_text23.txt"	mode:"at"	
 close "c:\\Test_Folder\\My_test_text23.txt" --it can't be closed how to close this file
 
 createFile "c:\\Test_Folder\\My_test_text25.txt"
 close "c:\\Test_Folder\\My_test_text25.txt"	----it can't be closed how to close this file
 
 --it can't be rither delete or rename. 

How to rename and delete the 23 and 25
in Upcoming script we sucssesfully create sequence of Text files but we don’t have
myfile=…,myfile2=…,myfile3=… so how to close all sequences of the files.

Thanks in advance