Notifications
Clear all

[Closed] How create a external File?

Hi Guys,

I want create a File, for example “data.txt”. And i want this text as data.

Name

Level

[grid]
1;Ux;Uy;Uz;45
2;Ux;Uy;Uz;45
3;Ux;Uy;Uz;45
4;Ux;Uy;Uz;45
[end]

Thanks for help.

mfg
hot chip

15 Replies

I just became interested in this as well, and after a quick search a few days ago, I came across this bit of code. I can’t remember where I saved it from, so if you know who wrote it, chime it. ( :

This script exports the currently selected models height, width, and length to a .csv file.


 -------------------------------------------
 -- ExportAsCVS.ms
 -- exports the currently selected models
 -- height, width, length (in that order)
 -------------------------------------------
 global theFileStream
 fn exportAs fileName = (
 -- create file
 theFileStream = createFile fileName;
 
 if theFileStream != undefined then (
 -- file ok to write
 selection = getCurrentSelection()
 
 if selection.count > 0 then (
 -- write header
 printToFile ("x, y, z, Pieces, Name")
 -- write data
 for obj in selection do (
 _name = obj.name
 _height = floorToNDecimals(obj.height) as string
 _width = floorToNDecimals(obj.width) as string
 _length = floorToNDecimals(obj.length) as string
 -- assuming pieces is always 1
 _pieces = "1"
 -- write to file
 printToFile (_length+","+_width+","+_height+","+_pieces+","+_name)
 )
 ) else (
 -- error
 printToFile ("Error: Nothing Selected!")
 )
 )
 printToFile ("Yeah!")
 
 -- close
 close theFileStream;
 )
 
 fn printToFile theString t:0 = (
 -- auto tab (empty string)
 tabString = " ";
 tabString = substring tabString 1 (t*4);
 -- print
 format "%%
" tabString theString to:theFileStream;
 )
 
 fn floorToNDecimals theNumber N:2 = (
 return floor( ((pow 10 N) * theNumber) ) / (pow 10 N)
 )
 
 exportAs("Test2.csv");
 

Loading in the values can be done thru the ‘stringStream’ thingy in maxscript. Check the help.

Hi Garrik,

thank you very much, i will try that.

mfg
hot chip

 PEN
f=createFile ((getDir #scripts)+"myFileName.txt")
format "data: %" "some thing to put in the file" to:f
close f
 lo1

If you have all your text in array before writing I find that


(dotnetClass "System.IO.File").WriteAllLines <filename> <string array>

works much faster

1 Reply
 PEN
(@pen)
Joined: 11 months ago

Posts: 0

So this is faster then format? I’ll have to give it a try. Thanks.

Oh guys,

thank you very much. I will try the codes.

mfg
hot chip

So this is faster then format? I’ll have to give it a try. Thanks.if you’re interested in speed, my guess would be that formatting everything to a stringstream and then formatting the resulting stringstream to a file would be faster (and safer than regular formatting to a file too). My reasoning is that the array of mxs strings wouldnt need to be converted to a dotnet string array before the WriteAllLines function can execute which i dont think scales as well as the stringstream method for large outputs. Please note that my thoughts have not been benchmarked…

1 Reply
(@denist)
Joined: 11 months ago

Posts: 0

my guess is that format has to be faster if you put only one string (it doesn’t matter how long the string is).

however if the task doesn’t need any particular file format i always save(export) data to XML. it might be slower at the write/read stage but much faster at parse/search.

I would second that, XML would be perfect for the sort of thing you are trying to store. My thinking – Is performance really an issue for strings of this length? I wouldn’t think so.

(I don’t mean to hijack this thread, and I think this relates closely with creating an external file)
I’m writing data to an external file, then trying to load that data back in. This is the code so far:


    f=createFile "test.objekt"
    format "line1:%" "data1
" to:f
    format "line2:%" "data2" to:f
    close f
    o=openFile "test.objekt" mode:"r+"
    myData = readDelimitedString o "line2:"
    print myData
     
 Gives error: Unable to convert: &lt;File:C:.../test.objekt&gt; to type: String

I’m sure I’m missing something obvious, a syntax error. Or perhaps I’m not loading the data in? Hmm

 lo1

I’m not sure you can write to a file without supplying a full path. Is the file actually being created, and if so, where?

Page 1 / 2