New topic: 

Deleting a file when I already have the filename

<http://forums.realsoftware.com/viewtopic.php?t=44791>

         Page 1 of 1
   [ 4 posts ]                 Previous topic | Next topic          Author  
Message        Farflame          Post subject: Deleting a file when I already 
have the filenamePosted: Fri Jul 20, 2012 6:43 pm                         
Joined: Tue Oct 05, 2010 6:25 am
Posts: 152                I've got my file as a folderitem in g, so I just do 
g.delete to delete it - well actually I'm going through a list of files and 
deleting them, so it's g.item(f).delete. This works fine. But then I need to 
delete another file with the same name but a different extension. I know how to 
take the filename and convert it to the new filename, with 
FileName=Left(FileName,Len(FileName)-4)+".htm". So now I have the filename, but 
I don't know how to delete it just from the name.

Incase I'm not explaining that very well, here's my loop (stripped down to the 
relevant parts).

Dim g As FolderItem

#If DebugBuild Then
  g=App.ExecutableFile.Parent.Parent.Parent.Child("reports")
#Else
  g=App.ExecutableFile.Parent.Parent.Child("reports")
#endif

for f as integer=1 to g.Count
  if Right(g.Item(f).Name,3)="txt" then
  
  ' Store the file name for later.
  dim FileName as string=g.AbsolutePath+g.Item(f).Name
  
  ' Change the filename from the .txt to the .htm version
  FileName=Left(FileName,Len(FileName)-4)+".htm"
  
  ' I email out these files here, this loop isn't just for deleting them. 
They're to be processed, and then deleted.
  
  g.Item(f).Delete ' This deletes the first file, the text file.
  
  (Now I want to delete the file 'Filename' here).
  
  end if
next
   
                             Top                Farflame          Post subject: 
Re: Deleting a file when I already have the filenamePosted: Fri Jul 20, 2012 
7:01 pm                         
Joined: Tue Oct 05, 2010 6:25 am
Posts: 152                Ok, I've partly solved this, I think I just need to 
create a new Folderitem from the file and then delete that.

dim F2 as FolderItem
F2=GetFolderItem(FileName)
f2.Delete


The only problem is, this reduces the count in the loop, so the loop doesn't 
complete properly. Should probably work with a different kind of loop though.

One thing, does the delete command above completely delete the file? It doesn't 
put it in the recycle bin or anything?   
                             Top                charonn0          Post subject: 
Re: Deleting a file when I already have the filenamePosted: Fri Jul 20, 2012 
7:24 pm                                 
Joined: Mon Apr 02, 2007 2:08 am
Posts: 849
Location: San Francisco, CA, USA                Farflame wrote:The only problem 
is, this reduces the count in the loop, so the loop doesn't complete properly. 
Should probably work with a different kind of loop though.


When using a For...Next loop to remove items from the thing you're looping over 
(be it a directory or an array) do it in reverse order with the DownTo keyword:

for f as integer= g.Count DownTo 1      
_________________
Boredom Software  
                             Top                Farflame          Post subject: 
Re: Deleting a file when I already have the filenamePosted: Fri Jul 20, 2012 
9:07 pm                         
Joined: Tue Oct 05, 2010 6:25 am
Posts: 152                Ah yeah thanks, that's much easier than what I used. 
I used a do-loop with a manual counter, not very pretty. I'll change it over to 
your method    
                             Top             Display posts from previous: All 
posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost 
timeSubject AscendingDescending          Page 1 of 1
   [ 4 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]

Reply via email to