How to mix Shell commands with variables?

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
Sinuvoid
Veteran
Posts: 155
Joined: Wed Jul 25, 2007 8:20 am

How to mix Shell commands with variables?

Post by Sinuvoid »

OK im trying to get my program to look for strings in a text file and print them, but how do i get the user to input information? ill show you the pat im having trouble with

Code: Select all

CLS

LOCATE 1,1
INPUT "Actor name, or part of one:", sactname$
if sactname$ = actname$ then 
SHELL "cd C:\ZSOACTRDAT"
SHELL "FIND /I ",sactname$ "Actordat.txt
sleep 10
INPUT #1, actname$, groupnum$, actornum$, pactnum$
CLOSE #1
Please reply ASAP and thanks very much!
Nodtveidt
Veteran
Posts: 826
Joined: Sun Jul 25, 2004 4:24 am
Location: Quebradillas, PR
Contact:

Post by Nodtveidt »

Not quite sure what you're doing there, but a couple of suggestions:

Code: Select all

SHELL "cd C:\ZSOACTRDAT"
can be rewritten as

Code: Select all

CHDIR "C:\ZSOACTRDAT"
and

Code: Select all

SHELL "FIND /I ",sactname$ "Actordat.txt
doesn't even look like it will work. This might work:

Code: Select all

SHELL "FIND /I " + sactname$ + " Actordat.txt"
Never used FIND so I can't help you there, but if all you're doing is searching a text file, you would probably want to read the file manually using INPUT # or LINE INPUT #. One final suggestion as well is to use dynamic file handles rather than absolute file handles...instead of using INPUT #1, you should create an integer variable and use it with FREEFILE to get a dynamic file handle:

Code: Select all

DIM FH AS INTEGER
FH = FREEFILE
OPEN "MYFILE.TXT" FOR INPUT AS #FH
etc.
Sinuvoid
Veteran
Posts: 155
Joined: Wed Jul 25, 2007 8:20 am

Post by Sinuvoid »

Im trying to search for strings in the text file. But I need input from the user to see what s\he wants to search.

Tried it and didnt work with the +'s, doesnt make anything show up and even used it in a do loop and saw nothing.
Nodtveidt
Veteran
Posts: 826
Joined: Sun Jul 25, 2004 4:24 am
Location: Quebradillas, PR
Contact:

Post by Nodtveidt »

If you're searching for strings in a text file where the format is well-known, skip using an external program altogether, as that's just plain ludicrous. You're much better off just opening the file for INPUT and doing string $$$ manually.

This line:

Code: Select all

INPUT "Actor name, or part of one:", sactname$
would be better off as this:

Code: Select all

INPUT "Actor name, or part of one:"; sactname$
but that's a minor detail. I think your method is correct there regardless. But why are you comparing sactname$ to actname$? Hopefully there's more to your program than that, and if so, show it so more help can be given. As it is, the program you posted will do absolutely nothing.
Sinuvoid
Veteran
Posts: 155
Joined: Wed Jul 25, 2007 8:20 am

Post by Sinuvoid »

Ill post the whole program right now, but ive tried INPUT #1, blah$ etc but dont know what to do after that....

Code: Select all

'Actor database of ZSO
CLS

Wbackround:
for i = 1 to 300
Color 0,15
Locate 15,20
PRint "Loading, Please wait."
for b = 1 to 30000
next b
CLS
Locate 15,20
PRint "Loading, Please wait.."
for c = 1 to 30000
next c
CLS
Locate 15,20
PRint "Loading, Please wait..."
for d = 1 to 30000
next d
CLS
next i

CLS
test:
Color 0,15
Locate 1,1
Print "A production of Souylsin."
Locate 2,1
Print "A find by the community of ZSO."
Locate 3,1
PRINT "ZSO DAT 0.01"
Locate 4,1
Print "Locating Actordat..."

