Copy a file and create folder structure for destination

Is there some slick way to copy a file and maintain the folder structure it came from?

I have an array with path and file names, I loop through this list and use FolderItem.CopyTo to copy the files. But it copies the files to the root of the destination location.

what I want:

Source: /folder1/folder2/my.file
Dest: /abc/folder1/folder2/my.file

Source: /folder1/folder2/folder3/my2.file
Dest: /abc/folder1/folder2/folder3/my.file

what I get:

Dest: /abc/my.file
Dest: /abc/my2.file

my (simplified) code:

Var d as New FolderItem(mypath)

For Each arrFile as String in arrFilesToCopy
     f = New FolderItem(arrFile)
     f.CopyTo(d)
Next

With pure Xojo code you would have to create each level of the file system within the application. For example you would have to create folder1 and then folder2 within it and folder3 within that and then copy your file.

Alternately you could use a shell script and rsync on a Mac to achieve the same thing.

1 Like

Don’t treat folderitems as strings. You need to check at every level if the parent was created or not.

4 Likes

@Yadda_Yadda ,

Your array structure is not ideal for representing the structure you want to create. You really want a tree structure that mimics the tree structure of the file system. Then you can traverse the input tree structure and use recursion to create the output tree structure of folders and files.

My tree structure for this sort of thing is in an SQLite database.

I didn’t suggest any arrays? :slight_smile:

Ha - sorry, it’s too late at night. I was trying to amplify your response and add an extra hint. I’ve modified my post, hope it’s clearer now.

No problem :slight_smile:

Thanks. Definitely need to get my head down.

1 Like

split the folder struct
and use FolderItem with
.Exists = False then .CreateFolder

thanks everyone. i know it is not ideal, i will try the split idea from Markus. my question was more around knowing if this problem had been solved in some way that is way more clever than what i will ever achieve!

maybe create a featurerequest that CopyTo get optional argument to create folder struct.

what I get:

it should run into a exception path not exists.

Well, I think it’s the various operating systems that usually don’t provide calls to copy folders directly.

I always take from this forum, so glad for an opportunity to give back. here is the function i wrote to recursively create the folders of a path you provide. Might not be the best way, my brain thinks in (various flavours of) BASIC.

Sub CreateFolderRecursive(path As String)
     Var x As FolderItem
     Var pathDir as String

     If TargetLinux Then
       pathDir = ""
  
       For Each dir as String in path.Split("/")
         If dir.Length > 0 Then
           If pathDir <> "" Then pathDir = pathDir + "/" Else pathDir = "/"
           pathDir = pathDir + dir
      
           x = New FolderItem(pathDir)
      
           If x.Exists = False Then x.CreateFolder()
         End If
       Next
     End If
End Sub

Thanks for that - I have another project where I can use this code.

1 Like