From QB64 Wiki
Revision as of 22:24, 3 July 2010 by imported>Clippy
Jump to navigation Jump to search

The SHELL statement allows a program to use OS command lines. In QB64 this would include Windows and Linux.

QB Syntax: SHELL [DOSCommand$]
QB64 Syntax: SHELL [_DONTWAIT] [_HIDE] [DOSCommand$]

  • If the DOSCommand$ STRING parameter isn't used the "console" is opened.
  • If _DONTWAIT is used the QB64 program doesn't wait for the SHELLed program/command to end.
  • When the _HIDE action is used, the command window is hidden and info can be "piped" to a file.
  • 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 single spacing between items and options.
  • 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 in Qbasic ONLY!
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 call saves a path for later program use FUNCTION Path$ SHELL "CD > DOS-DATA.INF" ' pipe current path info into a text file ' SHELL _HIDE "CD > DOS-DATA.INF" 'alternate code to hide window in QB64 OPEN "DOS-DATA.INF" FOR APPEND AS #1 'this may create the file L% = LOF(1) 'verify that file and data exist CLOSE #1 OPEN "DOS-DATA.INF" FOR INPUT AS #1 IF L% THEN 'read file if it has data LINE INPUT #1, line$ ' read one line of file Path$ = line$ + "\" 'add slash to path so only a filename needs added later CLOSE #1 ELSE : Path = "" 'returns zero length string if path not found END IF KILL "DOS-DATA.INF END FUNCTION

Explanation: The SHELL "CD" statement requests 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(QB64 needs _HIDE to not display the DOS window. The function uses the OPEN FOR APPEND mode to check for the file and the data(INPUT would create an error if file does not exist). The current path is listed on one line of the file. The file is opened and LINE INPUT returns one line of the file text. The function adds a "\" so that the Path$ returned can be used in another file statement by adding a file name.


Keyword Reference - Alphabetical
Keyword Reference - By Usage
Main Wiki Page