Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Thu Sep 19, 2019 4:01 pm
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: Move and Copy folders code sample
PostPosted: Sat Mar 08, 2008 1:07 am 
Offline

Joined: Wed Jan 30, 2008 11:45 am
Posts: 7
I spent a lot of time looking for these commands only to discover that they didn't exist. I then decided to code them up. Afterwords, I though others might appreciate having these subroutines... So if you ever need them... Here they are.


Sub CopyFolder(byval FoldernameFrom as folderItem, byval FoldernameTo as folderitem)
Dim i as integer
if FoldernameFrom.Directory = false then return
FoldernameTo.CreateAsFolder
for i = 1 to FoldernameFrom.Count
if FoldernameFrom.item(i).Directory then
dim subfolder as FolderItem = GetFolderItem(FoldernameTo.AbsolutePath + FoldernameFrom.Item(i).Name)
CopyFolder (FoldernameFrom.item(i),subfolder)
else
dim subfile as FolderItem = GetFolderItem(FoldernameTo.AbsolutePath + FoldernameFrom.Item(i).Name)
FoldernameFrom.Item(i).CopyFileTo (subfile)
end if
next


Sub MoveFolder(byval FoldernameFrom as folderItem, byval FoldernameTo as folderitem)
if FoldernameFrom.Directory = false then return
FoldernameTo.CreateAsFolder
do
if FoldernameFrom.item(1).Directory then
dim subfolder as FolderItem = GetFolderItem(FoldernameTo.AbsolutePath + FoldernameFrom.Item(1).Name)
MoveFolder (FoldernameFrom.item(1),subfolder)
FoldernameFrom.item(1).Delete
else
dim subfile as FolderItem = GetFolderItem(FoldernameTo.AbsolutePath + FoldernameFrom.Item(1).Name)
FoldernameFrom.Item(1).CopyFileTo (subfile)
FoldernameFrom.Item(1).Delete
end if
loop until (Foldernamefrom.Count =0)
FoldernameFrom.Delete


Top
 Profile  
Reply with quote  
 Post subject: Re: Move and Copy folders code sample
PostPosted: Sat Mar 08, 2008 1:33 am 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
You might consider using TrueItem() instead of Item() if you're copying the contents of a folder. You want to copy the shortcut/alias/link, not the target.

Tim


Top
 Profile  
Reply with quote  
 Post subject: Re: Move and Copy folders code sample
PostPosted: Sun Mar 09, 2008 8:36 pm 
Offline

Joined: Wed Jan 30, 2008 11:45 am
Posts: 7
Thanks, I'll make that change. Any other improvements would be appreciated.

Thanks again.


Top
 Profile  
Reply with quote  
 Post subject: Re: Move and Copy folders code sample
PostPosted: Tue Mar 11, 2008 1:17 am 
Offline

Joined: Mon Jul 17, 2006 10:39 am
Posts: 1718
You also might want to implement your own stack instead of relying on RB's method stack; it'll save you from StackOverflowExceptions from deep folder nests later on. Such a thing could be done very easily with an array.

_________________
Kenneth McCleary
ken@mccleary.me


Top
 Profile  
Reply with quote  
 Post subject: Re: Move and Copy folders code sample
PostPosted: Wed Mar 12, 2008 4:31 pm 
Offline

Joined: Wed Jan 30, 2008 11:45 am
Posts: 7
Unfortunately, I don't understand what you mean... Could you elaborate.

Thanks


Top
 Profile  
Reply with quote  
 Post subject: Re: Move and Copy folders code sample
PostPosted: Sat Apr 19, 2008 11:25 am 
Offline

Joined: Sun Oct 09, 2005 11:58 am
Posts: 273
Location: Northeastern, Nevada, USA
kendoll wrote:
You also might want to implement your own stack instead of relying on RB's method stack; it'll save you from StackOverflowExceptions from deep folder nests later on. Such a thing could be done very easily with an array.

Yes, I'd also like to seen an example of what you mean.
Thanks!


Top
 Profile  
Reply with quote  
 Post subject: Re: Move and Copy folders code sample
PostPosted: Sat Apr 19, 2008 1:24 pm 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
There are 2 approaches to iterating through an arbitrarily deep tree structure: recursive and non-recursive. Recursive code is a method that calls itself to process the next level down. The benefit is that it can be very compact (and elegant) code. The downside is that every recursive call uses up more of the Stack. In some instances that can cause you to run out of stack space, resulting in a StackOverflowException.

