Qbasicnews.com
November 14, 2019, 11:17:38 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: User chooses which file to open  (Read 4733 times)
Licentia
Member
*
Posts: 57



« on: July 03, 2009, 12:20:00 AM »

 Hello!

 (NOTE: If this isn't possible to do please someone post and let me know so I don't wait forever. Thanks so much!)

 Sorry I am sure this should be an easy answer but I am Googling to no avail. Instead of the normal command:

 OPEN "FILE.TXT" FOR INPUT AS #1

 I want to be able to have the user choose which file to open. I think this is possible and I have probably done it in the past, but I haven't done much programming for several years now. The consequence is I have forgotten how.

 Any help would be appreciated!

 Licentia
« Last Edit: July 03, 2009, 01:42:24 AM by Licentia » Logged
Clippy
Member
*
Posts: 84



« Reply #1 on: July 03, 2009, 12:54:36 AM »

Sure just use a string variable like FileName$ in the open statement instead of a literal value.

Create a menu display for the user and use SELECT CASE:

Code:
DO: choice$ = INKEY$ : LOOP UNTIL choice$ <> ""

SELECT CASE choice$
         CASE "1": Filename$ = "ABCD.txt"
         CASE "2": Filename$ = "EFGH.txt"
END SELECT

Then open the file: OPEN Filename$ FOR INPUT AS #1

Logged

Licentia
Member
*
Posts: 57



« Reply #2 on: July 03, 2009, 01:20:53 AM »

 Thanks for your speedy response!

 Sorry that I am not more clear about what I am trying to do. I don't want the user to choose from a list of options. I want the user to select any file they want as long as it's in the same file folder on the computer as the program is located.

 You know, I can compile the program and put it in any folder on my computer (run it in DosBox on Ubuntu Linux), and then put a text file in that same folder and the program will open that text file. This way people can name the text file whatever they want.

 PS: I found this bit of code online, and I don't like it:

Code:
INPUT "CHOOSE THE FILE YOU NEED", FILE$
Rem      CLS
         FILE$ = "c:\data\" + FILE$ + ".PRF"
         Open FILE$ For Input As #3

 I hope it's not saying that I need to pre-select a location for any file that I input as this isn't going to happen in Ubuntu. Unless DosBox somehow assigns a default location?

 Is there not a simple way to have QB just check for files in the same location that it is in without having to specify?

 I tried this code here but it won't work:

Code:
DO
 DO

  PRINT " Please enter the name of the file you are working with."
  PRINT ""
  INPUT FILE.CHOICE$
  PRINT ""
  PRINT " Did you type the file name correctly? Choose 'Y' or 'N'."
  PRINT ""
  INPUT OPTION.3$

 LOOP UNTIL OPTION.3$ = "Y" OR OPTION.3$ = "y" OR OPTION.3$ = "N" OR OPTION.3$ = "n"

 PRINT ""
 PRINT " If the file name is correct, type 'correct'."
 PRINT ""
 INPUT OPTION.3$

LOOP UNTIL OPTION.3$ = "CORRECT" OR OPTION.3$ = "Correct" OR OPTION.3$ = "correct"

  OPEN FILE.CHOICE$ FOR INPUT AS #1

 If it's not possible let me know so I can get on with my life!

 Thanks!

 Licentia
« Last Edit: July 03, 2009, 01:30:35 AM by Licentia » Logged
Clippy
Member
*
Posts: 84



« Reply #3 on: July 03, 2009, 03:29:25 PM »

FILES "*.BAS" would work, but it may scroll too fast to read all of the names.

The following pipes folder file information in DOS to a file. Then you can print them page by page. If you added a mouse and place the names into an array the user can click on them as each page is formatted the same way:

Code:
SCREEN 12 '80 X 30 text

SHELL "DIR *.BAS /B > BASLIST.DIR"  '/B = pipe without file information. Filenames only!

OPEN "BASLIST.DIR" FOR INPUT AS #1
COLOR 11: LOCATE 3, 2
 IF LOF(1) THEN
  Fcount = 0 'number of files found
  DO WHILE NOT EOF(1)
    Fcount = Fcount + 1
    LINE INPUT #1, FileN$
    PRINT FileN$; SPACE$(13 - LEN(FileN$));      'max DOS filename is 12 characters
    IF Fcount MOD 6 = 0 THEN PRINT : LOCATE , 2  'new line
    IF Fcount MOD 156 = 0 THEN                   'next screen
      COLOR 14: LOCATE 29, 25: PRINT "Press any key to continue!";
      DO: SLEEP: LOOP UNTIL INKEY$ <> ""
      CLS : COLOR 11: LOCATE 3, 2
    END IF
  LOOP
  CLOSE #1
 ELSE : COLOR 12: LOCATE 15, 20: PRINT "NO Files Found!"
 END IF
 PRINT " ";
 COLOR 10: LOCATE , 2: PRINT SPACE$(30); "Total Files ="; Fcount;
SYSTEM

The file is updated each run. Then you can check the files to see if they are empty with LOF() before trying to INPUT #. Reduces INPUT # errors.
« Last Edit: July 03, 2009, 03:32:01 PM by Clippy » Logged

Licentia
Member
*
Posts: 57



« Reply #4 on: July 04, 2009, 03:27:04 AM »

 So it's not an easy fix.

 Thanks so much for your help!

 Licentia
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!