Reading File Properties in PowerBuilder
If you look at MyComputer or Windows Explorer, you will notice that if you're in a folder loaded with mp3's or other media files, and you are in Details mode, that you can right-click on the header bar and you will see many more options for columns to display. Great news... Any of this information can be retrieved in PowerBuilder quite simply!
[img http://i49.photobucket.com/albums/f263/thekl0wn/tech/file_prop_1.jpg]
Before I dive into the nuts & bolts of how to retrieve everything, I will start by setting up a simple Custom UserObject called nvo_file. I set it to Auto-Instantiate, and then add an instance variable to hold the path of this object's file, and then a boolean to tell if the object has been set, and yet another private variable to hold a shell object.
[color green]/*nvo_file's instance variables*/
PUBLIC PRIVATEWRITE String is_path, is_folder, is_filename
PUBLIC PRIVATEWRITE Boolean ib_set
PRIVATE OLEObject obj_shell[/color]
Next, we need to initiate the shell object. We will handle this in the Constructor event of nvo_file...
[color green]/*nvo_file constructor event*/
obj_shell = CREATE OLEObject
obj_shell.ConnectToNewObject( 'shell.application' )[/color]
And likewise in the destructor event, I like to manually clear up my memory...
[color green]/*nvo_file destructor event*/
DESTROY obj_shell[/color]
Next, for ease of use, I create a simple UserFunction which returns a Boolean and accepts a String as the sole parm, and call it "is_blank". This function is very useful for simply testing a string to see if it's blank or not... Basically, it's a lazy shortcut to help ward off NULL strings.
[color green]/*is_blank -- returns boolean -- argument: as_test*/
IF IsNull( as_test ) THEN RETURN TRUE
IF as_test = '' THEN RETURN TRUE
RETURN FALSE[/color]
Now, back to nvo_file! I will break each item down into a separate function, rather than using properties. The first function of the UserObject should be one to set the path of the file. I chose to use a function to set the path this way over simply setting the is_path variable so that I only need to call the slow FileExists( is_path ) function once per file. This may not seem like too much of an issue, but many times I loop through thousands of file paths, and the FileExists( ) function is a hog! Also, the way this function is setup, you can tell if the path you passed works or not by what's returned from the set function.
[color green]/*nvo_file.of_set_file( String as_path ) returns Boolean*/
IF FileExists( as_path ) THEN
is_path = as_path
is_folder = Left( is_path, LastPos( is_path, '\' ) )
is_filename = Mid( is_path, LastPos( is_path, '\' ) + 1 )
ib_set = TRUE
ELSE
is_path = ''
is_folder = ''
is_filename = ''
ib_set = FALSE
END IF
RETURN ib_set[/color]
Now that we have everything set up to set our file, let's do a function to get the creation date of the file...
[color green]/*nvo_file.of_get_creation_date( ) returns DateTime*/
String ls_datetime, ls_time
DateTime ldt_file
Date ld_date
OLEObject obj_folder, obj_item
//first off, make sure the path is set to a valid file...
IF ib_set THEN
obj_folder = obj_shell.NameSpace( is_folder ) //folder
obj_item = obj_folder.ParseName( is_filename ) //file
ls_datetime = obj_folder.GetDetailsOf( obj_item, 4 )
//the date can be ripped directly out of the string
ld_date = Date( ls_datetime )
//time cannot be ripped directly out of the string
//a blank space is the separator from the date & time
ls_time = Mid( ls_datetime, Pos( ls_datetime, ' ' ) + 1 )
//combine the two...
ldt_file = DateTime( ld_date, Time( ls_time ) )
END IF
//clear up memory
DESTROY obj_folder
DESTROY obj_item
RETURN ldt_file[/color]
And now you have a functional object that can retrieve the creation date of a file! There are many more properties that you can grab out of a file...