Options

File Attribute

Mc3762Mc3762 Member Posts: 26
edited 2002-02-26 in Navision Financials
Path := 'C:\test.txt';
Create(myFile);
Create(myFilesystemobject);
myFile := muFileSystemObject.getfile(Path);
myFile.attributes := myFile.attributes + 1;

This does not work?
I Need to set a file to be read only. myFileSystemObject is a microsoft scripting runtime object, named Filesystemobject.
MyFile is also a Microsft scripting runtime object, named File.

Please help

Thanks in advance.

Morti
<img border="0" title="" alt="" src="images/smiles/icon_confused.gif" />

Comments

  • Options
    wonmowonmo Member Posts: 139
    You can do a SHELL command:

    attrib FILENAME +r

    This will set the file to read only from dos. Because you're using dos, though, there are limitations with file name length.

    Hope this helps.
  • Options
    john.fojohn.fo Member Posts: 16
    Have you tried myfile.WRITEMODE(FALSE); ?
  • Options
    Mc3762Mc3762 Member Posts: 26
    That's only in Navision, I need the file to be read only in the OS. So that you can't open the file from another program.
  • Options
    Mc3762Mc3762 Member Posts: 26
    Thanks :-) But it would be nice with a solution that didn't use the shell feature, because it is not sure that it works in every OS.
  • Options
    wonmowonmo Member Posts: 139
    Another way to set the file attribute would be to use an automation server to set the file attribute when saving. This also has some limitations but would bypass the use of the SHELL statement.
  • Options
    John_TegelaarJohn_Tegelaar Member Posts: 159
    The FileSystemObject is a powerful tool indeed, and allows many manipulations. You have to use it in a proper way, though, to get the result you want.

    This gives a better result:

    Vars:
    <BLOCKQUOTE><font size="1" face="Verdana, Arial">code:</font><HR><pre>
    Name DataType Subtype Length
    myFileSystemObject Automation 'Microsoft Scripting Runtime'.FileSystemObject
    myFile Automation 'Microsoft Scripting Runtime'.File
    myPath Text 200
    myAttributes Integer
    </pre><HR></BLOCKQUOTE>

    Code:
    <BLOCKQUOTE><font size="1" face="Verdana, Arial">code:</font><HR><pre>CREATE(myFileSystemObject);

    myPath := 'c:\test.txt';
    myFileSystemObject.GetFile(myPath);
    myFile := myFileSystemObject.GetFile(myPath);

    myAttributes := myFile.Attributes;

    IF (myAttributes MOD 2 <> 0) THEN
    MESSAGE('File %1 is now ReadOnly',myPath)
    ELSE
    MESSAGE('File %1 is now Read/Write',myPath);

    IF (myAttributes MOD 2 <> 0) THEN
    myAttributes := myAttributes - 1 //Normal
    ELSE
    myAttributes := myAttributes + 1; //ReadOnly

    myFile.Attributes := FORMAT(myAttributes);

    IF (myAttributes MOD 2 <> 0) THEN
    MESSAGE('File %1 is now ReadOnly',myPath)
    ELSE
    MESSAGE('File %1 is now Read/Write',myPath); </pre><HR></BLOCKQUOTE>

    John
  • Options
    Mc3762Mc3762 Member Posts: 26
    Thanks John

    But I get some errors.

    The first 4 lines is ok, but as soon as I use the myFile.Attributes, it says that it is not a legal data type.

    Why is this?
  • Options
    John_TegelaarJohn_Tegelaar Member Posts: 159
    Guess you didn't declare the variable correctly. Are you sure it's set to:

    Name DataType Subtype
    myFile Automation 'Microsoft Scripting Runtime'.File

    Just to be sure, I re-tested the code and here it's running fine.

    John
  • Options
    Mc3762Mc3762 Member Posts: 26
    I'm sure that they are set right.

    It could be because of our financial is ver. 2.01B

    Where we cant make the automation variables. But we do have a word codeunit with these variables in, so what I do is I copy this unit, and changes the variable names, and objects.
Sign In or Register to comment.