CFLib.org – Common Function Library Project

SetFileAttribute(sFilePath, sAttribute, bOnOff)

Last updated September 27, 2001

author

Nate Weiss

Version: 1 | Requires: CF5 | Library: FileSysLib

Description:
Function to set or clear a Windows file attribute (ReadOnly, Hidden, etc) for the specified file. This is essentially just a wrapper around the Scripting.FileSystemObject's File.Attribute property. It will work only on Windows systems.

Return Values:
Returns a Boolean value indicating whether the attribute was set.

Example:

<CFSET x = SetFileAttribute("c:\winnt\notepad.exe", "Archive", "No")>

<CFOUTPUT>
Was the attribute set? #YesNoFormat(x)#
</CFOUTPUT>

Parameters:

Name Description Required
sFilePath Absolute or relative path to the specified file. Yes
sAttribute Attribute you wish to set. Options are: ReadOnly, Hidden, System, Archive. Yes
bOnOff Boolean value indicating whether the attribute should be on (Yes) or off (No). Yes

Full UDF Source:

/**
 * Function to set or clear a Windows file attribute (ReadOnly, Hidden, etc) for the specified file.
 * Uses COM. This is a Windows only funciton. Requires CFOBJECT be enabled in the CF Administrator.
 * 
 * @param sFilePath 	 Absolute or relative path to the specified file. 
 * @param sAttribute 	 Attribute you wish to set.  Options are: ReadOnly, Hidden, System, Archive. 
 * @param bOnOff 	 Boolean value indicating whether the attribute should be on (Yes) or off (No). 
 * @return Returns a Boolean value indicating whether the attribute was set. 
 * @author Nate Weiss (nate@nateweiss.com) 
 * @version 1, September 27, 2001 
 */
function SetFileAttribute(sFilePath, sAttribute, bOnOff) {
   var result = False;
   var fso = 0;
   var f = 0;
   var iListPosition = 0;
   var iFlagPosition = 0;
   
   if ( FileExists(sFilePath) ) {
     fso = CreateObject("COM", "Scripting.FileSystemObject");
     f = fso.GetFile(sFilePath);
     iListPosition = ListFindNoCase("ReadOnly,Hidden,System,Archive", sAttribute);
     
     if (iListPosition GT 0) {
       iFlagPosition = ListGetAt("0,1,2,5", iListPosition);
       f.attributes = BitMaskSet(f.attributes, IIF(bOnOff, 1, 0), iFlagPosition, 1);
       result = True;
     }
   }
  
   return result;
 }
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson