Options

What are the options to delete a file from NAV?

ReedbergReedberg Member Posts: 68
Hello all,
I am wokring with NAV3.6 Attain, NAV5 is used as a client.
The task is very simple: I have to delete a file from a folder.

I used the following code to delete a file:
FileSystemObjL - 'Windows Script Host Object Model'.FileSystemObject
IF ISCLEAR(FileSystemObjL) THEN
  CREATE(FileSystemObjL);
IF FileSystemObjL.FileExists(AttachPathL + AttachNameL) THEN
  FileSystemObjL.DeleteFile(AttachPathL + AttachNameL);
It works just fine under my computer. I am able to delete files located on a local computer and somewhere in the network.
However when my client runs this code he gets the following error message:
This message is for C/AL programmers: An exception was raised in method DeleteFile. The OLE control or Automation server has returned error (HRESULT) -214735267. The component did not provide the exception description.
I do understand that -2147352567 is a general Exception that has occurred on the DeleteFile component. The best way to solve it is to attach Visual Studio, enable Exceptions and locate the error. However this is not an option because my client won't let me install Visual Studio and do debugging.

The only solution I can think of is to delete a file using File table, but path in File table is limited to 98 characters, while the average path length in my client network is about 150 characters, so using File table is not an option.

So, is there a way to fix DeleteFile method?
If no, what are the other options to delete files under NAV?

Any suggestions will be very appreciated!

Answers

  • Options
    Luc_VanDyckLuc_VanDyck Member, Moderator, Administrator Posts: 3,633
    You can also use native C/AL code to delete a file:
    IF EXISTS(AttachPathL + AttachNameL) THEN
      IF ERASE(AttachPathL + AttachNameL) THEN;
    
    But if you can't delete the file through FileSystem-automation, I guess you also won't be able to delete it through the native NAV functions.
    No support using PM or e-mail - Please use this forum. BC TechDays 2024: 13 & 14 June 2024, Antwerp (Belgium)
  • Options
    william_akihisawilliam_akihisa Member Posts: 14
    ReedBerg,

    it' s simple, your code is correct using filesystemobject could access modify or change any file in network or local.
    but you need to install COMDLG32.OCX (library for client application) in each client which using that code you made
    some client which had visual studio installed not need to re-register it. but in end user client you must register it. no need
    to download the library because it' s already in the system folder.. don' t forget to run regsvr32 'comdlg32.ocx' for 32 bit and
    regsvr32 'syswow64 path\comdlg32.ocx' for 64 bit
  • Options
    ReedbergReedberg Member Posts: 68
    william_akihisa, COMDLG32.OCX is installed as a library on a client computer, so this is not the case.
    Luc Van Dyck, at least native function does not have limitation to 98 characters. I will try your solution and report the results here, may be it will work out.

    Thank you for your replies!
  • Options
    ReedbergReedberg Member Posts: 68
    Luc Van Dyck,
    Thank you very much for your reply.
    I used the native NAV functions EXISTS and ERASE. They didn't delete a file but at least they displayed a meaningful error message stating that a file cannot be deleted because it is used by another program.

    I didn't told the whole story in my first message for this post. The task was to create a file using NAS, send it via E-mail using SMPT codeunit and then delete the file. I failed on the deletion step. It occurred that the cause of the error was NAV bug:
    When you use the SMTP Mail codeunit (400) to send email messages that have attachments in Microsoft Dynamics NAV 5.0 Service Pack 1 (SP1), the attached files are not released until the Microsoft Dynamics NAV client is closed. Before you close the Microsoft Dynamics NAV client, if you try to delete the attached file, you receive the error message…

    The bug can be fixed by KB 2280492.
    After I implemented this KB to the client environment everything became fine and NAS hopefully started to delete files after sending them via e-mails.

    So, once again, thank your for your valuables replied, they helped me to solve my issue!
Sign In or Register to comment.