BBC BASIC for Windows
General >> General Board >> File exists
http://bb4w.conforums.com/index.cgi?board=general&action=display&num=1307464514

File exists
Post by MrStreet on Jun 7th, 2011, 4:35pm

My colleague has reported that my program generates the "File exists" error the second time they loaded it. According to the BB4W documentation "This error is generated if you try to rename a file and a file with the new name already exists" but I am not trying to rename a file!

The only file handling I am doing is:
Code:
      def fn_folderExists(dir$)
      rem returns true if the directory exists
      *| docs -from  http://bb4w.wikispaces.com/Checking+whether+a+directory+exists
      local F%
      F% = openin(dir$+"\NUL")
      if F% close #F%
      = F% 


and Code:
proc_create 
which uses:

Code:
file% = openout(fileName$) 


The filename is obtained using the following function, but the error is generated before this code is executed.

Code:
      
      deffn_getFileName
      local ok% : ok% = 1
      local Quit% : Quit% = 2
      local Edit% : Edit% = 100
      local Static% : Static% = 101
      local click% , a$ , b$
      local message$
      if Pointer% = 1 then
        message$ = "Enter a filename for this video"
      else
        message$ = "Enter a name for this collection of videos"
      endif
      
      proc_showdialog(NAME%)
      proc_setDialogText(NAME%, Static%, message$)
      click% = fn_click(str$(ok%)+","+str$(Quit%))
      
      case true of
        when click% = ok%
          b$ = fn_getEdit(NAME%, Edit%)
          if b$<>"" then
            a$ = DOC$+"\"+b$+".html"
          else
            a$ = DOC$+"\default.html"
          endif
        otherwise
          a$ = ""
      endcase
      proc_closedialog(NAME%)
      = a$
       


I cannot recreate the error state that he has reported. Any ideas?
Re: File exists
Post by MrStreet on Jun 7th, 2011, 5:01pm

I should mention that the beta version of this program is available from http://superdecade.blogspot.com/p/you-tube-cleaner.html.
Re: File exists
Post by admin on Jun 7th, 2011, 6:10pm

on Jun 7th, 2011, 4:35pm, MrStreet wrote:
I cannot recreate the error state that he has reported. Any ideas?

Is your colleague trying to run the program under Wine rather than 'genuine' Windows? There is a known bug in Wine that can result in the 'File exists' error during program initialisation.

Richard.
Re: File exists
Post by MrStreet on Jun 7th, 2011, 6:27pm

Quote:
Is your colleague trying to run the program under Wine


No. Windows 7.
Re: File exists
Post by admin on Jun 7th, 2011, 8:28pm

on Jun 7th, 2011, 4:35pm, MrStreet wrote:
My colleague has reported that my program generates the "File exists" error the second time they loaded it.

Is there a clue in it being the second time they loaded it? What is different between the first and second times? What do they have to do to re-establish the initial conditions, so that it runs without error again (if only once)?

Is your colleague running the 64-bit version of Windows 7 or the 32-bit version, and which version are you running? I only have the 32-bit version here so I cannot test your program under Win64.

If you haven't already done so, I would suggest you ask for a screen 'snapshot' of the error message (e.g. created using Alt-PrintScreen) so you can see the context in which it occurs, and confirm that it really is being generated by your code.

Quote:
I cannot recreate the error state that he has reported.

It's always difficult when you can't reproduce the problem yourself. You might want to create a special 'debug' version of your program which reports, for example, the line which is causing the error.

Code:
*| docs -from  http://bb4w.wikispaces.com/Checking+whether+a+directory+exists 

Please be aware that the *| style of commenting does not get stripped out by the compiler, so makes your final EXE larger and slower than it needs to be:

http://bb4w.wikispaces.com/Alternative+comment+styles

Richard.
Re: File exists
Post by MrStreet on Jun 7th, 2011, 9:12pm

Quote:
What is different between the first and second times?


The decision is made in this procedure...

