Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Wed Sep 26, 2018 9:15 am
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 11 posts ] 
Author Message
 Post subject: Adding date + time to end of copied file or folder?
PostPosted: Tue Nov 06, 2012 3:04 am 
Offline
User avatar

Joined: Sun Jul 15, 2007 3:12 am
Posts: 119
Location: Denmark, Europe
Hi all,

I'm using the example code under "The CopyFileOrFolder method is as follows" (the second last example) from the FolderItem examples in the LR here.

I'd like to adapt this, to add an underscore and the SQLDate to the file or folder copied via the above example. So the file or folder shows the date it was copied to the new location, I can then alter this to include the time myself later. This makes backups easy in my app, and I don't have to worry about duplicates and so on.

I've tried a lot, but can't quite figure this out, even though I'm sure it's very easy :)

Can you please get me started guys, thank you :)

_________________
REAL Studio 2012 Pro (Release 1.2) on a 17" MacBook Pro running Mac OS X Mountain Lion.
ATTENTION Danish RS Devs - make sure you join our Facebook group at http://www.facebook.com/groups/danske.realstudio.udviklere/


Top
 Profile  
Reply with quote  
 Post subject: Re: Adding date + time to end of copied file or folder?
PostPosted: Tue Nov 06, 2012 3:20 am 
Offline
User avatar

Joined: Mon Apr 02, 2007 2:08 am
Posts: 1225
Location: San Francisco, CA, USA
You'll need to alter just one line:
newFolder = destination.Child(source.Name)


source.Name is a String representing the file or directory name of the copying source. newFolder is a FolderItem which represents the new destination. So, to add the SQLdate (another string) to the destination's name, simply concatenate, e.g.:
Dim d As New Date
newFolder = destination.Child(source.Name + "_" + d.SQLDate)


The function in the LR is an example of a recursive function, which can make it a little harder to follow the logic.

_________________
Boredom Software


Top
 Profile  
Reply with quote  
 Post subject: Re: Adding date + time to end of copied file or folder?
PostPosted: Tue Nov 06, 2012 3:41 am 
Offline
User avatar

Joined: Sun Jul 15, 2007 3:12 am
Posts: 119
Location: Denmark, Europe
charonn0 wrote:
You'll need to alter just one line:


Thanks my friend, that works great when it's a folder that is copied, but how do I then make the last line add the SQLDate to files being copied?

I began looking at files thinking that was the easiest to get working, but having tried many ways I can't seem to figure that one out :)

Thank you for your fast help, hope you can kick me over the finish line :)

_________________
REAL Studio 2012 Pro (Release 1.2) on a 17" MacBook Pro running Mac OS X Mountain Lion.
ATTENTION Danish RS Devs - make sure you join our Facebook group at http://www.facebook.com/groups/danske.realstudio.udviklere/


Top
 Profile  
Reply with quote  
 Post subject: Re: Adding date + time to end of copied file or folder?
PostPosted: Tue Nov 06, 2012 4:37 am 
Offline
User avatar

Joined: Mon Jan 02, 2012 1:51 am
Posts: 382
Location: India
Hi,

Try this...

Dim i As Integer
Dim newFolder As FolderItem
Dim newfile As FolderItem
Dim d As new Date

If source.Directory Then //it's a folder
newFolder = destination.Child(source.Name + "_" + d.SQLDate)
newFolder.CreateAsFolder
For i = 1 to source.Count //go through each item
If source.Item(i).Directory Then
//it's a folder
CopyFileOrFolder(source.Item(i), newFolder) //recursively call this routine passing it the folder
Else
source.Item(i).CopyFileTo(newFolder) //it's a file so copy it
newfile = newFolder.Child(source.Item(i).Name)
newfile.Name = newfile.Name.NthField(".", 1) + "_" + d.SQLDate + newfile.Name.Mid(newfile.Name.InStr("."))
End If
Next
Else //it's not a folder
source.CopyFileTo(destination)
newfile = destination.Child(source.Name)
newfile.Name = newfile.Name.NthField(".", 1) + "_" + d.SQLDate + newfile.Name.Mid(newfile.Name.InStr("."))
End If

