I am really embarrased to ask this, but

Hi All.

I know this should be easy, but for some reason it is evading me.

I have a database file that I want to, by selecting a location, I can send a copy to for backup.

This works:

var sourceFolder, destinationFolder as FolderItem
var dlg as OpenFileDialog
var theDate as new Date
var theDateString as string
var theFile, theFileNewName as FolderItem

'sourceFolder = SelectFolder
destinationFolder = SelectFolder
dlg = New OpenFileDialog
theFile = dlg.ShowModal
thedatestring = str(thedate.SQLDate)
//MessageBox (thedatestring) //for testing only
//MessageBox (“The source folder is " + sourceFolder.name+” The destination folder is : “+destinationFolder.name+”")
'MessageBox (“The destination folder is : “+destinationFolder.name+””)
'MessageBox (“The file to copy is “+theFile.name+””)

//copy the file


Return True

But I want to copy this file with a new name to my save location. I don’t seem to be able to in any way append date / time or even just text. I don’t want to overwrite the old file, just in case.

What am I doing wrong?


Create a new Folderitem with the new name and use that for the save. So when you get destinationFolder back, take theFile.name and modify it as you like (append date or whatever) as the name for the new destinationFolder (note that folderitem.CopyTo can take a folderitem pointing to a file, as well as a folder; all we’re doing here is identifying the file within the folder that was selected. Note the caveat on CopyTo, though, that you can’t CopyTo and existing file, so you may also want to check that the new destinationFolderitem you’ve created doesn’t .Exists).

To avoid having a name of a file that already exists (i.e. get an unique name), you can do something like this (better in a separate method that you’ll likely have to adapt it; it’s written from scratch):

Function GetUniqueName(InFolder as FolderItem, BaseName As String, Extension As String) As String
if InFolder=nil or not InFolder.Exists then Return "" //Invalid folder to look into.

Var TrueExtension As String=Extension
if BaseName.Right(1)<>"." and Extension.Left<>"." then TrueExtension="."+TrueExtension //Add a period to separate the name and extension, since none have it.

//First, check for the “default” file name:
Var FileName As String=BaseName+TrueExtension
Var File As FolderItem=InFolder.Child(FileName,False)

if File<>nil and not File.Exists then Return FileName //Plain file name doesn't exist. Use it.

Var Ticks As Integer=System.Ticks //Just to avoid endless loop, should something unexpected happen (e.g. if the folder has already the maximum number of items the filesystem permits or other filesystem oddities).
Var Counter As Integer=2 //To start with, e.g. “MyDocument 2.txt”
  FileName=BaseName+" "+Counter.ToString+TrueExtension
  if File<>nil and not File.Exists then Return FileName //A unique name
  If System.Ticks-Ticks>1200 then Return "" //20 seconds to find a unique name. Something must have been wrong. Just a safety measure; shouldn't happen.
End Function


Hi Arnaud_N

I fear I am showing my stupidity, because I really am having trouble here.

All I want to do, as I said earlier, was copy a file to another location as a backup. Using the code from the Language Reference, as follows:

Var dlg As New SaveFileDialog
Var f As FolderItem
Var newFile as FolderItem

Var txtType As New FileType
txtType.Name = “Text File (*.txt)”
txtType.MacType = “TEXT”
txtType.Extensions = “txt”

Var htmlType As New FileType
htmlType.Name = “HTML File (*.htm, *.html)”
htmlType.MacType = "HTML "
htmlType.Extensions = “htm”

Var csvType As New FileType
csvType.Name = “CSV File (*.csv)”
csvType.MacType = “TEXT”
csvType.Extensions = “csv”

Var xlsType As New FileType
xlsType.Name = “Excel File (*.xls)”
xlsType.MacType = "XLS "
xlsType.Extensions = “xls”

'put in my .sqlite here
var sqliteType as New Filetype
sqliteType.Name = “SQLite File (*.sqlite)”
sqliteType.MacType = “SQLITE”
sqliteType.Extensions = “sqlite”

