The operating system cannot access to the file

dgaber
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
\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?
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

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?
0
Best Answers
-
I'm not sure if this is the problem or not, but you shouldn't open the file if you are creating it. Try changing your code as follows...
FileSystem.WRITEMODE(TRUE); FileSystem.TEXTMODE(TRUE); IF NOT FILE.EXISTS(FileName) THEN FileSystem.CREATE(FileName) ELSE BEGIN FileSystem.OPEN(FileName); FileSystem.SEEK(FileSystem.LEN); END; FileSystem.WRITE(FText); FileSystem.CLOSE;
5 -
Thank you all for your answers. I fiddled around with and I found out a stupid mistake on my behalf. I set the WRITEMODE parameter to FALSE, which in turn opened the file in read-only mode.
This now works:
IF ISCLEAR(WinFileSystem) THEN
CREATE(WinFileSystem);
IF NOT WinFileSystem.FolderExists('D:\NAVADSYNC_LOG') THEN
WinFileSystem.CreateFolder('D:\NAVADSYNC_LOG');
FileSystem.TEXTMODE(TRUE);
IF NOT FILE.EXISTS(FileName) THEN
FileSystem.CREATE(FileName);
FileSystem.WRITEMODE(TRUE);
FileSystem.OPEN(FileName);
FileSystem.SEEK(FileSystem.LEN);
FileSystem.WRITE(FText);
FileSystem.CLOSE;5
Answers
-
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 - ArcherPoint1 -
check, if the nav service account has write access to folder
\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/0 -
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.0 -
I'm not sure if this is the problem or not, but you shouldn't open the file if you are creating it. Try changing your code as follows...
FileSystem.WRITEMODE(TRUE); FileSystem.TEXTMODE(TRUE); IF NOT FILE.EXISTS(FileName) THEN FileSystem.CREATE(FileName) ELSE BEGIN FileSystem.OPEN(FileName); FileSystem.SEEK(FileSystem.LEN); END; FileSystem.WRITE(FText); FileSystem.CLOSE;
5 -
Thank you all for your answers. I fiddled around with and I found out a stupid mistake on my behalf. I set the WRITEMODE parameter to FALSE, which in turn opened the file in read-only mode.
This now works:
IF ISCLEAR(WinFileSystem) THEN
CREATE(WinFileSystem);
IF NOT WinFileSystem.FolderExists('D:\NAVADSYNC_LOG') THEN
WinFileSystem.CreateFolder('D:\NAVADSYNC_LOG');
FileSystem.TEXTMODE(TRUE);
IF NOT FILE.EXISTS(FileName) THEN
FileSystem.CREATE(FileName);
FileSystem.WRITEMODE(TRUE);
FileSystem.OPEN(FileName);
FileSystem.SEEK(FileSystem.LEN);
FileSystem.WRITE(FText);
FileSystem.CLOSE;5
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions