Copying Folders in Windows

I’ve been trying to figure out how to drag a folder into Xojo so that it is copied to the main folder of an application. I’ve seen other posts on the forums regarding this by never a definitive solution.

I can get the code below to work with individual files and folders on a Mac, but on Windows, it will only copy a single file and not an entire folder.

I have a canvas in a window. On the open event I have this code:

Me.AcceptFileDrop("????")

On the DropObject Event I have this code:

[code] Dim ft as new filetype
Dim f as FolderItem = obj.FolderItem
Dim T as String = f.DisplayName

    If obj.FolderItemAvailable then
            
            //Show File Name
            txtFile.Text=T
            ft.name=txtFile.Text

            f.CopyFileTo(getfolderitem("").child(ft.name))
            
    End If[/code]

I was wondering if there was a way to actually drag the contents of an entire folder and drop it in another folder in Windows? The code above only works with individual files in Windows but works perfectly with both folders and individual files on a Mac.

Not sure what you are trying to do here.
Couple of things:

Dim T as String = f.DisplayName[/code]

If the user has 'hide file extensions for known file types' turned on, a file called  THIS.TXT will show up as THIS
Thats not the filename if you want to use it to work out a folderitem

[code]ft.name=txtFile.Text[/code]

You set the name of the FILETYPE to be the displayname of the object??


[code]f.CopyFileTo(getfolderitem("").child(ft.name))[/code]
probably ought to be 
[code]f.CopyFileTo(getfolderitem("").child(f.name))[/code]

This tries to copy <something> to the same folder as the app.
Thats not allowed by Windows if the app is in Program Files.


Here is a copyfolder routine:

[code]Sub Copyfolder(foldernamefrom as folderitem,foldernameto as folderitem)
  Dim i as integer
  if FoldernameFrom.Directory = false then return
  if foldernameto.exists = false then 
    FoldernameTo.CreateAsFolder
  else
    try
      deletefolder foldernameto
      FoldernameTo.CreateAsFolder
    catch
    end try
  end if
  
  for i = 1 to FoldernameFrom.Count
    if FoldernameFrom.item(i).Directory then
      try
        dim subfolder as FolderItem = GetFolderItem(FoldernameTo.AbsolutePath + FoldernameFrom.Item(i).Name)
        
        CopyFolder (FoldernameFrom.item(i),subfolder)
      catch
      end try
    else
      try
        dim subfile as FolderItem = GetFolderItem(FoldernameTo.AbsolutePath + FoldernameFrom.Item(i).Name)
        FoldernameFrom.Item(i).CopyFileTo (subfile)
      catch
      end try
    end if
  next
End Sub[/code]


So try something like this, but remember what I said about writing to the application folder in Windows:



       

    [code]    If obj.FolderItemAvailable then
                 Dim f as FolderItem = obj.FolderItem
  if f <> nil and f.exists  and f.directory then
Copyfolder f,getfolderitem("").child(f.name)
end if

        End If

Hi Jeff,

Thank you so much for taking the time to help me. That worked perfectly!! It is just what I needed. I am not copying the folder to the Applications folder, but instead to the C:\Users\Public folder which is writable and were the application is stored.

Thanks again for your help. I really appreciate it.

James, you know there is an Applications folder to store the… applications, do you ?

Yes I know, but that is not where we want to store the app. You cannot write data to Program Files.

I think Emile is referring to the application data folder:

SpecialFolder.ApplicationData

Accessing System Folders

However, it appears you want the data stored in the same place as the app, so that might not be for you.