ScriptSpot

Forum topic · General Scripting

How can I replace a string inside a file?

By alexmbr · 2007-03-26

Description

Hi. I need some help with replacing a string inside a file.

I need to open an OBJ file and change this mtllib ./occlusion02.mtl    into this     mtllib occlusion02.mtl  , removing the "./"

How can I do that?

Thanks

Comments (6)

WillyWonka · 2007-03-29

This is my stab at it. I have no idea if this code works at all but it should be a good starting point:

 

function changeFile file = (

    fileHandle = openFile file mode:r

    position = skipToString fileHandle "./occlusion02.mtl"

    if position == undefined then (

        -- This string isn't in the file

        close fileHandle

        return true

    ) else (

        -- Now you know where the string is in the file, and you can output

        -- a copy of the file with the new string in that location

    )

    close fileHandle

    return true

)

 

files = getFiles "c:\\foo\\*.obj"

for f in files do (changeFile f)

 

alexmbr · 2007-03-31

So, if I replace that string, everything else will stay unchanged?

WillyWonka · 2007-03-31

Only if an obj file is text and not binary.  I don't know if it is or not.  You may just want to import it into max and adjust the textures before exporting it again.

 

Zbuffer · 2007-03-26

Hi,

 Just open the .obj file in a text editor (notepad) and change the line.

 

alexmbr · 2007-03-26

I need to do this with maxscript, because I have several hundres of OBJ models.

alexmbr · 2007-03-29

Anyone?