dlg.InitialFolder = SpecialFolder.Desktop
dlg.promptText = “Prompt Text”
dlg.SuggestedFileName = “Suggested Filename”
dlg.Title = “Title Property”
dlg.Filter = txtType + htmlType + csvType + xlsType + sqliteType
f = dlg.ShowModal
If f <> Nil Then
If Right(f.Name, 3) = “txt” Then
ElseIf Right(f.Name, 3) = “htm” Then
ElseIf Right(f.Name, 3) = “csv” Then
ElseIf Right(f.Name, 3) = “xls” Then
elseif Right(f.Name,6) = “sqlite” then
MessageBox (“5”)
//what goes here.
f.CopyTo newFile
End If
// user canceled
End If

Under message box 5, I need to get the name in the suggested file name box, and I have tried everything I can think of… dlg.suggestedname, f.name, and so many other things I’ve lost track. I’ve looked in the Introduction to Xojo programming PDF I have, but I just can’t get it to work. What the honk allows me to give newFile a new name so it can be copied. I’m really stumped.

I’m sorry…

Oh… just to let you know I haven’t been sitting on my hands. This works… just too dang specific…

dim f, f2, fa as FolderItem
dim thedate as new date
dim thedatestring as string

thedatestring = str(thedate.SQLDate)
MsgBox thedatestring

f = GetFolderItem(“”).child(“workTime_.sqlite”)
fa = GetFolderItem(“”).child(“backup”)
f2 =GetFolderItem(“backup”).child(“workTime_”+thedatestring+“.sqlite”)

if fa.exists = false then
f.CopyFileTo f2
MsgBox “File backed up as : workTime_”+thedatestring+“.sqlite”
f.CopyFileTo f2
MsgBox “File backed up as : workTime_”+thedatestring+“.sqlite”
end if


should do it…

Thanks. I knew I was trying to do stuff the hard way.

I got mine “kinda” working with this:

//ok, lets try this.
var f2 as new FolderItem
dim thedate as new date
dim thedatestring as string

thedatestring = str(thedate.SQLDateTime)


MessageBox ("The file you selected is " + f.name)
MessageBox ("The date / time is : "+thedateString)
MessageBox (“F2 is : “+f2.name+”.sqlite”)
f.CopyTo f2

Thanks to all for their time, patience, and forgiveness of this silliness.


I possibly understood your needs wrong. My code above just returns you a filename that doesn’t yet exist in a given folder. In your case, you ask the user already, so either the file doesn’t yet exist or the user accepted to replace it; in both cases, my code doesn’t make much sense for your needs.

What led me to write that code in the first place is something else you wrote:

And that, my code would do it. But I’m now wondering whether you actually asked for this.

May I suggest you to use a more readable code?


Var FileName as string=f.Name

Select Case FileName.NthField(".",FileName.CountFields(".")) //The extension
case "txt"
case “htm”
case “csv”
case “xls”
case “sqlite”
MessageBox (“5”)
**//what goes here.** Answer: Jeff's code
f.CopyTo newFile
End Select

(again, written from scratch)



Thank you, again one thing I’ve not noticed when it appeared in the language (how can it be, knowing I read all release notes? Do some things not appear there?).

Ok, then:

Select Case f.Name.LastField(".")
case "txt"
case “htm”
case “csv”
case “xls”
case “sqlite”
MessageBox (“5”)
**//what goes here.** Answer: Jeff's code
f.CopyTo newFile
End Select

Nope. Your answers based on what I said was correct. I was just, for want of a better phrase over-thinking things. And yes a select case would be more readable.

Once again, thanks for help and showing compassion (grin)


If you put my code there, only sqllite files will be copied.
Is that what you intended?

if you want ANY of those files to work, create a boolean ‘Doit’ variable
Set Doit to false
In the cases, set Doit = true for any that you want to work

AFTER the End select,

if Doit then
//jeffs code
end if

Michael has all the pieces of the puzzle; he’ll know better than anyone what to do with them :grin:

Hi All.

Yep, after seeing what I (note the big word with one letter “I”) was doing wrong, thanks to your responses, everything is working happy as a clam now.

The next question is “How happy is a clam???”