Using the file system as a data source in Crystal Reports
A report that shows the files and folders in your file system is easy to create - and potentially very useful.
By Lynn Evans
In this article, I'll show you how to create a report that's based on your computer's file system, that is, on the files and folders on your hard drive or other storage devices. You might wonder why on earth you would want to do that. In fact, this type of report has a surprisingly large number of uses.
Here are some examples of things you can do with a report based on your computer's (or server's) file system:
- Analyzing disk usage, including looking for empty directories, unusually large files, or files that haven't been accessed for a long time.
- Identifying files that have changed but haven't been backed up.
- Recording and analyzing statistics for Word documents (number or words, number of pages, etc).
- Tracking version numbers of executable programs.
- Cataloguing the books or music tracks in an e-book reader or MP3 player.
- Checking a folder's security settings.
- Looking for duplicate files.
Figure 1 shows an example. This is a list of files, grouped by folder, showing the file size, date last accessed, version number and copyright information.
Figure 1: In this report, the data source is not a database, but is the user's file system.
How to do it
Creating this type of report is easier than you might think. In all versions of Crystal Reports (going back at least to CR 7.0), the computer's file system is available as a data source. Its name varies with the version - it might be called Local File System, File System Data, or something similar - but it will always be present.
When you select the file system as the data source, CR will prompt you for the following details:
- The starting directory
- Whether to include lower-level directories
- Whether to include empty directories
- Whether to include all files or only those whose names match a given wildcard pattern (such as *.DOC).
If you wanted a report of an entire hard drive, you would specify the drive's root directory as your starting point, and you would choose to show lower-level directories and all files. But be warned: With the hard drive of a typical modern computer, it could take many minutes for the report to refresh. It's more likely that you would want to focus on a smaller group of folders, such as those containing specific types of documents or files.
Once you've selected the data source and specified the above options, Crystal Reports will treat it in much the same way as a table. Each file or folder corresponds to a record, and each of its 65 or so attributes correspond to its fields. These attributes include such familiar items as the path, name, size, date created, date last modified, and attribute flags (hidden, read-only, etc). There are also many less familiar ones, such as version information, copyright details, author, title and number of pages (but not all attributes are present for all file types).
These attributes are all visible in the Database node of the Field Explorer. You can drag them from the Field Explorer and drop them in the report. You can also sort, group and select on them, or reference them from a formula. If you're not sure what a particular attribute is used for, right-click on it in the Field Explorer and choose Browse Data in the usual way.
And that's all there is to it. Creating reports based on the file system is not at all difficult, and can potentially provide a lot of useful information that could not easily be obtained in any other way. Give it a try.
Please note: The information given on this site has been carefully checked and is believed to be correct, but no legal liability can be accepted for its use. Do not use code, components or techniques unless you are satisfied that they will work correctly with your sites or applications.