PowerBuilder Forum

Members Login
Username 
 
Password 
    Remember Me  
Post Info TOPIC: Reading File Information (getDetailsOf)


Veteran Member

Status: Offline
Posts: 40
Date:
Reading File Information (getDetailsOf)


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!

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.

/*nvo_file's instance variables*/
PUBLIC PRIVATEWRITE String is_path, is_folder, is_filename
PUBLIC PRIVATEWRITE Boolean ib_set
PRIVATE OLEObject obj_shell


Next, we need to initiate the shell object.  We will handle this in the Constructor event of nvo_file...

/*nvo_file constructor event*/
obj_shell = CREATE OLEObject
obj_shell.ConnectToNewObject( 'shell.application' )


And likewise in the destructor event, I like to manually clear up my memory...

/*nvo_file destructor event*/
DESTROY obj_shell


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.

/*is_blank -- returns boolean -- argument: as_test*/
IF IsNull( as_test ) THEN RETURN TRUE
IF as_test = '' THEN RETURN TRUE

RETURN FALSE


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.

/*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


Now that we have everything set up to set our file, let's do a function to get the creation date of the file...

/*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


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...


__________________


Newbie

Status: Offline
Posts: 1
Date:

Hi. I love this function. I modified it to request various information but I was wondering if you had a similar example not of how to get information, but how to set some of these properties such as artist or title? At my company we have just begun dealing with various media files (mostly mp4 and jpg) and we would like a way to tag the files with member idenfitication numbers in one of the lesser used properties and I figured if you can get any property from a file there has to be a similarly easy way to set any of the properties.

__________________


Veteran Member

Status: Offline
Posts: 40
Date:

Setting is a whole 'nother beast, which I haven't programmatically PB-conquered yet! However, you can embed a WindowsMediaPlayer OLE object and set the data that way. It's an easy work around for now, but a resource hog.

__________________


Newbie

Status: Offline
Posts: 4
Date:

Something went wrong with my pdf reader. I wanna convert pdf into jpg first. Any suggestion will be appreciated. Thanks in advance. How about the abovementioned one?



-- Edited by evanpan on Friday 11th of March 2016 04:17:57 AM

__________________


Veteran Member

Status: Offline
Posts: 40
Date:

Super-late response here, but the OLE Objects will not do file conversions...  It will only change the file extension.



__________________
Page 1 of 1  sorted by
 
Quick Reply

Please log in to post quick replies.

Tweet this page Post to Digg Post to Del.icio.us


Create your own FREE Forum
Report Abuse
Powered by ActiveBoard