FILE.COPY fail

FreemanFreeman Member Posts: 28
Hi, Navision gurus.

I have problem with coping files which is busy by third party program.

Here is the description:

Third party program generate files into defined directory (let it be ImportFrom directory). Every 5 minutes Navision must scan ImportFrom directory and if any files found - read them and move to another directory (Archive directory).
If Navision try read file which is exist but not ready to copy (let it be CurrentFile) - it is still generates by third party program (still writing data) - Navision generates run-time error and this is normal, I handle this error, so haven't interruption. But (here is the problem) when file generation complete and after next 5 minutes Navision read same file (CurrentFile) again, history repeating - Navision still perceive this file as non-readable and can not read it! ](*,)

Here is the code from OnTimer trigger of form:
        // Clear and reset list of import files:
        CLEAR(FileList);
        FileList.RESET;
        FileList.SETCURRENTKEY(Path);
        FileList.SETRANGE(Path, '');
        FileList.SETRANGE("Is a file", TRUE);
        IF FileList.FIND('-') THEN ;

        // Open and read Excel files:
        FileList.RESET;
        FileList.SETCURRENTKEY(Path);
        FileList.SETRANGE(Path, ImportFromDirectory);
        FileList.SETRANGE("Is a file", TRUE);
        IF FileList.FIND('-') THEN BEGIN
          REPEAT
            // Copy import file from Import From Directory (remote computer) into Archive Directory (local computer)
            // erase import file from Import From Directory (remote computer)
            // and then safely read import file form Archive directory (local computer):
            IF [b]FILE.COPY[/b](ImportFromDirectory+'\'+FileList.Name, ArchiveDirectory+'\'+FileList.Name) THEN BEGIN  // here is problem
              IF NOT FILE.ERASE(ImportFromDirectory+'\'+FileList.Name) THEN
                LogFileGlob.WRITE(FORMAT(TIME, 0, '<Standard Format,0>') + ';' +
                                  'err' + ';' +
                                  ImportFromDirectory+ '\'+FileList.Name + ';' +
                                  '0;' +
                                  'Could not erase file ' + ImportFromDirectory+ '\'+FileList.Name + ';')
              ELSE
                IF FORSDataImport(ArchiveDirectory+'\'+FileList.Name) THEN
                  CurrForm.UPDATE(FALSE);
            END ELSE
              LogFileGlob.WRITE(FORMAT(TIME, 0, '<Standard Format,0>') + ';' +
                               'err' + ';' +
                               ImportFromDirectory+ '\'+FileList.Name + ';' +
                               '0;' +
                               'Could not move file ' + ImportFromDirectory+ '\'+FileList.Name + ' to archive directory;');
          UNTIL FileList.NEXT = 0;
        END;

FileList is a record with SubType=File
LogFileGlob is log file (not important)
FORSDataImport is function which read Excel file and write data into Navision table.


For this task I must use Navision 3.6 RU SP4 on Windows XP SP3, SQL Server 2000, MS Excel 2003.

Any help will be appreciated!
Thanks in advance!

Comments

  • FreemanFreeman Member Posts: 28
    I have solve this problem by adding checking is import file busy by other program before FILE.COPY command. Do this checking by command FILE.RENAME.
  • ara3nara3n Member Posts: 9,256
    you can also do this another option that is less computing intensive.

    Try to do ChangeDatetime on the file. The function is SETSTAMP.

    if SETSTAMP(filename,newdate,newtime) then begin
      //run your code
    
    end;
    
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • FreemanFreeman Member Posts: 28
    ara3n wrote:
    you can also do this another option that is less computing intensive.

    Try to do ChangeDatetime on the file. The function is SETSTAMP.

    if SETSTAMP(filename,newdate,newtime) then begin
      //run your code
    
    end;
    


    Thanks, ara3n.
    Seems to me your solution is better =D>
  • ara3nara3n Member Posts: 9,256
    you are welcome.
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • FreemanFreeman Member Posts: 28
    Hi all, again.

    Previous solution failed - because customer changed requirement ](*,) ](*,) ](*,)
    Now the file which I need to read will always have read only access. It mean that previous solution would not work - SETSTAMP will always return false.
    So, how can Navision understand is the file not busy by other program if I can not use SETSTAMP or RENAME?

    Any help will be appreciated!
    Thanks in advance!
  • ara3nara3n Member Posts: 9,256
    Follow the previous advise and try and copy.
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • FreemanFreeman Member Posts: 28
    ara3n, thank you for you attention, your previous advise was use SETSTAMP to determine is file busy by other program. But in my case, SETSTAMP will always return false because file always will have read only permission. So, unfortunately, it is not useful for me.
    Copy also not useful because if file is busy, copy will create file with size=0 and hold access until Navision restart. In my task import must work 24\7 without any control from users.
  • ara3nara3n Member Posts: 9,256
    use windows host script automation and try and copy it.
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • FreemanFreeman Member Posts: 28
    ara3n wrote:
    use windows host script automation and try and copy it.

    I have created such simple code in test codeunit:
    CREATE(WSH);
    WSH.CopyFile('C:\test.xls', 'C:\archive\test.xls');
    CLEAR(WSH);
    MESSAGE('done');
    

    Where WSH is Windows Script Host.

    It works! It works when file not used by any programs and it works if file opened in Excel.
    Thanks, ara3n!

    But, in my case file can be opened programmatically in Excel by third party program for exporting data from this third party program. So Navision can open this file in the moment when file is still writing... How to define file opened for writing and still is writing or file opened only for reading? Guess, this issue I should discuss with my customer...
Sign In or Register to comment.