FolderItem MoveTo only moving some files

I have 9 files in the c:\temp\dp folder I am trying to move to c:\temp\archive but only 5 out of the 10 move. This is all part of a bigger project that ftps these files and moves them upon successful transfer. I haven’t found a post that helps and I’m not sure what I’m doing wrong, here’s the code:

Var file, localFolder, archiveFolder As FolderItem

localFolder = Volume(0).Child(“temp”).Child(“dp”)
archiveFolder = Volume(0).Child(“temp”).Child(“archive”)

For i As Integer = 0 To localFolder.Count - 1
file = localFolder.ChildAt(i)

If file <> Nil Then
file.MoveTo(archiveFolder)
End If
Next

Don’t have localFolder.Count in your For loop. It changes during execution as you move files.

Also:

when you post code, please do this:

a) put the code in your post
b) Select it all with the mouse
c) Click on </>

You might do better with:

while  (localFolder.Count>0)
  file = localFolder.ChildAt(0)
  if  (file<>Nil)  then file.MoveTo (archiveFiolder)
wend

except that calculating .Count every time round the loop might be expensive.

So what about:

Var  i, lim as Integer

lim = localFolder.Count - 1

For i  = lim Downto 0
  file = localFolder.ChildAt(0)                // The 0 is deliberate
  If file <> Nil Then file.MoveTo(archiveFolder)
next

Perhaps try counting backwards. By the time you’ve moved 5, the counter is at 5 and then it stops.

Thanks guys! In case it helps someone else this is what I ended up with:

Var file, localFolder, archiveFolder As FolderItem
Var ii as integer

localFolder = Volume(0).Child(“temp”).Child(“yec”).Child(“dp”)
archiveFolder = Volume(0).Child(“temp”).Child(“yec”).Child(“archive”)

ii = localFolder.Count
MessageBox(“count=” + str(ii))
while (ii > 0)
file=localFolder.ChildAt(ii-1)
if (file<>Nil) then
file.MoveTo(archiveFolder)
end if
ii = ii - 1
wend

You could probably replace the loop with:

for each f as FolderItem in localFolder.Children
  f.MoveTo(archiveFolder)
next
1 Like

Why do you not use .ChildAt(0) ??

If for some reason, ChildAt(0) failed to move, the entire operation would fail because you’re trying to move the same unmovable file ii number of times.

Then would you not get an IOException?

Of course you are right. I haven’t caught up to the new mechanisms. At this point it comes down to how the OP wants to handle an error, via Try…Catch or Exception.