New topic: OutofBounds exception for moving all files out of a folder
<http://forums.realsoftware.com/viewtopic.php?t=47723> Page 1 of 1 [ 14 posts ] Previous topic | Next topic Author Message nilesmitchell Post subject: OutofBounds exception for moving all files out of a folderPosted: Sat Apr 27, 2013 8:26 pm Joined: Mon Jan 17, 2011 4:51 pm Posts: 125 I can't seem to figure this part out. I'm trying to move all the files out of a certain directory into another directory that is chosen by the user. Even though similar code worked for copying multiple files from an open dialog box I used earlier in my code, it doesn't seem to work for just a folder. I get an OutofBounds exception when running the program. It chokes on the line "f2 = dir2.Item(i)" // Choose Destination and Move dim dir2 as FolderItem dir2= SpecialFolder.ApplicationData.Child("My Great App").Child("temp") Dim dlg2 As New SelectFolderDialog dlg2.InitialDirectory = SpecialFolder.Desktop Dim dest As FolderItem dest = dlg2.ShowModal If dest = Nil Then w2.hide return End dim f2 as FolderItem if dir2.Count = 1 then f2 = dir2.Item(1) else for i as integer = 0 to dir2.Count-1 f2 = dir2.Item(i) f2.MoveFileTo (dest) next end f2.MoveFileTo (dest) Top charonn0 Post subject: Re: OutofBounds exception for moving all files out of a foldPosted: Sat Apr 27, 2013 8:58 pm Joined: Mon Apr 02, 2007 2:08 am Posts: 1201 Location: San Francisco, CA, USA FolderItem.Item is one-based rather than zero-based, but your For loop starts at zero. Also, when moving or deleting FolderItems in a For loop, do the loop in reverse order to avoid getting an OutOfBoundsException: for i as integer = dir2.Count DownTo 1 _________________ Boredom Software Top nilesmitchell Post subject: Re: OutofBounds exception for moving all files out of a foldPosted: Sat Apr 27, 2013 9:12 pm Joined: Mon Jan 17, 2011 4:51 pm Posts: 125 Thank you for explaining that. I no longer get an error on that line, but now get a NilObjectException on the next line: f2.MoveFileTo (dest) Top nilesmitchell Post subject: Re: OutofBounds exception for moving all files out of a foldPosted: Sat Apr 27, 2013 9:23 pm Joined: Mon Jan 17, 2011 4:51 pm Posts: 125 Never mind. It's magically working now! Thanks for the info on FolderItems being 1 based! Top nilesmitchell Post subject: Re: OutofBounds exception for moving all files out of a foldPosted: Sat Apr 27, 2013 9:26 pm Joined: Mon Jan 17, 2011 4:51 pm Posts: 125 And now the NilObjectException error is back again! Any ideas? Top DaveS Post subject: Re: OutofBounds exception for moving all files out of a foldPosted: Sat Apr 27, 2013 9:56 pm Joined: Sun Aug 05, 2007 10:46 am Posts: 4807 Location: San Diego, CA Did you reverse the order of the loop? That is a MUST do..... and you might want to use TRUEITEM instead of ITEM _________________ Dave Sisemore iMac I7[2012], OSX Mountain Lion 10.8.3 RB2012r2.1 Note : I am not interested in any solutions that involve custom Plug-ins of any kind Top nilesmitchell Post subject: Re: OutofBounds exception for moving all files out of a foldPosted: Sat Apr 27, 2013 10:07 pm Joined: Mon Jan 17, 2011 4:51 pm Posts: 125 You'll have to explain reverse the order of the loop. Top DaveS Post subject: Re: OutofBounds exception for moving all files out of a foldPosted: Sat Apr 27, 2013 10:32 pm Joined: Sun Aug 05, 2007 10:46 am Posts: 4807 Location: San Diego, CA for i as integer =dir2.Count downto 1 f2 = dir2.Item(i) f2.MoveFileTo (dest) next _________________ Dave Sisemore iMac I7[2012], OSX Mountain Lion 10.8.3 RB2012r2.1 Note : I am not interested in any solutions that involve custom Plug-ins of any kind Top timhare Post subject: Re: OutofBounds exception for moving all files out of a foldPosted: Sat Apr 27, 2013 10:36 pm Joined: Fri Jan 06, 2006 3:21 pm Posts: 12262 Location: Portland, OR USA Running through the items in a folder in reverse produces the worst possible performance. Instead, append the TrueItem's to an array and loop through the array to move them. The reason that you would (normally) run the loop backwards is that as you remove an item, all the others shift down, meaning that a) you skip some, and b) you run off the end too early. And definitely use TrueItem instead of Item. You don't want to move a file from some other directory, just because this folder contained a shortcut to it. Move the shortcut (trueitem), not the actual file. Top charonn0 Post subject: Re: OutofBounds exception for moving all files out of a foldPosted: Sat Apr 27, 2013 10:37 pm Joined: Mon Apr 02, 2007 2:08 am Posts: 1201 Location: San Francisco, CA, USA When you use a For...Next loop to move or delete items from a directory you need to do it in reverse order otherwise the loop will finish but some files will be skipped. Consider this loop: For index As Integer = 1 To SomeDirectory.Count SomeDirectory.Item(index).MoveFileTo(SpecialFolder.Desktop) Next This looks like it should work, but SomeDirectory.Count is recalculated every at the start of each loop. If there were 10 items in the directory, on the first iteration SomeDirectory.Count=10; on the second iteration it's 9, then 8, etc. because items are being moved. At the same time, the loop index (called index, in the above code) is being incremented by one at the beginning of each loop. The progression looks like this: i=1 count=10 i=2 count=9 i=3 count=8 i=4 count=7 i=5 count=6 i=6 count=5 <--- loop terminates, skipping half of the files! Doing it in reverse order with the DownTo keyword (see my previous reply) takes care of that: i=10 count=10 i=9 count=9 i=8 count=8 i=7 count=7 i=6 count=6 i=5 count=5 i=4 count=4 i=3 count=3 i=2 count=2 i=1 count=1 <---loop terminates with no skipped files _________________ Boredom Software Top nilesmitchell Post subject: Re: OutofBounds exception for moving all files out of a foldPosted: Sat Apr 27, 2013 11:02 pm Joined: Mon Jan 17, 2011 4:51 pm Posts: 125 Well then, yes I reversed the order, as was originally posted by charonn0. That took care of the OutofBounds exception. What I'm getting now is a NilObjectException on the line: f2.MoveFileTo (dest) And it is random. Sometimes I don't get this error. To sum up: Original OutofBounds error fixed by reversing the loop. Now sometimes get NilObjectException error on the "f2.MoveFileTo (dest)" line. P.S. Trueitem not an issue....this is a controlled Application Support folder which cannot contain Shortcuts because I don't allow them to be copied in the in the first place. Top DaveS Post subject: Re: OutofBounds exception for moving all files out of a foldPosted: Sat Apr 27, 2013 11:13 pm Joined: Sun Aug 05, 2007 10:46 am Posts: 4807 Location: San Diego, CA Assuming (I hate that word) that DEST is NOT NIL and therefore not your problem.... that only leaves F2 Add a check to insure two conditions...... a) F2 is not NIL b) F2 exists (what? of course it exists, it is in DIR2!..... protected directory or not...... check it anyways.... better to get in the habit of checking, and not assuming anything) for i as integer =dir2.Count downto 1 f2 = dir2.Item(i) if f2<>nil then if f2.exists then f2.MoveFileTo (dest) end if end if next IFs can be combined into one... I put them this way for illustration purposes Also.... you might want to check if F2 points to a DIRECTORY. Do it correctly now, and you will have a code snippet you can drop into any future project and know that it will get the job done. _________________ Dave Sisemore iMac I7[2012], OSX Mountain Lion 10.8.3 RB2012r2.1 Note : I am not interested in any solutions that involve custom Plug-ins of any kind Top markwalsh Post subject: Re: OutofBounds exception for moving all files out of a foldPosted: Sun Apr 28, 2013 8:02 am Joined: Wed May 20, 2009 4:43 pm Posts: 954 nilesmitchell wrote:Well then, yes I reversed the order, as was originally posted by charonn0. That took care of the OutofBounds exception. What I'm getting now is a NilObjectException on the line: f2.MoveFileTo (dest) Which one? In your original code, you have 2 lines with that code- one within the for i... loop, and one outside. I am assuming this is probably the cause, although I can't tell what your updated code is. I am assuming that line should be moved into the first 'If' clause: dim f2 as FolderItem if dir2.Count = 1 then f2 = dir2.Item(1) f2.MoveFileTo (dest) // <<< Move that line here else ... although, I would probably remove that entirely, and just use the for/loop exclusively whether there is only one item or many items. _________________ RB 2009r4 Windows XP Top nilesmitchell Post subject: Re: OutofBounds exception for moving all files out of a foldPosted: Sun Apr 28, 2013 9:31 am Joined: Mon Jan 17, 2011 4:51 pm Posts: 125 Thanks Dave! that seems to work great! Top Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending Page 1 of 1 [ 14 posts ] -- Over 1500 classes with 29000 functions in one REALbasic plug-in collection. The Monkeybread Software Realbasic Plugin v9.3. http://www.monkeybreadsoftware.de/realbasic/plugins.shtml [email protected]
