From QB64 Wiki
Revision as of 18:12, 22 September 2009 by imported>Clippy
Jump to navigation Jump to search

The SHELL statement allows a program to use OS commands.

Syntax: SHELL DOSCommand$

  • Commands are external DOS commands as STRINGs enclosed in quotes or string variables.
  • Commands can be a mixture of STRINGs and string variables added together using the + concatenation operator.
  • Command text can be in upper or lower case. Use proper spacing between items.
  • For a list of DOS commands see: MSDOS

Example 1: Opening a Windows program(Notepad) to read or print a Basic created text file.

SHELL "cmd /c start notepad " + filename$ 'display in a window in XP or NT
SHELL "start /min notepad /p " + filename$ 'taskbar print on Win 9X machines

Explanation: Notepad is an easy program to open in Windows. No path is needed! Windows NT computers, including XP, use CMD /C where older versions of DOS don't require any command reference. The top command opens Notepad in a normal window for a user to view the file. They can use Notepad to print it. The second command places Notepad file in the taskbar and prints it automatically. The filename variable is added by the program using proper spacing.

  • Start is used to allow a Basic program to run without waiting for Notepad to be closed.
  • /min places the window into the taskbar. /max is fullscreen and no option is a normal window.
  • Notepad's /p option prints the file contents. Even with USB printers!
Note: A fullscreen SCREEN mode must be changed after a Windows program is opened!
Besides minimizing a Basic program, fullscreen modes will lose the current screen information. Just switch to another screen mode and back to the one you were in. You will have to redo the screen too. Screen 0 windows work OK.

Example 2: Function that returns the current program working path.

currentpath$ = Path$ ' function usage saves a path for later program use
FUNCTION Path$ ' assign to a variable for later use in other folders!
SHELL "DIR *.BAS > DOS-DATA.INF" ' pipe all file & dir info into a text file
DO WHILE NOT EOF(1) ' just in case file is empty
LINE INPUT #1, line$ ' read each line of file
location = INSTR(1, line$, ":\") ' find the drive path notation
IF location THEN EXIT DO ' drive path notation found
IF location THEN
line$ = MID$(line$, location - 1, LEN(line$) - location + 2)
Path$ = RTRIM$(line$) + "\" ' a useable directory path
ELSE : Path = "" ' returns zero length string if path not found

Explanation: The SHELL statement requests a directory list of all .BAS files in the current working path. This info is normally printed to the screen, but the > pipe character sends the information to the DOS-DATA.INF file instead. The current path is listed near the beginning of the file. The file is opened and LINE INPUT returns each line of the file text. INSTR looks for the :\ after the drive letter and returns that position in the line of text. Then we can use MID$ to grab that portion of the string. But first we need the current drive too so 1 is subtracted from the location to include that and MID$ returns the rest of the line. RTRIM$ removes any end spaces and adds a \ so that the Path$ returned can be used in another file or SHELL statement with a file name added.



Go to Keyword Reference - Alphabetical

Go to Keyword Reference - By usage