FILE.COPY error on open file

BGIBGI Member Posts: 176
Hi all,
maybe stupid question but following scenario:
- I copy a file from pe. desktop to special folder using the navision command file.copy(origfilename,destfilename).
Now sometimes these files (pe. word doc) are still open by user, so he gets a error stating file is in use...so far no problem.
User closes word and want to copy again, but now error that destination file already exists.

This destination file indeed exists, and has 0bytes, and apparantly is held open by the navision client, because i cannot delete it.
Only when we close navision client, this file is being freed up and can be deleted.

Now my question:
- how can i see if a file is in use? (so i don't give the copy command).
- or can i force the destinationfile to be deleted?

Rgds
Benny
Rgds
Benny Giebens

Answers

  • SavatageSavatage Member Posts: 7,142
    EXISTS
    Use this function to determine if a file exists.
    [Ok :=] EXISTS(Name)

    ERASE
    Use this function to erase a file.
    [Ok] := ERASE(Name)

    You can always do a quick check & erase before you do the copy.
    (Example)
    If exists ('YourDrive:\YourDirectory\YourFile.txt') then
    erase ('YourDrive:\YourDirectory\YourFile.txt');
    >>your copy command here

    **depending on what's going on with this file you can use F1 help to read about
    Currfile.CLOSE; command

    We use it on a dataport to move a file from it's original folder to a saved folder once the data has been imported.
    here's an example.

    Text Constant
    FilePath = I:\Internet Data Feed\Completed Import\
    OnPostDataport()
    CurrFile.CLOSE; <<close the file
    SavedFileName := FORMAT(WORKDATE,0,'<Year4>'+'-'+'<Month Text,3>'+'-'+'<Day,2>')+'.txt'; <<Autocreate a date oriented file name
    FILE.COPY(CurrDataport.FILENAME,FilePath+SavedFileName);<<copy the file to new location
    ERASE(CurrDataport.FILENAME);<<delete file from original location
    
  • BGIBGI Member Posts: 176
    Hi Harry,
    i know those commands, no problem there.
    But do pe. the following:

    Open word document test1.doc in word.

    Do in navision: file.copy(test1.doc,copytest1.doc).

    You will get an error that file is open.
    Close word, and in the same navision session (without closing your navision client), do the copy a second time.
    You will get an error that copytest1.doc already exists.
    Try to delete copytest1.doc and it will not work.
    When you close the navision client, then the file copytest1.doc is deleted.

    Thats my problem....
    Rgds
    Benny Giebens
  • Luc_VanDyckLuc_VanDyck Member, Moderator, Administrator Posts: 3,633
    Maybe you can try to open the file in Writemode first. If that succeeds, you know that the file is not in use:
    File.WRITEMODE(TRUE);
    IF NOT(File.OPEN('c:\yourfile')) THEN
      MESSAGE('File does not exists or is in use');
    File.CLOSE;
    

    If it succeeds, you can close the file and copy it.
    No support using PM or e-mail - Please use this forum. BC TechDays 2024: 13 & 14 June 2024, Antwerp (Belgium)
  • BGIBGI Member Posts: 176
    Luc,
    indeed simple but working....even did

    File.open(...);
    File.close;
    file.copy(..)

    And it works ....stupid but it solves the problem.

    Thnx
    Benny
    Rgds
    Benny Giebens
  • andy76andy76 Member Posts: 616
    But FILE.CLOSE doesn't exist in NAV 5.0 Sp1.

    Is the file automatically closed or there is another method to close it?

    Thank you
  • Luc_VanDyckLuc_VanDyck Member, Moderator, Administrator Posts: 3,633
    File.CLOSE does exists in my version of NAV 5.0 SP1:

    Quoting on-line help:
    CLOSE (File)
    Use this function to close a file which has been opened by OPEN (File).

    File.CLOSE
    File

    Data type: file

    Use this variable to refer to the file.

    Comments
    If the file is not open, a run-time error will occur.
    But File.CLOSE refers to a variable with DataType = File.
    No support using PM or e-mail - Please use this forum. BC TechDays 2024: 13 & 14 June 2024, Antwerp (Belgium)
  • BGIBGI Member Posts: 176
    Yep,
    Also works in 5.1, that is the version we are using...
    Rgds
    Benny Giebens
  • andy76andy76 Member Posts: 616
    I think to have solved.

    I tried FILE.CLOSE(FileName) where filename was my text variable in the same way of FILE.ERASE(FileName) without using the variable of type FILE .

    Thank you
Sign In or Register to comment.