fileattrib
This lesson focuses on files.
Example 1
Set or remove the attributes of the files a read-only file. This program accepts command line parameters. The files can be stored in templates, using the following operators: '*' and '?'. The '*' operator defines any sequence of characters, '?' represents a single character.Thus,
c:\temp\*.* - all files in the folder c:\temp
c:\temp\*.exe - all files with the extension exe in the folder c:\temp
c:\temp\a*.* - all files beginning with 'a' in the folder c:\temp
c:\temp\ab?*.* - all files beginning with 'ab' and one other character in the folder c:\temp
So, we start with command line. It is fairly easy, because of two functions: argc returns the number of arguments, argv returns the required parameter. The first parameter must be the word on or off for setting or removing the attribute, the second parameter must be the template for file processing. So, we can do it like this:
if argc() > 1
{
if argv( temp, 1 ) %== "on" : mode = 1
elif argv( temp, 1 ) %== "off" : mode = 2
argv( path, 2 )
}
The '%==' operator produces a line-by-line comparison ignoring the characters' case. Here, you can write ON as well as Off.
If the parameters have not been indicated by the time the program starts or you typed ones that are not valid, give a chance to input necessary information on the console.
if !mode
{
mode = conrequest( "Choose an action (press a number key):
1. Turn on readonly attribute
2. Turn off readonly attribute
3. Exit\n", "1|2|3" ) + 1
if mode == 3 : return
congetstr( "Specify a filename or a wildcard: ", path )
}
Here the user has to type: 1 to set the attribute, 2 to remove it and 3 to exit the program. The conrequest function waits for the keystroke, then returns the number of the selected variant from 0.
For example,
conrequest("Press #'Y#' or #'N#'", "Yy|Nn" )
OK. Now we proceed to the task solution. The ffind structure searches for the specified filename. Let's describe and initialize the variable fd of type ffind.
fd.init( path, $FIND_FILE | $FIND_RECURSE )
$FIND_FILE points to the search of specified filenames.$FIND_RECURSE indicates the search of specified filenames in all subdirectories.
For instance,
fd.init( "c:\\temp.txt", $FIND_FILE | $FIND_RECURSE )
with the specified flag $FIND_RECURSE will search for the filename: temp.txt on the entire C: drive.
The foreach operator is used for file searching:
foreach cur,fd
{
attrib = getfileattrib( cur.fullname )
if mode == 1 : attrib |= $FILE_ATTRIBUTE_READONLY
else : attrib &= ~$FILE_ATTRIBUTE_READONLY
setfileattrib( cur.fullname, attrib )
print( "\(cur.fullname)\n" )
}
finfo is a type that stores information about files. More information about this can be found in Help.
cur is a variable of the specified type which contains the stored information about any file that has been found.
Now, I would like to say a few words about loop content. We obtain the current file attributes
attrib = getfileattrib( cur.fullname )
According to conditions we set or remove the attribute of the file a read-only file. Other attributes are saved.
if mode == 1 : attrib |= $FILE_ATTRIBUTE_READONLY
else : attrib &= ~$FILE_ATTRIBUTE_READONLY
We write the modified attributes of the file.
setfileattrib( cur.fullname, attrib )