_________________
Amitava Karan
Real Studio 2012 Release 1
Windows 7 (32 bit)
Mac OS X Version 10.7


Top
 Profile  
Reply with quote  
 Post subject: Re: Adding date + time to end of copied file or folder?
PostPosted: Tue Nov 06, 2012 5:44 am 
Offline
User avatar

Joined: Sun Jul 15, 2007 3:12 am
Posts: 119
Location: Denmark, Europe
amitavak wrote:
Hi,

Try this...


Amitava, You're the BEST! :-)

Thank you so much, that worked perfectly and I would never have figured that out myself!

I ended up only implementing your solution in the last ELSE statement, so only files and the top-most folders would get dates added, and not the files contained in folders. And it all works just like I wanted it to!

Very happy newbie here, thank you charonn0 and Amitava!

Cheers,
Thomas

_________________
REAL Studio 2012 Pro (Release 1.2) on a 17" MacBook Pro running Mac OS X Mountain Lion.
ATTENTION Danish RS Devs - make sure you join our Facebook group at http://www.facebook.com/groups/danske.realstudio.udviklere/


Top
 Profile  
Reply with quote  
 Post subject: Re: Adding date + time to end of copied file or folder?
PostPosted: Wed Nov 07, 2012 3:59 am 
Offline
User avatar

Joined: Sun Jul 15, 2007 3:12 am
Posts: 119
Location: Denmark, Europe
Hi again :)

A follow-up question regarding formatting of hours, minutes and seconds, and optimization of the code.

Because I'm on a Mac I can't use colons in the time-stamps, because a colon is the only char not allowed in filnames under Mac OS X. That's no problem with the SQLDate, as it formats dates like so: 2012-11-07

But when it comes to hours, minutes and seconds created by SQLDateTime, LongTime or ShortTime, they all use the locale and formatting dictated by the users preferences in the Date & Time System Preferences under Mac OS X. SQLDateTime for example creates something like this: 2012-11-07 13:39:16 - a filename not allowed under Mac OS X.

I have solved this by creating the timestamp myself, using underscores instead, like so:

newFile.Name = newFile.Name.NthField(".", 1) + "__" + d.SQLDate + "__" + Str(d.Hour) + "_" + Str(d.Minute) + "_" + Str(d.Second) + newFile.Name.Mid(newFile.Name.InStr("."))


Which gives me file and folder names like this: TestFolder1__2012-11-06__12_38_52

This is fine, I can format the timestamp as I wish this way. But, if the time has one-digit numbers - in the morning for example - I would get a filename like this: TestFolder1__2012-11-06__9_3_6 - which is today at 09:03 and six seconds (AM). Not pretty or understandable at all :)

I have created the test-code below, which places a zero in front of one-digit seconds. It works, but it's messy and will be even more horrible when doing the same for minutes and hours.

Is there a way to either make SQLDateTime use underscores instead of colons (other than having the user change his computers locale and formatting :) or is there a cleaner way to detect one-digit Date.Hour, Date.Minute and Date.Second output, and place a zero in front? I can't see how you can alter the output of SQLDateTime - and neither should you (probably?) - but can the other thing be done cleaner than the example below (which only detects one-digit seconds right now):

If Len(Str(d.Second)) = 1 Then

source.CopyFileTo(destination)
newFile = destination.Child(source.Name)
newFile.Name = newFile.Name.NthField(".", 1) + "__" + d.SQLDate + "__" + Str(d.Hour) + "_" + Str(d.Minute) + "_" + "0" + Str(d.Second) + newFile.Name.Mid(newFile.Name.InStr("."))

Else

source.CopyFileTo(destination)
newFile = destination.Child(source.Name)
newFile.Name = newFile.Name.NthField(".", 1) + "__" + d.SQLDate + "__" + Str(d.Hour) + "_" + Str(d.Minute) + "_" + Str(d.Second) + newFile.Name.Mid(newFile.Name.InStr("."))

