Qbasicnews.com

Full Version: filename/path input vs COMMAND$
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I've noticed some of the code posted here contains explicit path information, or else files need to be loaded in a particular directory to be accessable when the program is run.

1 pretty good way to prevent paths from needing to be hard-wired while allowing the user to hunt for their files is with code like this:

Code:
CASE "3"
  PRINT
DO
  SHELL "cd"   'to print the 'pwd'
  PRINT "Enter DOS command or <Q>uit "
  INPUT DOS$
  IF LCASE$(DOS$) = "q" THEN EXIT DO
  COLOR 15
  SHELL DOS$
LOOP
where 3 was menu item that said something like "change directory".
Once the path is identified, the user can type his filename using a different menuitem.

However...this is still clunky. When I write a program that I want to open a file, I find it easier to simply use the COMMAND$, then compile the program before using it. That way, you can simply drag-n-drop the file from windows onto the executable to launch the program with the dropped file as the target. This makes it *much* easier to use.

The code looks something like this:



Code:
CALL commandhandler (needfile%)   'checks if COMMAND$ contains data

fullfilein$=COMMAND$
IF needfile%=1 THEN
  CALL filesmenu  (fullfilein$)   'get a name (with option to change path) if command$ contains no data
ENDIF

CALL pathparser (fullfilein$, path$, root$, extension$)

OPEN path$+root$+extension$ FOR BINARY AS 1
OPEN path$+root$+".tmp" FOR BINARY AS 2

(caution...this is not actual code subs not included...just an example)

Note the inclusion of the filehandling routine...this is to make the code usable from within the interpreter...if you don't include this, then the code is only usable in its compiled form.

Just a thought...does anyone else use the drag-n-drop method?
Are there easier ways to get path info?
Thanks

This doesn't work if you have a bunch of data files...in this case, it's easiest to use relative addressing and put the files in a directory where the exe is located...
About path info, there is stuff in the FAQ ::

http://faq.qbasicnews.com/?blast=RelativePaths

check it and learn how to use relative paths.

(heh - in fact I uploaded that stuff a minute ago - LOL)
Quote:About path info, there is stuff in the FAQ ::

http://faq.qbasicnews.com/?blast=RelativePaths

check it and learn how to use relative paths.

(heh - in fact I uploaded that stuff a minute ago - LOL)

Thanks for the useful info...however...I was talking about something different. If you have *no* path info...but use COMMAND$ as the input file (but only for compiled code), it's really convenient. Drop a file onto the EXE and the program launches with the dropped file as input to the EXE...then it doesn't matter which path the file is in...


As an example...compile this code, then drop a file on it...the checksum will be returned...
Code:
'this is powerbasic code adapted from: http://pbsound.basicguru.com/files/pbsource/crc32bas.bas
DEFINT A-Z


p$ = COMMAND$   'this code must be compiled to be useful...

PRINT "calculating CRC for "; p$

DIM CRCTable(256) AS LONG
DIM Buffer AS STRING * 4096

FOR I = 0 TO 255
  CRC32& = I
  FOR J = 1 TO 8
    IF (CRC32& AND 1) THEN
      CRC32& = (CRC32& \ 2) XOR -306674912
    ELSE
      CRC32& = CRC32& \ 2
    END IF
  NEXT J
  CRCTable&(I) = CRC32&
NEXT I


OPEN p$ FOR BINARY AS 1
filelen& = LOF(1)
CRC32& = -1
DO UNTIL EOF(1)
  GET #1, , Buffer$
  FOR a% = 1 TO LEN(Buffer$)
     TEMP1& = CRC32& \ 256
     TEMP2& = CRCTable&((CRC32& XOR ASC(MID$(Buffer$, a%, 1))) AND 255)
     CRC32& = TEMP1& XOR TEMP2&
  NEXT
LOOP

CRC32& = CRC32& XOR -1
CLOSE #1

CLS
PRINT "the file "; p$; " is "; filelen& / 1000; " kb and has a crc of (dec/hex):"
PRINT CRC32&, HEX$(CRC32&)

DO
LOOP UNTIL INKEY$ <> ""