Skip to main content
WolfShade
Legend
April 18, 2014
Answered

CF9/10: Change file attributes

  • April 18, 2014
  • 1 reply
  • 461 views

Hello, everyone,

Is there a way to change the attributes of a .txt file that already exists?

For example:  I use CFFILE to write a .txt file that is blank, and supply the "readOnly" attribute.  Later, I want to dynamically add data to the file.  How do I set the .txt file to NOT readOnly so I can add data?  And, conversely, how do I set the attribute back to readOnly?

V/r,

^_^

    This topic has been closed for replies.
    Correct answer BKBK

    An example on Windows, using 3 steps:

    <cfset path_to_directory="C:\Users\BKBK\Desktop">

    <!--- Create readOnly blank text file --->

    <cffile action="write" attributes="readonly" file="#path_to_directory#\testFile.txt" output="">

    <!--- Disable readOnly attribute --->

    <cffile action="rename" attributes="normal" source="#path_to_directory#\testFile.txt"  destination="#path_to_directory#\testFile.txt">

    <!--- Write some text to file, switching readOnly attribute back on --->

    <cffile action="write" attributes="readonly" file="#path_to_directory#\testFile.txt" output="Some text">

    Done!

    1 reply

    BKBK
    Community Expert
    BKBKCommunity ExpertCorrect answer
    Community Expert
    April 19, 2014

    An example on Windows, using 3 steps:

    <cfset path_to_directory="C:\Users\BKBK\Desktop">

    <!--- Create readOnly blank text file --->

    <cffile action="write" attributes="readonly" file="#path_to_directory#\testFile.txt" output="">

    <!--- Disable readOnly attribute --->

    <cffile action="rename" attributes="normal" source="#path_to_directory#\testFile.txt"  destination="#path_to_directory#\testFile.txt">

    <!--- Write some text to file, switching readOnly attribute back on --->

    <cffile action="write" attributes="readonly" file="#path_to_directory#\testFile.txt" output="Some text">

    Done!

    WolfShade
    WolfShadeAuthor
    Legend
    April 21, 2014

    !!!

    Thanks, BKBK!  I tried using append with attributes, that didn't work.. hadn't thought of rename.  Brilliant!

    V/r,

    ^_^