[Closed] Format not outputting what i've specified
This is probably something really small that i’ve made a mistake on but could someone have a look? I’m loading in a file that is basically 4 columns of numbers and i’m reading it back out and getting some undefined.
Heres the part of the script:
/*
vert file combiner
*/
if DDE_MFC != undefined do (closerolloutfloater DDE_MFC)
DEBUGGER= format
pos=[0.0,0.0,0.0]
rollout myroll "Geometry Loader"
(
button file_but "Geometry File" width:180 enabled:true
on file_but pressed do
(
local motionPath
local ValArray = #()
local strStore = "" as string
-- choose a file and open it
motionPath = (getOpenFileName "Choose Geometry Data File") as string
files = openFile motionPath
-- do until we hit the end of the file
while (eof files==false) do
(
-- read in a line from the file
local Search = (readline files) as string
for x=1 to Search.count do
(
o = Search[x] as string
-- if we hit a break... store the completed string
if o==" " do
(
append ValArray strStore
strStore = ""
o = ""
)
strStore += o as string
)
-- add that last string
--append ValArray strStore
-- cast our values to relevent types
local pn =ValArray[1] as string --pn is the vertex number
local x = ValArray[2] as string
local y = ValArray[3] as string
local z = ValArray[4] as string
-- print the result
format "% % % %
" pn x y z
-- nullify the worker variables
ValArray=#()
strStore = "" as string
)
)
)
DDE_MFC = newrolloutfloater "Vert File Combiner" 250 280
addrollout myroll DDE_MFC rolledup:false
This is the last few lines that i’m inputting:
35000757 -1.13001 0.786284 1.23207
35000758 -1.12743 0.805144 1.17828
35000759 -1.09947 0.809579 1.17041
This is what the last few lines of the output look like:
35000757 -1.13001 0.786284 undefined
35000758 -1.12743 0.805144 undefined
35000759 -1.09947 0.809579 undefined
undefined undefined undefined undefined
Thanks in advance guys…really, really appreciate the help!
Not sure if this is the cause, but you’ve commented out this line?
--append ValArray strStore
Also, casting the ValArray items to string seems redundant; aren’t they strings already?
Aaaarrrrgghh…you’re right…that was it! Uncommented it and it worked fine! I meant to ask earlier…is it possible to combine all files in a folder into one file? In laymans terms i want to be able to click a button navigate to a folder where all these text files are and have maxscript read them in one by one and then print them all into one file with a line break in between each set of data (from the files). This is kind of out of my league…which is why i’m doing it manually with the above code…reading it all to the listener and then copying it out! Would love to understand how to do this…its an obvious time saver!
How about something like this?
(
local files = getFiles "C:/mypath/*.txt"
local output = createFile "C:/myfile.txt"
for f in files do (
local input = openFile f
format "%" (readChars input -1 errorAtEof:false) to:output
close input
format "%" "
" to:output
)
close output
)
I haven’t tested it, and you might be able to do it more efficiently, faster and compact. But it’s a start.
oops too slow
(
-- generate some files
local theFolder = "C:/Temp/"
local theFilePrefix = "File_"
for f = 1 to 50 do
(
local theFile = (createfile (theFolder + theFilePrefix + (formattedPrint f format:"03u") + ".dat"))
for k = 1 to 20 do
(
format "File: %, Line: %, RandomValue: %
" f k (random 0 5000) to:theFile
)
close theFile
)
--parse all the files and combine in a single file
local theOutPutFile = createfile (theFolder + "AllFiles.txt")
local fileList = getfiles (theFolder + "*.dat")
for i = 1 to fileList.count do
(
format "
Reading file: %
" fileList[i]
local theInputFile = openfile fileList[i]
seek theInputFile 0
while not eof theInputFile do
(
local theLine = (readLine theInputFile)
print theLine
format (theLine + "
") to:theOutPutFile
)
)
close theOutPutFile
)