@Kickaha, here is the scenario:
1. A folder in the Sd card root (File.DirRootExternal) called : ECA with about 10 files ( the biggest is only 160 KB, the others are very small) and only 1 subfolder. The subfolder is also called: ECA (ECA\ECA)
2. Via your code, I tried to copy the same folder and its sub to the Sd card root as ECA2 folder and the subfolder is kept as ECA: ECA2\ECA
3. The error is in the line 'MyFile=MyList.Get(i)' as you mentioned OutofboundsException.
I tried it with the subfolder empty and with it having 1 file. The sceanrio is really as simple as that.
The simple answer is you are doing something wrong, as I have now tested it with exactly the same settings etc that you describe and it works.
I may be able to get to the bottom of your problem if you actually answer the questions asked, otherwise all I can suggest is you look at your code carefully to see where you have gone wrong.
If so, what is the value of i and of OldFolderName.
Do you have any of the variables used by the sub defined as Globals?
If you zip up the program and post it on here I will have a look at it.
@Kickaha: You are absolutely correct. Your code is perfect. I had it as part of another project as a test portion where there is a lot of rogue code including dimming the list in global which turned out the culprit that misled me. Now, I am going to try to add some code to have a message box that displays the number of files and number of subfolders in each folder copied if I can.
Thank you very much for your contribution.
I would not say perfect, rather functional. Glad it is now sorted.
If you initialise some global variables - FilesCopied and FoldersCopied - and set them to zero before calling CopyDir, then they should count the files and folders created.
B4X:
If File.IsDirectory(OldRoot&"/"& OldFolderName,MyFile)=True Then
CopyDir(OldRoot & "/" & OldFolderName, NewRoot & "/" & NewFolderName, MyFile, MyFile)
FoldersCopied = FoldersCopied+1
Else
File.Copy(OldRoot&"/"&OldFolderName,MyFile, NewRoot&"/"& NewFolderName,MyFile)
FilesCopied = FilesCopied+1
End If
This leads to another question. Does this recursive stuff work only when a sub calls it's self or does all function/sub calls in B4A work this way? Say you have a sub that parses a string and you are calling it hundreds or times with a string variable, does it create a new set of variables each time it is called. This is of course being called from other places in the project and not from within the sub it's self.
This leads to another question. Does this recursive stuff work only when a sub calls it's self or does all function/sub calls in B4A work this way? Say you have a sub that parses a string and you are calling it hundreds or times with a string variable, does it create a new set of variables each time it is called. This is of course being called from other places in the project and not from within the sub it's self.
As long as the variables are local to the sub and have not been declared somewhere as globals then a new set is created for each call to the sub. When you return from the sub the variables are handed over to the garbage collector to reclaim the memory.
Just to clarify, recursion is when a sub calls itself and is very useful for tree searches and linked lists.
A linked list is a way of storing arrays of data where each data structure points to the next one in the list. The major benefit of linked lists is that you do not specify a fixed size for your list. The more elements you add to the chain, the bigger the chain gets. It is also then easy to add and remove items at any point in the list without having to move all the subsequent items.
I recommend reading the Wiki entry on this, as it explains it sooooo much better than I can, and I do not want to go any more off topic than we already are.
It depends on the scope of the variables defined within the routine.
I forgot you said this, it is a very good point and we should always be aware of the scope of our variables otherwise we run into the problems that Mahares had. When writing a lot of code I tend to prefix all globals with G_ so that I do not inadvertently use a global name as a local variable.
Maybe we need to start a programming 101 thread or sub forum, where people can explain things like this and other tricks/techniques:sign0163:
That sounds like a great idea, starting a new thread to cover this kind of information. Of course it would require someone with your knowledge to start the contributions. You have my vote!!