Open "Actordat.txt" for INPUT as #1
INPUT #1, there$
CLOSE #1
if there$ = "0000 0000 Link (may sometimes cause Z-buffer glitches)" then 
goto con
else 
LOCATE 5,1
PRINT "Could not locate Actordat, now exitting..."
sleep 1
end
end if

con:
do
Locate 5,1
PRINT there$
LOCATE 6,1
PRINT LEN(there$)
Locate 7,1 
PRINT "Actordat Located successfully"
LOCATE 8,1 
PRINT "Press C to Continue"
kp$ = INKEY$
if kp$ = "c" then goto confrm
loop

confrm:
CLS
DO
LOCATE 15,20
PRINT "TEST COMPLETE"
kp$ = INKEY$
if kp$ = "d" then goto Menu
LOOP

Menu:
CLS
do
LOCATE 10, 20
PRINT "Database of Zelda Actors"
LOCATE 20, 5
PRINT "(S)earch for Actors"
LOCATE 20, 20
PRINT "(E)xit"

kp$ = INKEY$
if kp$ = "s" then goto srcaktr
if kp$ = "e" then goto ext

loop

srcaktr:
CLS

LOCATE 1,1
INPUT "Actor name, or part of one:", sactname$
if sactname$ = actname$ then 
SHELL "cd C:\ZSOACTRDAT"
SHELL "FIND /I " + sactname$ + "Actordat.txt
sleep 10
INPUT #1, actname$, groupnum$, actornum$, pactnum$
CLOSE #1

ext:
end
Mac
Veteran
Posts: 151
Joined: Mon Aug 06, 2007 2:00 pm

Post by Mac »

Lee wrote:Ill post the whole program right now
OPEN "Actordat.txt" FOR INPUT AS #1
- If the file is not there, this instruction will fail to run
- It will fail anyway because filenames are limited to 8.3
- Try "Actor.txt"

if kp$ = "s" then goto srcaktr
if kp$ = "e" then goto ext
loop
- Why are these in lower case? Have you even tried the program?
- Please copy code from running programs

if sactname$ = actname$ then
- Since actname$ has no value, this IF statement will never work

SHELL "cd C:\ZSOACTRDAT"
- This will fail due to long file name.
- Rename the directory and try "cd C:\ZSOACTRS"

Your FIND command is hopeless. Try this
cmd$ = "FIND /I " + CHR$(34) + sactname$ + CHR$(34) + " Actordat.txt"
PRINT cmd$
SHELL cmd$

By printing the cmd$, you can see what you are asking DOS to do.

Finally, what is the SLEEP 10 for? What does any of this have to do with the INPUT #1 command that follows? Do you think that because the FIND program found sactname$ that your program is set to read that record? DOS doesn't return file position to your program, even if you give it 10 seconds.

My guess is that you are a beginner programmer who is trying much too tough an initial project. Stick to simpler programs for a few months until your skills improve. It is very difficult to write a database program. Below is some working code that abandons the SHELL concept, as suggested by Nodtveidt. It will work assuming you have the file and it contains stuff like
"John Smith", "Group1","34343","12"
"Mary Jones", "Group2","22","22"
"John Brown", "Group3","3437","43"

As you can see, it needs still more work: What happens if I enter "John" and wanted "John Brown"? And if I enter "M" I will get "John Smith", not
"Mary Jones" because of the "m" in smith. And what would I do with the data? Just look at it? The list goes on. Database programs are tough.

Mac

Code: Select all

CONST ActorFile = "C:\ZSOACTRS\Actordat.txt"
DIM SHARED MyErr AS INTEGER
MyErr = 0: ON ERROR GOTO GetMyErr
OPEN ActorFile FOR INPUT AS #1
ON ERROR GOTO 0
IF MyErr > 0 THEN PRINT "Unable to find "; ActorFile: SYSTEM
' Test format
DO WHILE NOT EOF(1)
  FOR i = 1 TO 4
    IF EOF(1) THEN
      PRINT "Format error in "; ActorFile
      SYSTEM
    ELSE
      INPUT #1, l$
    END IF
  NEXT i