End If


Thank you all in advance, I'm really learning a lot from this!

_________________
REAL Studio 2012 Pro (Release 1.2) on a 17" MacBook Pro running Mac OS X Mountain Lion.
ATTENTION Danish RS Devs - make sure you join our Facebook group at http://www.facebook.com/groups/danske.realstudio.udviklere/


Top
 Profile  
Reply with quote  
 Post subject: Re: Adding date + time to end of copied file or folder?
PostPosted: Wed Nov 07, 2012 6:05 am 
Offline
User avatar

Joined: Mon Jan 02, 2012 1:51 am
Posts: 382
Location: India
ok...one things can be done.... create a method which will return a custom time format like...

Function GetCustomTimeFormat(Extends dateValue As Date) As String
If dateValue <> Nil Then

Return Format(dateValue.Hour, "00") + "_" +_
Format(dateValue.Minute, "00") + "_" +_
Format(dateValue.Second, "00")

End If
End Function


Then use it in the "CopyFileOrFolder" method.....

newFile.Name = newFile.Name.NthField(".", 1) + "__" + d.SQLDate + "__" + d.GetCustomTimeFormat + newFile.Name.Mid(newFile.Name.InStr("."))


put that "GetCustomTimeFormat" in a global place like "Module".

_________________
Amitava Karan
Real Studio 2012 Release 1
Windows 7 (32 bit)
Mac OS X Version 10.7


Top
 Profile  
Reply with quote  
 Post subject: Re: Adding date + time to end of copied file or folder?
PostPosted: Thu Nov 08, 2012 5:45 am 
Offline
User avatar

Joined: Sun Jul 15, 2007 3:12 am
Posts: 119
Location: Denmark, Europe
amitavak wrote:
ok...one things can be done.... create a method which will return a custom time format like...


Amitava, you continue to ROCK :)

As you can see below, everything works as it should! Thank you very much for your kind help, and a big thank you to Charon also - I really learned a lot from this, much more than I have various books and articles, you just learn better when it's hands on:)

Image

Cheers,
Thomas

_________________
REAL Studio 2012 Pro (Release 1.2) on a 17" MacBook Pro running Mac OS X Mountain Lion.
ATTENTION Danish RS Devs - make sure you join our Facebook group at http://www.facebook.com/groups/danske.realstudio.udviklere/


Top
 Profile  
Reply with quote  
 Post subject: Re: Adding date + time to end of copied file or folder?
PostPosted: Tue Nov 13, 2012 4:47 am 
Offline
User avatar

Joined: Sun Jul 15, 2007 3:12 am
Posts: 119
Location: Denmark, Europe
Hi all,

I need a bit more help, this time with sub-folders and not setting date + time on them.

As you can see from the code below, I have this great method you guys helped me create, that takes any files and/or folders dropped on my apps main window, and adds date and time to their file-names. That way my user can see when he used the app, to backup certain files and folders. You also helped me create a method, that made sure times with one-digit (like 08:02:02 AM) got zeros added so we didn't end up with filenames like "test-file__2012-11-13__8_2_2" (see the code for that earlier in the thread).

The issue is, that this works too well :)

As you can see in this screenshot, sub-folders within the folder User dropped on the app, also gets date + time added. This I would like to avoid, and thought I could easily figure this out. I tried various things around the ...

If source.item(i).Directory Then


...block, but I can't seem to nail it completely. I end up with either duplicate files and folders, or NilObject errors. Now I bet this is really easy to get working, but I must admit I need help ... so my friends, is there a guru among you who can help me please? :)

Here's the code for my backup method that does the copying:

Sub BackupFileOrFolder(source As FolderItem, destination As FolderItem)

Dim i as Integer
Dim newFolder As FolderItem
Dim newFile As FolderItem
Dim d As New Date


// If it's a folder User drags to our window...
If source.directory Then

// copy the top-folder and add date+time via SQLDate and the custom GetCustomTimeFormat method
newFolder = destination.Child(source.Name + "__" + d.SQLDate + "__" + d.GetCustomTimeFormat)
newFolder.createAsFolder