Code:
      defproc_checkSetup
      rem checks that the user has a folder in my documents
      DOC$ = fn_specialFolder(5)
      if fn_folderExists( DOC$+"\My_"+GAME_NAME$+"_Projects" ) then
        rem do nothing
      else
        rem make projects folder
        oscli "MKDIR """+DOC$+"\My_"+GAME_NAME$+"_Projects"+""""
        rem inform user
        proc_OK("It looks like you are running "+GAME_NAME$+" for the first time on this computer. I have created"\
        \+" a new folder:"+LF$+LF$+DOC$+"\My_"+GAME_NAME$+"_Projects"+LF$+LF$+"I will save your embedded video files there.")
      endif
      endproc
 


Quote:
Is your colleague running the 64-bit version of Windows 7 or the 32-bit version, and which version are you running?


I am running 64. I think he is using 32 (but could be wrong). He is running on a network drive, mine is a stand-alone C: drive.

Quote:
You might want to create a special 'debug' version of your program


Yes, have been building it with on error local commands in each procedure. Will test it out tomorrow on the networked machines and see what's what. Thanks again for your kind help as ever.




Re: File exists
Post by admin on Jun 7th, 2011, 10:14pm

on Jun 7th, 2011, 9:12pm, MrStreet wrote:
Code:
oscli "MKDIR """+DOC$+"\My_"+GAME_NAME$+"_Projects"+"""" 

*MKDIR will result in the 'File exists' error if the directory already exists, so given the symptom, and the fact that it happens only on the second run, it suggests to me that FN_folderExists is returning FALSE even though the directory does exist. You might want to check whether GAME_NAME$ contains any illegal characters (i.e. characters that are not allowed in a directory name) because that could cause the observed effect.

Incidentally DOC$ in your code would appear to be identical to the built-in 'system' variable @usr$.

Richard.
Re: File exists
Post by MrStreet on Jun 8th, 2011, 4:32pm

Quote:
...check whether GAME_NAME$ contains any illegal characters...


This shouldn't create an illegal folder name, should it?

Code:
 GAME_NAME$ = "You Tube Video Cleaner" 


I haven't had chance to try the updated (debug) version on the networked machines yet, but deleting the folder created by PROC_checkSetup (my documents\My_You Tube Video Cleaner_Projects) fixes the problem (until you run it again). The program stills works as expected on my stand-alone machine.

I am very confused. The ability to create a folder when the program first runs, and then using it to save files to would be very handy, although I could create a work-around.

Re: File exists
Post by admin on Jun 8th, 2011, 5:39pm

on Jun 8th, 2011, 4:32pm, MrStreet wrote:
The ability to create a folder when the program first runs, and then using it to save files to would be very handy

You can definitely do that (at the risk of repeating myself, you can do virtually anything that is possible in Windows, since BBC BASIC for Windows is a general purpose programming language).

Even if you cannot get your existing code to work there are straightforward workarounds such as creating the folder using SYS "CreateDirectory", which doesn't fail if it already exists.

But ideally you should find out why the current code isn't working, since that might point to a more fundamental issue with your program.

Quote:
I haven't had chance to try the updated (debug) version on the networked machines yet

Networked machines? Have you mentioned that before? If the folder you're testing for is on a network server running an 'unusual' filing system (e.g. SAMBA), then maybe relying on the file NUL existing in every directory isn't valid.

Richard.
Re: File exists
Post by MrStreet on Jun 8th, 2011, 5:42pm

Can't I create the NUL file using the usual OPENOUT and put it into the projects folder when the program creates it the first time?
Re: File exists
Post by admin on Jun 8th, 2011, 8:17pm

on Jun 8th, 2011, 5:42pm, MrStreet wrote:
Can't I create the NUL file using the usual OPENOUT and put it into the projects folder when the program creates it the first time?

You can't create a NUL file - it's a forbidden file name. But since you are wanting to test for the existence of a directory which you have created yourself, why not simply store a file there specifically for the purpose? Because you know that file will always be there, you can simply test for it being present - you don't actually need to test for the existence of the directory itself!

Richard.
Re: File exists
Post by MrStreet on Jun 8th, 2011, 8:19pm

Many thanks again, Richard.
Re: File exists
Post by MrStreet on Jun 11th, 2011, 11:24am

I think I have fixed the problem using an on error local statement call to :-

Code:
      
      deffn_setupError
      if err=196 then
        rem  file exists error
        = false
      else
        if fn_FAULT_M(" I am sorry the following error occured during setup: "+LF$+LF$+report$+LF$+LF$+" Do you want to quit?") then
          = true
        else
          =false
        endif
      endif