Maxscript write and read .txt file

So I'm making an exporter in maxscript
I have troubles writing the export path into a .txt file and reading it.
This is what I have:

----------------------------------------------------------------------------------
label labelExportFolder "Export folder" pos:[10,600]
edittext folderPathTxt "" pos: [10,620] width:140
button btnBrowse "..." pos: [160,620]
on btnBrowse pressed do
(
Global MyPath = "" -- this stores the path in string form

local dir = getSavePath caption:"path..."
if (dir != undefined) do ( folderPathTxt.text = dir )
append MyPath folderPathTxt.text as string -

maxRootDir = getdir #maxroot
global TextFileName = (maxRootDir +"\\ExportPath.txt")
-- if file doesn't exist, create one and store MyPath in it
if doesFileExist (maxRootDir +"\\ExportPath.txt") == false do
(
global TextFile = (createFile TextFileName)
format MyPath to:TextFile
close TextFile
)

-- if the .txt file does exist, read it and store the content in MyPath and put it in the edittext
if doesFileExist (maxRootDir +"\\ExportPath.txt") == true do
(
local replaceTxt = ""
MyPath = (openFile TextFileName mode:"r+")
folderPathTxt.text = MyPath
close TextFile
)
)
-------------------------------------------------------------------------------------------

So I hope it's clear what I'm trying to do.
I'm not sure whether I should use the TxtFile or TextFileName
when i do "close TextFile" or "MyPath = (openFile TextFileName mode:"r")"

I guess the TextFileName just contains the path to the file that is created,
but I don't know what the TextFile variable is exactly. I'm confused

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Elias Leonard's picture

ITS ALIVE!

finally got it working :)

fn setFOKINPATH =
(
if MyPath != undefined do
(
	local maxINI = getMAXIniFile()
	setINISetting  maxINI "EliasScriptSettings" "ExportDirectory" MyPath
)
)
 
--Get .ini
(
	local maxINI = getMAXIniFile()
	MyPath = getINISetting  maxINI "EliasScriptSettings" "ExportDirectory"
	messagebox "GOT IT!"
	print MyPath
)
 
rollout TestDialog "Dialog"
(
 
	label labelExportFolder "Export folder" pos:[10,10] 
	edittext folderPathTxt "" text:MyPath pos: [10,30] width:140
	button btnBrowse "..." pos: [160,30]
	on btnBrowse pressed do 
	(
		MyPath = getSavePath caption:"Choose export directory"
		if (MyPath != undefined) do 
		(
			folderPathTxt.text = MyPath 
		)
		-- setINISetting  MaxINI "EliasScriptSettings" "ExportDirectory" "test" -- !!ERROR HERE!!
		setFOKINPATH()
	)
)
createdialog TestDialog width: 200 height: 200
<code>
Elias Leonard's picture

Unable to convert: undefined to type: FileName

So I tried making using the .ini file from Max which is easier to handle.
(someone on polycount gave me that tip)
But now I get another error.
I Think this should be easy to solve if I only Knew How :p

this is the error: -- Unable to convert: undefined to type: FileName

global MyPath = ""
-- Set .ini
if MyPath != undefined do
(
	local maxINI = getMAXIniFile()
	setINISetting  maxINI "EliasScriptSettings" "ExportDirectory" MyPath
)
 
--Get .ini
(
	local maxINI = getMAXIniFile()
	MyPath = getINISetting  maxINI "EliasSettings" "ExportDirectory"
)
 
rollout TestDialog "Dialog"
(
	label labelExportFolder "Export folder" pos:[10,10] 
	edittext folderPathTxt "" text:MyPath pos: [10,30] width:140 text: MyPath
	button btnBrowse "..." pos: [160,30]
	on btnBrowse pressed do 
	(
		local dir = getSavePath caption:"Choose export directory"
		if (dir != undefined) do ( folderPathTxt.text = dir )
		MyPath = (folderPathTxt.text) as string     -- get edittext content and put in MyPath
		setINISetting  MaxINI "EliasScriptSettings" "ExportDirectory" MyPath -- !!ERROR HERE!!
	)
)
createdialog TestDialog width: 200 height: 200
<code>
Elias Leonard's picture

Update

Changed my code a bit, now I get this error:
--Runtime error: FileStream cannot create: E:\Max\3ds Max 2016\\ExportPath.txt

global MyPath = "no path"
maxRootDir = getdir #maxroot -- get max root file path
global TextFileName = (maxRootDir +"\\ExportPath.txt")

fn CreateTextFile =
(
global TextFile = (createFile TextFileName)
format MyPath to:TextFile
close TextFile
messagebox "created"
)
if doesFileExist (maxRootDir +"\\ExportPath.txt") == false do
(
CreateTextFile()
messagebox "file didn't exist, creating one"
)

label labelExportFolder "Export folder" pos:[10,600]
edittext folderPathTxt "" text:MyPath pos: [10,620] width:140 text: MyPath
button btnBrowse "..." pos: [160,620]
on btnBrowse pressed do
(
local dir = getSavePath caption:"path..."
if (dir != undefined) do ( folderPathTxt.text = dir )
MyPath = folderPathTxt.text as string -- get edittext content and put in MyPath
-- folderPathTxt.text = MyPath

fs = openFile TextFileName mode:"r+" --opens the file
format "put it iin" to:fs --this writes your text to the now open file
close TextFile

)

miauu's picture

.

I think that your problem is with write persmission. Your OS(UAC) not allows you to create files in max root folder, which is in C drive(I suppose). Use this and the script should work:

maxRootDir = getdir #export

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.