LOOP
CLOSE #1
' OK, there is a good file
CLS
DO
  PRINT : LINE INPUT "Actor name, or part of one: ", sactname$
  IF sactname$ = "" THEN EXIT DO
  GotIt = 0
  OPEN ActorFile FOR INPUT AS #1
  DO WHILE NOT EOF(1)
    INPUT #1, actname$
    IF INSTR(UCASE$(actname$), UCASE$(sactname$)) > 0 THEN GotIt = -1: EXIT DO
    FOR i = 1 TO 3: INPUT #1, l$: NEXT i
  LOOP
  IF GotIt THEN
    INPUT #1, groupnum$, actornum$, pactnum$
    PRINT "Found "; actname$; " "; groupnum$; " "; actornum$; " "; pactnum$
  ELSE
    PRINT "Unable to find "; sactname$
  END IF
  CLOSE #1
LOOP
PRINT "End of Run"
SYSTEM
GetMyErr: MyErr = ERR: RESUME NEXT
Sinuvoid
Veteran
Posts: 155
Joined: Wed Jul 25, 2007 8:20 am

Post by Sinuvoid »

Ive tried to do a database before, a much simplier one but didnt know how to find strings, and what is CHR$(34) and CHR$(35), are tehey those spiecal characters (e.g. arrow keys etc?...) but I think that one line of code you gave me will do it =)

Code: Select all

cmd$ = "FIND /I " + CHR$(34) + sactname$ + CHR$(34) + " Actordat.txt" 
I meant to put Sleep 1, jsut for testing and seening if it worked, when im done testing im going to take it out =)

Yeah the IF statment will be useless

THIS DOES WORK

Code: Select all

SHELL "cd C:\ZSOACTRDAT" 
same with my Actordat.txt, it also works.
Below is some working code that abandons the SHELL concept, as suggested by Nodtveidt. It will work assuming you have the file and it contains stuff like
"John Smith", "Group1","34343","12"
"Mary Jones", "Group2","22","22"
"John Brown", "Group3","3437","43"
you mean the
Open "blah.txt" for APPEND as #1
WRITE #1, name$, number%, etc$
CLOSE #1

Open "blah.txt" for INPUT as #1
INPUT #1, variables (name$ etc)
CLOSE #1

like so?

but what im trying to do is search for none variable strings, which cannot be done in QB without teh SHELL command.

Anyways thanks for your help!!
Ill tst teh line of code you gave me RIGHT now =)
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Those are Quotes

Post by burger2227 »

In certain DOS commands, you need Quotation marks in the command. CHR$(34) is simply a ". However, QB would think that was a string marker if you just used that. The character is preserved in the string command for use by DOS only.

Look up the ASCII character codes somewhere. They are an important part of coding and formatting text in QB.

Ted
Mac
Veteran
Posts: 151
Joined: Mon Aug 06, 2007 2:00 pm

Post by Mac »

Lee wrote:what im trying to do is search for none variable strings, which cannot be done in QB without teh SHELL command.
Don't know what you mean by that. But when you get tired messing around, try this:
a) Create C:\ZSOACTRS
b) Go there in DOS and do this
NOTEPAD Actor.dat
and paste this:

Code: Select all

"John Smith", "Group1","34343","12"
"Mary Jones", "Group2","22","22"
"John Brown", "Group3","3437","43"
Then put my program somewhere and run it. Note it works perfectly.

Mac
Sinuvoid
Veteran
Posts: 155
Joined: Wed Jul 25, 2007 8:20 am

Post by Sinuvoid »

Thanks, I got it working anyways with the line of code you gave me =)
cmd$ = "FIND /I " + CHR$(34) + sactname$ + CHR$(34) + " Actordat.txt"
That was what I was EXACTLY looking for =)
Now read my next post and help me out please XD
Post Reply