How to create a new file using DzFile ?

hello,

 

I'm having hard time trying to create a simple .ini file

I know how to read or write an existing file but cannot achieve to create a new one...

var path_file_daz_ini = " B:\\- ALBUM -\\Page 03\\5.Obj\\p03c08_joao.ini"function create_daz_ini(srcFilePath) {        var srcFile = new DzFile(srcFilePath);        print (srcFilePath)    if (!srcFile.open(DzFile.ReadOnly)) {        MessageBox.critical("Could not open file.", "Open File", "&OK", "");        return;    }            srcFile.close();    }     create_daz_ini(path_file_daz_ini)

 

thanks for your help

Post edited by ratus69_c6741fefbf on

Comments

  • hphoenixhphoenix Posts: 1,335
    edited February 2016

    The line

    if (!srcFile.open(DzFile.ReadOnly)) {

     

    needs a different access type.  DzFile.ReadOnly makes it only read/open from an existing file.  Use either DzFile.ReadWrite, or DzFile.WriteOnly, or DzFile.Append, or DzFile.Truncate.

    (see http://docs.daz3d.com/doku.php/public/software/dazstudio/4/referenceguide/scripting/api_reference/object_index/file_dz )

     

    Post edited by hphoenix on
  • rbtwhizrbtwhiz Posts: 2,217
    edited February 2016

    In your sample, your use of DzFile.ReadOnly should be replaced with DzFile.WriteOnly, DzFile.ReadWrite or DzFile.Append (as appropriate). Here is a tweaked version that will create a file named "testing.txt" at the root of the D drive:

    function createFile( sPath ) {	var oFile = new DzFile( sPath );	if( !oFile.open( DzFile.WriteOnly ) ){		MessageBox.critical( qsTr("Could not open \"%1\" for writing.)").arg( sPath ), qsTr("Error"), qsTr("&OK"), "" );		return;	}		oFile.close();};createFile( "D:/testing.txt" );

    You may also want to take a look at the Install Manager Config (from DAZ Studio) sample. Particularly the writeInstallManagerConfig function.

    -Rob

    Edit: Apparently I had this response open in a browser tab for nearly 45 min (cross-posted). Hmm... multi-tasking.

    Post edited by rbtwhiz on
  • ratus69_c6741fefbfratus69_c6741fefbf Posts: 32
    edited February 2016

    thanks to both of you hphoenix and Rob

    in fact I have used the DzFile.WriteOnly arg with an other example but with no success

    I have taken the function from Rob and put it in my script and it was not working but the whole example was...

    Finally I discovered that there were a space in my path between the " and the path : 

    " B:\\- ALBUM -\\Page 03\\5.Obj\\p03c08_joao.ini"

    very stupid error...

    anyway, thanks to both of you again !!

    Post edited by ratus69_c6741fefbf on
Sign In or Register to comment.