1

Very basic thing I am trying to accomplish.

A have a source of a file (image) stored as string (simple path).

I want to copy that file to custom destination. to be more precise to a folder name image which is located in the application root. I have checked documentation, and all of them refer to FolderItem class, which unfortunately, I cant figure out.

Any Ideas?

EnterpriseXL
  • 467
  • 8
  • 19

4 Answers4

1

The FolderItem class has a built-in FileCopy method and I'd recommend learn FolderItem because it makes file handling so much easier in the long run because it's really the only way to do it in Xojo/Real Studio.

Generally the folderitem class is initialized by using the GetFolderItem method:

dim f as folderitem = GetFolderItem("somefile.pdf")

This basic function looks for the pdf file in the same directory as the executable. If the file is somewhere else you can use the Absolute Path like "C:/SomeFolder/somefile.pdf".

There are some proscribed locations that are meant to be accessed a lot (Application Data, Preferences, etc) and the easiest way to get to them is use the SpecialFolders object. If your files was in the Application Data folder you would access it:

dim f as folderitem = SpecialFolder.ApplicationData.child("somefile.pdf")

SpecialFolder.ApplicationData returns a folderitem and child looks for the file. Folderitem child and parent methods are very important to learn.

There are many examples of how to use GetFolderItem at https://docs.xojo.com/index.php/GetFolderItem

SpecialFolder explained at https://docs.xojo.com/index.php/SpecialFolder

If you want video training, subscribers can get over 40 hours of Real Studio and Xojo training at http://www.bkeeney.com/XojoTraining/xojotraining.cgi

BKeeney Software
  • 1,299
  • 6
  • 8
1

If you are not used to object-oriented syntax, just think of FolderItem as the thing where the copy command is. So below, we make two FolderItem objects: one for the source file, and one for the destination folder. Once that is done, we can use the CopyFileTo() method of FolderItem to copy the file:

dim s as String
dim source as FolderItem
dim dest as FolderItem

s="C:\test.jpg"
source=GetFolderItem(s)

dest=GetFolderItem("C:\image")

source.CopyFileTo(dest)
1

The FolderItem class can represent any file or folder on the machine. To create a FolderItem instance for a particular absolute path, pass the path to the GetFolderItem method and store the result:

Dim SourceFile As FolderItem
SourceFile = GetFolderItem("C:\ExampleFolder\ExampleFile.txt", PathTypeAbsolute)

Once you've constructed the FolderItem you can modify its properties and call its methods to affect changes to the underlying file or directory.

To copy or move a file to another directory, you need to acquire a FolderItem representing the destination directory. Depending on the destination, you can use one of several methods to acquire the destination FolderItem.

For example,

Dim destination As FolderItem
destination = GetFolderItem("C:\DestinationExample\", PathTypeAbsolute)

or, using the SpecialFolder module:

destination = SpecialFolder.Desktop 'the user's desktop directory

or, using the parent folder of the ExecutableFile property of the App class:

destination = App.ExecutableFile.Parent 'your app's directory

Once you have both the source and destination FolderItems set up, simply call the CopyFileTo or MoveFileTo methods of the source FolderItem:

Dim SourceFile As FolderItem
SourceFile = GetFolderItem("C:\ExampleFolder\ExampleFile.txt", PathTypeAbsolute)

Dim destination As FolderItem
destination = GetFolderItem("C:\DestinationExample\", PathTypeAbsolute)

SourceFile.MoveFileTo(destination)
' or
SourceFile.CopyFileTo(destination)

Note that the CopyFileTo and MoveFileTo methods can't be used to move or copy directories, only files.

Andrew Lambert
  • 1,869
  • 1
  • 17
  • 31
1

Folderitems are a way to represent a path, independently of the OS particulars. It is important if you plan on creating apps for Windows and Mac or Linux, for instance.

In Windows, a typical path is expressed as

C:\Users\MitchMatch\Desktop\myPicture.png

In Mac OS X or Linux, the same path will be :

C:/Users/MitchMatch/Desktop/myPicture.png

FolderItem also provides ways to directly access the desktop :

Dim f as folderItem = SpecialFolder.Desktop.Child("myPicture.png")

To copy a file, you can use Xojo built in FolderItem.CopyFileTo method, or shell to the system, and use a command line.

On Windows for instance, you can use

Dim s as new shell
s.execute("Copy c:\Users\MitchMatch\Desktop\myPicture.png c:\Users\MitchMatch\Pictures")

On Mac OS X and Linux, the command is CP. Note that contrary to the Xojo CopyFileTo command, the system function is able to copy an entire directory.

Mitch Match
  • 339
  • 4
  • 14