The operating system cannot access to the file

dgaberdgaber Member Posts: 13
Hi everybody,

I'm trying to create a log file. Basically write rows of text in .txt file. I've created a function, which is called everytime the condition to write into a file is met. the code is:


Name DataType Subtype Length
FileName Text 100
FileSystem File
WinFileSystem Automation 'Microsoft Scripting Runtime'.FileSystemObject

WriteLog(FText : Text[300])

FileName := 'D:\NAVADSYNC_LOG\test.txt';

IF ISCLEAR(WinFileSystem) THEN
CREATE(WinFileSystem);
IF NOT WinFileSystem.FolderExists('D:\NAVADSYNC_LOG') THEN
WinFileSystem.CreateFolder('D:\NAVADSYNC_LOG');

IF NOT FILE.EXISTS(FileName) THEN BEGIN
FileSystem.WRITEMODE(FALSE);
FileSystem.CREATE(FileName);
END;

FileSystem.TEXTMODE(TRUE);
FileSystem.OPEN(FileName);
FileSystem.SEEK(FileSystem.LEN);
FileSystem.WRITE(FText);
FileSystem.CLOSE;

When I run the function, I get the error:

"The operating system cannot access to the file D:\NAVADSYNC_LOG\test.txt. Verify that the file type and attributes are correct"

Debugger stops at the last line of the function "FileSystem.CLOSE". If I comment out the last line, function writes only the first row into the file, all other are left out.

I've tried to recode the function with CREATEOUTSTREAM, but the function always errors out at FileSystem.CLOSE statement. I also restarted the PC... to no success...

Does anybody have any suggestions on what I'm missing out?

Best Answers

Answers

  • kylehardinkylehardin Member Posts: 257
    Is this RTC? If so, then by default, all files are created on the NAV Service Tier server, not the local workstation.

    You have a couple of options: use a network share that both the workstation and the NST can see, or you have to create the file and then move it. There are good built-in functions in codeunit 419 to make the second option easier.

    If you go with the first option, then the NST service needs to run under a proper Active Directory account rather than NETWORK SERVICE.

    Actually, I should clarify a bit. Since you are using an automation to create the directory, that automation is is running on the client. Then your FileSystem.OPEN command comes along, but that runs on the NAV Service Tier. That's why you get the error - the directory was created on the client but the file was created on the NST.

    I'd skip creating the directory in code - this will run many times and it's just wasted effort. Just create that directory by hand on the NST.
    Kyle Hardin - ArcherPoint
  • archer89archer89 Member Posts: 337
    check, if the nav service account has write access to folder D:\NAVADSYNC_LOG.
    best regards
    Franz Kalchmair, MVP
    Alias: Jonathan Archer

    please like / agree / verify my answer, if it was helpful for you. thx.
    Blog: http://moxie4nav.wordpress.com/
  • dgaberdgaber Member Posts: 13
    Hey there, thank you for your replies.

    @kylehardin - It's not RTC it's classic v4.0 technically upgraded to v 2009.
    @archer89 - as mentioned in my original post - folder and file is created and first line of text gets written. Therefore I'm sure that the issue isn't with write access.

    A couple of months ago I wrote a short piece of code to store e-mail adresses in a .txt file. If you look below, functions are basically the same, with one crucial difference... The code below works! It stores a .txt file in a user definen folder (locally)...


    This one works without any problems:

    Name DataType Subtype Length
    FileTest File
    FileName Text 250
    FilePath Text 150
    Folder Automation 'Microsoft Shell Controls And Automation'.Folder
    rEmployee Record SKE Employee
    rTMP Record Usklajenost projekt - plače
    WindowsShell Automation 'Microsoft Shell Controls And Automation'.Shell


    rTMP.RESET;
    IF rTMP.FINDFIRST THEN REPEAT
    IF FilePath = '' THEN BEGIN
    IF ISCLEAR(WindowsShell) THEN
    CREATE(WindowsShell);
    Folder := WindowsShell.BrowseForFolder(0, 'Choose Save To folder!', 0);
    IF NOT ISCLEAR(Folder) THEN
    FilePath := FORMAT(Folder.Items().Item.Path)
    ELSE
    ERROR('Choose Save To folder!');

    FileName := FilePath + '\' + FileName;
    END;

    FileTest.TEXTMODE(TRUE);

    IF NOT FILE.EXISTS(FileName) THEN BEGIN

    FileTest.WRITEMODE(FALSE);
    FileTest.CREATE(FileName);
    END;

    FileTest.OPEN(FileName);
    FileTest.SEEK(FileTest.LEN);
    FileTest.WRITE(rTMP.Text1);
    FileTest.CLOSE;
    UNTIL rTMP.NEXT = 0;

    I find this hard to comprehend... I've been banging my hoead over this mystery over the weekend to no success. Any hint would be appreciated. As per advice I took out the piece of code which creates the folder and created the folder manually (locally). Still no success...

    Any help or hint would be appreciated.
Sign In or Register to comment.