Login
Templates, Tools and Utilities
|
||
Icetips Utilities: Core Class TutorialCore Class (ITCoreClass)The Core Class contains 53 methods as of version 1.2.2433 that was released on June 29, 2015. The Core class is what all the other classes are based on so it has some very basic methods that are used all over the place in the other classes. Note that not all of the methods are documented as of that release. Below are short tutorials on how to accomplish various things with this class. The tutorials are short and demonstrate simple use of the methods. For more detailed documentation of the string class please see the online documentation. This tutorial is also included in the documentation. Getting and Setting file informationGet temporary filenames and temporary folderGetting and setting file informationWhen dealing with files it is often desirable to be able to know, for example, if a file has the read-only attribute set. In some cases it can also be useful to be able to change the attributes of files, for example to change a hidden file to be not hidden or to mark a file to be read-only. The Core class has several methods to get information about files and folders. To start with, let's take a look at the GetFileAttrib and SetFileAttrib. Example:ITC ITCoreClassClass RO Byte HI Byte SY Byte Code ITC.GetFileAttrib('C:\Temp\Filename.txt',RO,HI,SY) Message('Read-Only = ' & RO &| '|Hidden = ' & HI &| '|System File = ' & SY) This will give us the attributes of this file. The GetFileAttrib and SetFileAttrib only retrieve and set the Read-only, Hidden and System attributes. Example: ITC ITCoreClassClass RO Byte HI Byte SY Byte Code RO = True HI = False SY = False ITC.SetFileAttrib('C:\Temp\Filename.txt',RO,HI,SY) !! Set the attributes !! Set it to false to make sure we are !! getting the information from the file. RO = False ITC.GetFileAttrib('C:\Temp\Filename.txt',RO,HI,SY) !! Get the attributes Message('Read-Only = ' & RO &| '|Hidden = ' & HI &| '|System File = ' & SY) Now you should see that the Read-only has been set on the file. Take a look in Windows Explorer to make sure. There are two other functions that give us information about files. IsFolder and IsFileInUse will let us know if a filename is a folder or a file and IsFileInUse will tell us if a specified file can be written to or not. When dealing with foldernames it is sometimes very important to be able to distinguish if a filename is a foldername or not. With valid foldernames such as MyFile.TXT it could lead to all sorts of problems if we passed this into a method or function that attempted to write to the file. In this case the filename is perfectly valid for both a folder and a file. This is where IsFolder comes to the rescue. It will ONLY return True if the filename passed to it is a name of a folder by getting the attribute of the file with the GetFileAttributes api call. Example: ITC ITCoreClass FN CString(2049) Code FN = 'C:\temp\myfile.txt' If Not ITC.IsFolder(Fn) Remove(Fn) End In this case the file would only be removed if it is a regular file, not if it is a folder. This can be very useful when you are writing generic methods that can take a parameter with a filename or a foldername and need to work differently depening on if the filename is actually a name of a folder or a file.
Get temporary filenames and temporary folderSometimes you need to place some data temporarily into a file and then remove the file. Rather than create those files in the application folder, which can cause virtualization to kick in, or in your data folder, it can be very convenient to write those files to the temporary folder. The GetTempFilename and GetTempFolder methods are designed to take care of that. GetTempFilename creates a temporary filename AND creates the temporary file in either the specified folder or in the temporary folder - same as returned by GetTempFolder. Example: ITC ITCoreClass Fn CString(2049) Pref CString(4) Code Fn = ITC.GetTempFileName() Message('Temp filename: ' & Fn) This will return a filename in the temp folder and at this point this file already exists. In this case the function will return a unique filename that is usually build up with hexadecimal characters and could look something like C:\WINDOWS\TEMP\35.tmp Note that the GetTempFileName() always returns the ShortPath() of the path. Example: ITC ITCoreClass Fn CString(2049) Pref CString(4) Code Pref = 'ITCTEST' Fn = ITC.GetTempFilename(Path(),Pref) Message('Temp filename: ' & Fn) In this case the function will return (and create) a file called something like ITC2F.tmp in the currently active folder. The Pref variable is an optional prefix that you can specify but note that only the first 3 characters of the prefix are used. This is a limitation of the API call, not of the GetTempFilename method! This would return something like C:\Clarion\Apps\C63\Products\UTILIT~1\Demos\CORECL~1\ITC36.tmp
Example: ITC ITCoreClass Fn CString(2049) Code Fn = ITC.GetTempFilename(Path(),Pref) Remove(Fn) Fn = ITC.GetFilepart(Fn,FNS_FileName) Fn = '"' & LongPath() & '\ITCTEST' & Fn & '"' Message('Temp filename: ' & Fn) Of course there is nothing preventing you from being creative and creating a longer prefix to your temporary filename! The above code creates the temp filename, then removes the file that it created. It then retrieves the filename only (filename + extension) from the full pathname returned by GetTempFilename and adds "ITCTEST" in front of the filename as it combines it with the Path(). This would return something like "C:\Clarion\Apps\C63\Products\UtilityClass\ Demos\CoreClass\ITCTEST34.tmp" Notice that in this case we use LongPath() instead of Path() to get the long path to the file. Also notice that we remove the file created by the GetTempFilename right after we call it before we start manipulating the filename.
How to buyThe Core Class is part of the Icetips Utilities, which are included Icetips' premium high-value Gold Subscription for $299. The Icetips Utilities may also be purchased separately for just $99. (Multiple copy pricing is available.) |
|