How can I replace a string inside a file?

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

Comment viewing options

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

This is my stab at it. I

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's picture

So, if I replace that

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

WillyWonka's picture

Only if an obj file is text

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's picture

Hi,  Just open the .obj

Hi,

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

 

alexmbr's picture

I need to do this with

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

alexmbr's picture

Anyone?

Anyone?

Comment viewing options

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