For i = 1 to source.Count // Run through the items in the folder...

If source.item(i).Directory Then // if it's another folder...
BackupFileOrFolder(source.item(i), newFolder) // call this routine recursively and pass it all the folders we find...

Else
source.item(i).CopyFileTo(newFolder) // copy the files inside the folders
End If

Next

Else
source.CopyFileTo(destination) // And finally, copy any "loose files" we find inside the top-level folder
newFile = destination.Child(source.Name) // and add date + time to them
newFile.Name = newFile.Name.NthField(".", 1) + "__" + d.SQLDate + "__" + d.GetCustomTimeFormat + newFile.Name.Mid(newFile.Name.InStr("."))

End If



End Sub


Thank you all in advance!

Cheers,
Thomas

_________________
REAL Studio 2012 Pro (Release 1.2) on a 17" MacBook Pro running Mac OS X Mountain Lion.
ATTENTION Danish RS Devs - make sure you join our Facebook group at http://www.facebook.com/groups/danske.realstudio.udviklere/


Top
 Profile  
Reply with quote  
 Post subject: Re: Adding date + time to end of copied file or folder?
PostPosted: Tue Nov 13, 2012 9:04 am 
Offline
User avatar

Joined: Mon Jan 02, 2012 1:51 am
Posts: 382
Location: India
A first think that strike me, add a third parameter. Depending on that parameter value, we can decide if we add the time stamp with the folder name or not.

But there must be a better way. I can think or found any better solution, definitely post here.

Edit: This is my solution using a third parameter.

Sub BackupFileOrFolder(source As FolderItem, destination As FolderItem, AddTimeStamp As Boolean = True)

Dim i as Integer
Dim newFolder As FolderItem
Dim newFile As FolderItem
Dim d As New Date


// If it's a folder User drags to our window...
If source.directory Then

// copy the top-folder and add date+time via SQLDate and the custom GetCustomTimeFormat method
If AddTimeStamp then
newFolder = destination.Child(source.Name + "__" + d.SQLDate + "__" + d.GetCustomTimeFormat)
Else
newFolder = destination.Child(source.Name)
End If

newFolder.createAsFolder

For i = 1 to source.Count // Run through the items in the folder...

If source.item(i).Directory Then // if it's another folder...
BackupFileOrFolder(source.item(i), newFolder, False) // call this routine recursively and pass it all the folders we find...

Else
source.item(i).CopyFileTo(newFolder) // copy the files inside the folders
End If

Next

Else
source.CopyFileTo(destination) // And finally, copy any "loose files" we find inside the top-level folder
newFile = destination.Child(source.Name) // and add date + time to them
newFile.Name = newFile.Name.NthField(".", 1) + "__" + d.SQLDate + "__" + d.GetCustomTimeFormat + newFile.Name.Mid(newFile.Name.InStr("."))

End If



End Sub

_________________
Amitava Karan
Real Studio 2012 Release 1
Windows 7 (32 bit)
Mac OS X Version 10.7


Top
 Profile  
Reply with quote  
 Post subject: Re: Adding date + time to end of copied file or folder?
PostPosted: Wed Nov 14, 2012 7:18 am 
Offline
User avatar

Joined: Sun Jul 15, 2007 3:12 am
Posts: 119
Location: Denmark, Europe
My friend, this works beautifully - thank you so much! :)

And even though you may not think it's the most elegant solution, I think it's awesome. Plus it taught me something I did not know: That you can assign a value directly in the method's parameters, without having to create a property just to store something like that. I can use that in a lot of places, where creating a property for something you just use once, is totally overkill.

So I'm very happy, thank you :)

Cheers,
Thomas

_________________
REAL Studio 2012 Pro (Release 1.2) on a 17" MacBook Pro running Mac OS X Mountain Lion.
ATTENTION Danish RS Devs - make sure you join our Facebook group at http://www.facebook.com/groups/danske.realstudio.udviklere/


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group