How do I copy a file

I have the following code which keeps giving me a Nil object error but not sure why?

dim oldFile as FolderItem = SpecialFolder.Library.Child( "/Mail/V4/MailData/Envelope Index" ) dim newFile as FolderItem = SpecialFolder.Temporary.Child("Envelope Index - NWC " + replaceall( dt.SQLDateTime, ":", "" ) ) oldFile.CopyFileTo( newFile )

That’s not how you use .Child that’s why.

SpecialFolder.Library.Child("Mail").Child("V4").Child("MailData").Child("Envelope Index")

http://documentation.xojo.com/index.php/FolderItem.Child

Also, if you’re not sure a post went through, reload the main forum index to check :slight_smile:

Ok I tried that before but I will give it another go. Re the post, the forum told me it hadn’t been able to communicate with the server so I pressed try again, didn’t realise it was going to post twice :wink:

It’s okay, someone will clean it up.

You might also want to check into http://documentation.xojo.com/index.php/GetTemporaryFolderItem because you won’t have to manage things in the temporary directory with timestamps.

Thanks Tim, I deleted the other posts to keep things clean.

I will look at that. I made the changes as you said but I get a Nil Object error still on the oldFile. Does Apple do something naughty with Library files for security that might be stopping me from copying the file?

You end up with exceptions if you attempt to access a folderitem that is more than one level invalid.
For example, my ~/Library/Mail/ folder does not have a V4, so you may be running into an issue there.

A simple workaround is to create each level and check for .Exits = true and error out if not.
Otherwise you can try…catch it but then you don’t know where along the path it became invalid.

Thanks I will try that. Out of interest what version of OSX are you running? As I thought all recent versions of OSX have a V4 folder under Mail?

El Cap

Ah ok that makes sense, need to use a different location for that one. Cheers

Never assume that any folder exists on a user’s machine. Always check for it and create it if missing.

@Roger Clary yes, I agree, I thought it was an Apple folder that always exists but have now put some checks around it to make sure. The weird thing is that the folder and file does exist on my device but I am still getting an exception so will need to dig around some more.

Apple has changed the directory structures of various apps on every release. You can never trust that you have the right path. You have to check every time. Microsoft does this too.