Update Export File

SV_WillSV_Will Member Posts: 12
Hi there,

within a CodeUnit I generate a File "Production.csv". I would like to add a new line everytime into the file I run the CodeUnit!

But without the

IF EXISTS(FileNameProdZahlen) THEN
ERASE(FileNameProdZahlen);

I get the Error "The File does already exist!"

Possible?

Thanks a Lot !!

Comments

  • SavatageSavatage Member Posts: 7,142
    not sure but it might be somehting like this (snipit from how i can keep exporting info to the same file and the file just keep builing upon itself.
    Hopefully this can give you an idea. I believe if you search too there was a similar post a while back that you possibly could find. Perhaps search for Add to existing text file :-k
    OpenMyFile() Status : Boolean
    FormatMyFileName;
    MyFile.TEXTMODE(TRUE);
    MyFile.WRITEMODE(TRUE);
    IF MyFile.OPEN(MyFileName) THEN BEGIN
      BeginFileLength := MyFile.LEN;
      MyFile.SEEK(BeginFileLength);
      MyFile.TRUNC;
      // write your field w/code here
      EXIT(TRUE);
    END;
    
    MyFile.CLOSE;
    

    here are some more posts
    http://www.mibuso.com/forum/viewtopic.php?t=4706
    http://www.mibuso.com/forum/viewtopic.php?t=13457
    http://www.mibuso.com/forum/viewtopic.php?t=8364

    probably more if you search
  • XypherXypher Member Posts: 297
    I believe you are looking more for something like this...
    OnRun()
      MyFile.TEXTMODE(TRUE);
      MyFile.WRITEMODE(TRUE);
    
      IF EXISTS(FileNameProdZahlen) THEN
        MyFile.OPEN(FileNameProdZahlen)
      ELSE
        MyFile.CREATE(FileNameProdZahlen);
    
      IF MyFile.LEN > 0 THEN
        MyFile.SEEK(MyFile.Len);
    
      MyFile.CREATEOUTSTREAM(oStream);
    
      oStream.WRITETEXT('CodeUnit Access: ' + FORMAT(CURRENTDATETIME) + ' / ' + USERID);
      oStream.WRITETEXT();
    
      MyFile.CLOSE;
    
  • jversusjjversusj Member Posts: 489
    I am having some trouble with this.

    I have created a Report with nested dataitems. a line of code in our business logic will call this report if a certain condition is met. The report will create a CSV file if none exists, and append to the CSV file when it does exist.

    Creation of the file is clockwork. no problems. I am, however, having issues with the append. I have tried both Savatage and Xypher's suggestions, but each time i am getting appending at the start of the file, and that is no good.

    I have three data items.
    Integer (OnAfterGetRecord I write field label data, but only when the file is being created)
    Sales Header (OnAfterGetRecord, I fill some variables with Sales Header values)
    Sales Line (linked to the header, on AfterGetRecord, I fill some var. w/ data from tables linked to sales line and do my outstream.write).

    On PreReport, I have the following code:
     //Mibuso sample code for appending to an existing file.
    POFile.TEXTMODE(TRUE);
    POFile.WRITEMODE(TRUE);
    
    IF FileForExport THEN
      POFile.OPEN(sFileName)
    ELSE
      POFile.CREATE(sFileName);
    
    IF POFile.LEN > 0 THEN
       POFile.SEEK(POFile.LEN);
    
    
    POFile.CREATEOUTSTREAM(POOutstream);
    
    
    FileForExport() FileFound : Boolean
     //temporary hard-coded directory to store CSV files for upload to Customer
    sFilePath := '\\fps\test\';
    
    CLEAR(recPOFile);
    recPOFile.SETFILTER(Path,'%1',sFilePath);
    recPOFile.SETFILTER(Name,'%1','@*.csv');
    recPOFile.SETRANGE("Is a file",TRUE);
    IF NOT recPOFile.FIND('-') THEN BEGIN
       sNoSeries := 'WM_POTRACK';
       NoSeriesMgt.InitSeries(sNoSeries,sNoSeries,0D,sBatchNo,sNoSeries);
       sFile := OutVendorNo + '_' + sBatchNo + '.csv';
       sFileName := sFilePath + sFile;
       bCreateCSV := TRUE;
       EXIT(FALSE)
    END ELSE BEGIN
       sFileName := sFilePath + recPOFile.Name;
       EXIT(TRUE);
    END;
    

    it is still early draft of code, so forgive the hard-coding.

    my issue is that when i open the existing file and seek the LEN, I begin writing at the start of the file, not the end. I assume I am missing a line of code somewhere, but my search of the forum only talks about using LEN and going from there. Any thoughts on what i am doing wrong?

    thanks!
    kind of fell into this...
  • DaveTDaveT Member Posts: 1,039
    Hi,

    A file.SEEK to LEN should be fine. here's a test with your code..
    POFile.TEXTMODE(TRUE);
    POFile.WRITEMODE(TRUE);
    
    POFile.OPEN('C:\users\public\dave.txt');
    
    IF POFile.LEN > 0 THEN
       POFile.SEEK(POFile.LEN);
    
    
    POFile.WRITE( ' Inserting time ' );
    POFile.WRITE( ' Time - ' + FORMAT( TIME ) );
    
    POFile.CLOSE;
    

    You have not shown the writing code - is it possible that another seek is affecting it?
    Dave Treanor

    Dynamics Nav Add-ons
    http://www.simplydynamics.ie/Addons.html
  • jversusjjversusj Member Posts: 489
    DaveT wrote:
    Hi,

    A file.SEEK to LEN should be fine. here's a test with your code..
    POFile.TEXTMODE(TRUE);
    POFile.WRITEMODE(TRUE);
    
    POFile.OPEN('C:\users\public\dave.txt');
    
    IF POFile.LEN > 0 THEN
       POFile.SEEK(POFile.LEN);
    
    
    POFile.WRITE( ' Inserting time ' );
    POFile.WRITE( ' Time - ' + FORMAT( TIME ) );
    
    POFile.CLOSE;
    

    You have not shown the writing code - is it possible that another seek is affecting it?
    thank you!

    here is my code for writing. it occurs in the sales line dataitem. i was using Outstream, and maybe that is my problem? your example shows POFile.WRITE....but i do a POFile.CREATEOUTSTREAM.
    POOutstream.WRITETEXT(OutVendorNo + ',' +
                          OutPONo + ',' +
                          OutStoreNo + ',' +
                          OutSONo + ',' +
                          FORMAT(OutEstShipDate) + ',' +
                          FORMAT(OutActShipDate) + ',' +
                          FORMAT(OutEstDelDate) + ',' +
                          OutShipName + ',' +
                          OutShipAddress + ',' +
                          OutShipCity + ',' +
                          OutShipState + ',' +
                          OutSCAC + ',' +
                          OutProNo + ',' +
                          OutBOLNo + ',' +
                          OutPalletIDNo + ',' +
                          OutContainerType + ',' +
                          OutContainerNo + ',' +
                          FORMAT(OutQtyContainer) + ',' +
                          OutWMItemNo + ',' +
                          OutItemNo + ',' +
                          OutItemDesc + ',' +
                          OutWMCompItemNo + ',' +
                          OutCompItemNo + ',' +
                          OutCompItemDesc + ',' +
                          FORMAT(OutQtyOrdered) + ',' +
                          FORMAT(OutQtyShipped) + ',' +
                          OutStatus + ',' +
                          OutShipCondition + ',' +
                          OutPartsType
                         );
    //Without following WRITETEXT, the text file that is created will be one continuous string. This breaks the line.
    POOutstream.WRITETEXT();
    

    there is no other SEEK.
    kind of fell into this...
  • DaveTDaveT Member Posts: 1,039
    Hi,

    I generally use the WRITE command with text and use the OUTSTREAM with binary etc. I haven't tried a SEEK/OUTSTEAM combination before but it should work.

    Two suggestions:
    1. Use the WRITE command
    2. Put the SEEK after creating the OUTSTREAM.
    Dave Treanor

    Dynamics Nav Add-ons
    http://www.simplydynamics.ie/Addons.html
  • jversusjjversusj Member Posts: 489
    DaveT wrote:
    Hi,

    I generally use the WRITE command with text and use the OUTSTREAM with binary etc. I haven't tried a SEEK/OUTSTEAM combination before but it should work.

    Two suggestions:
    1. Use the WRITE command
    2. Put the SEEK after creating the OUTSTREAM.

    hello, thanks again.

    I tried putting the seek after creating the OUTSTREAM, but with the same results. I will try your other idea next!
    kind of fell into this...
  • jversusjjversusj Member Posts: 489
    using the WRITE command instead of WRITETEXT did it! I got rid of the outstream and now my file appends as desired!

    \:D/

    thanks so much, DaveT!
    kind of fell into this...
  • DaveTDaveT Member Posts: 1,039
    Glad to Help :mrgreen:
    Dave Treanor

    Dynamics Nav Add-ons
    http://www.simplydynamics.ie/Addons.html
  • SavatageSavatage Member Posts: 7,142
    It's great when a plan comes together, isn't it! =D>
  • jversusjjversusj Member Posts: 489
    it sure is. this site is great for getting another set of eyes on a problem, and helped me through it, as usual.

    it did appear that the SEEK was working - i created some variables to show me what position it was on after doing the seek and the number looked correct. when it came to writing the output, however, it seemed to ignore the position altogether.
    kind of fell into this...
Sign In or Register to comment.