BBC BASIC for Windows
Programming >> Database and Files >> *CHDIR + OPENIN with wildcard = wrong folder
http://bb4w.conforums.com/index.cgi?board=database&action=display&num=1442846487

*CHDIR + OPENIN with wildcard = wrong folder
Post by 5against4 on Sep 21st, 2015, 2:41pm

When i change directory using *CHDIR, and then use OPENIN with a wildcard so that the Open File dialogue box is displayed, the folder shown in the dialogue box is not the one i've just changed to but the original one (i.e. the one in which the program itself is stored).

Am i missing something or is this just the way BBC Basic wants to behave? i need the Open File dialogue box to show me the contents of the folder i've changed to, not the default one.
Re: *CHDIR + OPENIN with wildcard = wrong folder
Post by sveinioslo on Sep 21st, 2015, 5:37pm

Manual info, at pages:
'Setting the File Open dialogue's initial directory'
and
'Using dialogue boxes'


Replace *CHDIR with this:
Code:
      DIM fs{lStructSize%, hwndOwner%, hInstance%, lpstrFilter%, \
      \      lpstrCustomFilter%, nMaxCustFilter%, nFilterIndex%, \
      \      lpstrFile%, nMaxFile%, lpstrFileTitle%, \
      \      nMaxFileTitle%, lpstrInitialDir%, lpstrTitle%, \
      \      flags%, nFileOffset{l&,h&}, nFileExtension{l&,h&}, \
      \      lpstrDefExt%, lCustData%, lpfnHook%, lpTemplateName%}
      DIM fp% 255
      InitialDir$ = "C:\___SOMEWHERE___"+CHR$0 : REM Change here ...................
      FileFilter$ = "*:*"+CHR$0+CHR$0
      fs.lStructSize% = DIM(fs{})
      fs.hwndOwner% = @hwnd%
      fs.lpstrFilter% = !^FileFilter$
      fs.lpstrFile% = fp%
      fs.nMaxFile% = 256
      fs.lpstrInitialDir% = !^InitialDir$
      fs.flags% = 6
 


Replace OPENIN"*" with this:
Code:
      SYS "GetOpenFileName", fs{} TO result%
      IF result% THEN file$=$$fp% : PRINT file$
 


Hope this helps.
Svein


Re: *CHDIR + OPENIN with wildcard = wrong folder
Post by 5against4 on Sep 21st, 2015, 8:38pm

Thanks Svein - it's a shame (and a bit ridiculous) it needs to be as complex as that! But i'm grateful for the help smiley
Re: *CHDIR + OPENIN with wildcard = wrong folder
Post by hellomike on Sep 21st, 2015, 9:45pm

Hi,

Probably I don't understand what you want but why use *CHDIR at all?

Whats wrong with this?
Code:
      SearchDir$="C:\Windows\System32\"

      F%=OPENIN(SearchDir$+"*.xml")
      IF F%=0 ERROR 100,"File not found."
      Line1$=GET$#F%
      CLOSE#F%

      PRINT "This program is stored in: "+@dir$
      PRINT "First line in the chosen XML was: '"+Line1$+"'"
      END
 


Cheers,

Mike
Re: *CHDIR + OPENIN with wildcard = wrong folder
Post by 5against4 on Sep 22nd, 2015, 09:09am

That's quite nice Mike, thanks!

However - is it possible to make the search directory relative rather than absolute? i have a subfolder called DATA and want it to look in there, but nothing seems to tell it to look in there unless i specify the full absolute location.

But otherwise your solution is perfect - thank you! laugh
Re: *CHDIR + OPENIN with wildcard = wrong folder
Post by DDRM on Sep 22nd, 2015, 1:24pm

Hi,

Does @dir$ do what you need? That is set to the directory from which the program was loaded, so if your DATA folder is in there you could refer to it as @dir$+"DATA\*.bbc"

Hope that helps,

D
Re: *CHDIR + OPENIN with wildcard = wrong folder
Post by hellomike on Sep 22nd, 2015, 4:14pm

As far as I know, the OPENIN() function either needs an absolute/full path or, if it doesn't have that, e.g. because you are using it like

F% = OPENIN("*.*")

,it will make the Open File dialogue box relative to the content of what's in the @dir$ variable, NOT to the current working folder.

But no worries. You can easily find out the absolute path for the current working folder at any given time and then append "\DATA\*.*" to it as follows:

Code:
      ......
      REM Get absolute path to working folder.
      DIM cd% 255
      SYS "GetCurrentDirectory", 256, cd%
      PRINT $$cd%

      REM Show Open File dialogue box relative to it...
      F% = OPENIN($$cd%+"\DATA\*.*")
      ...... 


This way you provide OPENIN() with an absolute path.

If you need that more often in the source, you better use the function as documented in the Help for the *CHDIR command.

Then the OPENIN() call would be:

Code:
      F% = OPENIN(FNcurrentdirectory+"\DATA\*.*")
 


Happy coding.

Mike