Page 1 of 1

Why does this crash every time???

Posted: Fri Nov 14, 2003 12:43 am
by bcgreen
Yep, me again with another stupid question. Why does this crash every time the "renamefiles" procedure is called?

Code: Select all

Enumeration
  #Window_0
EndEnumeration

Enumeration
  #oldextbox
  #newextbox
  #renamebutton
  #browsebutton
  #Gadget_5
  #Gadget_6
  #listbox
EndEnumeration

Procedure Open_Window_0()
  If OpenWindow(#Window_0, 259, 121, 652, 359,  #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_TitleBar , "batchRenamer")
    If CreateGadgetList(WindowID())
      StringGadget(#oldextbox, 30, 330, 110, 20, "")
      StringGadget(#newextbox, 260, 330, 120, 20, "")
      ButtonGadget(#renamebutton, 150, 330, 100, 20, "Rename File!")
      ButtonGadget(#browsebutton, 20, 10, 140, 30, "Browse For Folder")
      TextGadget(#Gadget_5, 30, 310, 110, 20, "Old Extension:")
      TextGadget(#Gadget_6, 260, 310, 120, 20, "New Extension:")
      ListViewGadget(#listbox, 20, 50, 620, 260)
      
    EndIf
  EndIf
EndProcedure


Procedure listfiles(path$)
  result = ExamineDirectory(1, path$, "*.*")
  If result = 0: MessageRequester("Error!", "Can't open directory!"):ProcedureReturn:EndIf
  ClearGadgetItemList(#listbox)
  Repeat
   result = NextDirectoryEntry()
  If result = 1
    AddGadgetItem(#listbox, -1, DirectoryEntryName())
  ElseIf result = 0
    ProcedureReturn
  EndIf
  ForEver
EndProcedure

Procedure renamefiles(oldext$, newext$)
  ExamineDirectory(1, path$, "*.*")
  ClearGadgetItemList(#listbox)
  Repeat
    result = NextDirectoryEntry()
    If result = 1
      filename$ = DirectoryEntryName()
      oldfilename$ = filename$
        If Right(filename$, 3) = oldext$
          filename$ = Left(filename$, Len(filename$)-3) + newext$
          RenameFile(path$ + oldfilename$, path$ + filename$)
        EndIf
    AddGadgetItem(#listbox, -1, filename$)
    ElseIf result = 0
    ProcedureReturn
    EndIf
  ForEver
EndProcedure

Global path$

Open_Window_0()

Repeat
  
  Event = WaitWindowEvent()
  
  If Event = #PB_EventGadget
    
    GadgetID = EventGadgetID()
   
    If GadgetID = #renamebutton
      If GetGadgetText(#oldextbox) = "" Or GetGadgetText(#newextbox) = ""
        MessageRequester("Error!", "No extensions specified!")
      ElseIf path$ = ""
        MessageRequester("Error!", "No path specified!")
      Else
      oldext$ = GetGadgetText(#oldextbox)
      newext$ = GetGadgetText(#newextbox)
      renamefiles(oldext$, newext$)
      EndIf
      
    ElseIf GadgetID = #browsebutton
      path$ = PathRequester("Choose a folder:", "")
      listfiles(path$)
    EndIf
    
  EndIf
  
Until Event = #PB_EventCloseWindow

End
It seems that as soon as the renamefiles proc is called and it hits the ExamineDirectory command, it craps out. I've run it with all the statements in the proc commented out except ExamineDirectory, and that's what seems to crash it...is there a way to "close" a directory, before using "ExamineDirectory()" again? Is that what's doing it? I've also tried changing the directory ID to "2", with the same results....

Thanks, as always!
Bryan

Posted: Fri Nov 14, 2003 12:57 am
by Pupil
Any globals that you want to use in a procedure MUST be placed on a source line ABOVE the beginning of the procedure, this is a restriction that comes with a singlepass compiler.. check your Path$ variable placement ;)

Posted: Fri Nov 14, 2003 1:01 am
by bcgreen
Hmmm..... but when I place :"Debug path$" after the ExamineDirectory() statement in the renamefiles proc, it displays the same directory that was used in the "listfiles" proc....

I also just tried placing the statement "Shared path$" in the listfiles proc, and got rid of the Global path$ statement....still doesn't work.

The procedure is getting the path stored in the path$ variable...but the ExamineDirectory() statement seems to crash the program...

Bryan

Posted: Fri Nov 14, 2003 2:12 am
by bcgreen
I made it work by removing the renamefiles proc and placing the code in the same block as the "#renamebutton" code....so it didn't work because the code was in a procedure.....why?

****UPDATE****

Put the renamefiles code back into a procedure and got it to work by putting the "Shared path$" command in that proc AND the listfiles proc.
Don't know why, but that did the trick. Thought you only had to use the Shared command once to share a variable across multiple procs...at least that's what the docs say...

Bryan

Hello

Posted: Fri Nov 14, 2003 4:41 pm
by DominiqueB
In fact you just need to put the "Shared path$" in the RenameFiles() proc, cause the ListFiles() one receive the var as argument so it sees it.

For the "Shared" use, you need it in a proc to tell the compiler you need to use the same var that is named outside of the proc.

So if i'm right you have to put "Shared" in each proc where you need inside to use a var that is declared outside of it !

Dominique.

Posted: Fri Nov 14, 2003 8:29 pm
by Henrik
For use with Globals like pupil said
Like this:

Code: Select all


Enumeration 
  #Window_0 
EndEnumeration 

Enumeration 
  #oldextbox 
  #newextbox 
  #renamebutton 
  #browsebutton 
  #Gadget_5 
  #Gadget_6 
  #listbox 
EndEnumeration 


Global path$ ; <- ****** changed *******


Procedure Open_Window_0() 
  If OpenWindow(#Window_0, 259, 121, 652, 359,  #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_TitleBar , "batchRenamer") 
    If CreateGadgetList(WindowID()) 
      StringGadget(#oldextbox, 30, 330, 110, 20, "") 
      StringGadget(#newextbox, 260, 330, 120, 20, "") 
      ButtonGadget(#renamebutton, 150, 330, 100, 20, "Rename File!") 
      ButtonGadget(#browsebutton, 20, 10, 140, 30, "Browse For Folder") 
      TextGadget(#Gadget_5, 30, 310, 110, 20, "Old Extension:") 
      TextGadget(#Gadget_6, 260, 310, 120, 20, "New Extension:") 
      ListViewGadget(#listbox, 20, 50, 620, 260) 
      
    EndIf 
  EndIf 
EndProcedure 


Procedure listfiles(SomeString$) ; <- ****** changed *******

path$=SomeString$ ; <- ****** changed *******

  result = ExamineDirectory(1, path$, "*.*") 
  If result = 0: MessageRequester("Error!", "Can't open directory!"):ProcedureReturn:EndIf 
  ClearGadgetItemList(#listbox) 
  Repeat 
   result = NextDirectoryEntry() 
  If result = 1 
    AddGadgetItem(#listbox, -1, DirectoryEntryName()) 
  ElseIf result = 0 
    ProcedureReturn 
  EndIf 
  ForEver 
EndProcedure 

Procedure renamefiles(oldext$, newext$) 
  ExamineDirectory(1, path$, "*.*") 
  ClearGadgetItemList(#listbox) 
  Repeat 
    result = NextDirectoryEntry() 
    If result = 1 
      filename$ = DirectoryEntryName() 
      oldfilename$ = filename$ 
        If Right(filename$, 3) = oldext$ 
          filename$ = Left(filename$, Len(filename$)-3) + newext$ 
          RenameFile(path$ + oldfilename$, path$ + filename$) 
        EndIf 
    AddGadgetItem(#listbox, -1, filename$) 
    ElseIf result = 0 
    ProcedureReturn 
    EndIf 
  ForEver 
EndProcedure 

;Global path$ 

Open_Window_0() 

Repeat 
  
  Event = WaitWindowEvent() 
  
  If Event = #PB_EventGadget 
    
    GadgetID = EventGadgetID() 
    
    If GadgetID = #renamebutton 
      If GetGadgetText(#oldextbox) = "" Or GetGadgetText(#newextbox) = "" 
        MessageRequester("Error!", "No extensions specified!") 
      ElseIf path$ = "" 
        MessageRequester("Error!", "No path specified!") 
      Else 
      oldext$ = GetGadgetText(#oldextbox) 
      newext$ = GetGadgetText(#newextbox) 
      renamefiles(oldext$, newext$) 
      EndIf 
      
    ElseIf GadgetID = #browsebutton 
      path$ = PathRequester("Choose a folder:", "") 
      listfiles(path$) 
    EndIf 
    
  EndIf 
  
Until Event = #PB_EventCloseWindow 

End
best regards
Henrik

Posted: Fri Nov 14, 2003 10:15 pm
by bcgreen
Thanks everyone!

Ya know, if it weren't for the PureBasic community, I don't know what I'd do...

:D Bryan