The non-recursive approach involves maintaining your own array of items to process. A typical approach might be to handle files immediately, but append folders to the array. The code then sits in a loop, pulling the next folder off the array and processing it, and potentially adding more folders to the array. It loops until there are no more folders to process. The advantage to this approach is that you use a controlled amount of stack resources; your data is populated in global memory (sometimes called "heap"), of which there is a lot more available. The downside is that the code and logic can be more difficult, and not as pretty. (But it's not really that hard.)

HTH,
Tim


Top
 Profile  
Reply with quote  
 Post subject: Re: Move and Copy folders code sample
PostPosted: Thu Dec 04, 2008 3:12 pm 
Offline

Joined: Mon Jan 22, 2007 9:47 am
Posts: 131
Quote:
I'd also like to seen an example


Here's an example of mine showing a "non-recursive approach".

Dim oListOfAllFolderItemsInSourceFolder() As FolderItem

Dim oFolderList() As FolderItem
oFolderList.Append(SourceFolder)

while (UBound(oFolderList) >= 0)
for i As Integer = 1 to oFolderList(0).Count
Dim oCurrentItem As FolderItem
oCurrentItem = oFolderList(0).TrueItem(i)

if (oCurrentItem = nil) then continue
if (not oCurrentItem.Exists) then continue
if (oCurrentItem.Name = ".") or (oCurrentItem.Name = "..") then continue

if (oCurrentItem.Directory) then
oFolderList.Append(oCurrentItem)
end if

'just build a list - of course you could process the oCurrentItem right here
oListOfAllFolderItemsInSourceFolder.Append(oCurrentItem)
next

oFolderList.Remove(0)
wend


Top
 Profile  
Reply with quote  
 Post subject: Re: Move and Copy folders code sample
PostPosted: Sun Dec 14, 2008 5:23 pm 
Offline
User avatar

Joined: Thu Jan 04, 2007 1:00 pm
Posts: 176
Location: Lego® Deathstar, Orbiting Above Scottsdale, AZ, USA
Since this topic is still visible, I thought it might be a good idea to bring up that Linux (and other Unix variants) have support for full tree copy or move available in a simple shell call. I've added a bit of support code, but you could achieve this if you're using the Save Dialog since the paths would have to exist if the user has selected them. As always, please double check the code in a test environment before using it in a live application.

Sub CopyFolderUsingShell(FolderFromPath As String, FolderToPath As String) As Boolean
Dim theShell As New Shell

// Check to make sure the destination folder exists
// Note that if you're passing the as a FolderItem.ShellPath, you should
// alleviate the double quotes surrounding the paths.
theShell.Execute "[ -f """ + FolderToPath + """ ]
If theShell.ErrorCode <> 0 Then
// no - then create it (-p says to create the full path including any missing parents)
theShell.Execute "mkdir -p """ + FolderToPath + """"
// we should check the ErrorCode and handle errors...
End If

// Because we want the process to be interactive, I use and Mode 2 shell and
// the DoEvents() method for UI updates (I know, I know, but this works)
theShell.Mode = 2
// This is the actual shell command that does all of the work
theShell.Execute "cp -rv """ + FolderToPath + """ """ + FolderToPath + """"
Do
App.DoEvents(100)
// Maybe update Info in the GUI
Loop Until Not theShell.IsRunning
// The copy is completed
If theShell.ErrorCode <> 0 Then
Return False
Else
Return True
End If
End Sub

Sub MoveFolderUsingShell(FolderFromPath As String, FolderToPath As String) As Boolean
Dim theShell As New Shell

// Check to make sure the destination folder exists
theShell.Execute "[ -f """ + FolderToPath + """ ]
If theShell.ErrorCode <> 0 Then
// no - then create it (-p says to create the full path including any missing parents)
theShell.Execute "mkdir -p """ + FolderToPath + """"
// we should check the ErrorCode and handle errors...
End If

// Because we want the process to be interactive, I use and Mode 2 shell and
// the DoEvents() method for UI updates (I know, I know, but this works)
theShell.Mode = 2
// This is the actual shell command that does all of the work
theShell.Execute "mv """ + FolderToPath + """ """ + FolderToPath + """"
Do
App.DoEvents(100)
// Maybe update info in the GUI...
Loop Until Not theShell.IsRunning
// The copy is completed
If theShell.ErrorCode <> 0 Then
Return False
Else
Return True
End If
End Sub


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group