Difference between revisions of "SHELL"

From QB64 Wiki
Jump to navigation Jump to search
Line 21: Line 21:
''Example 1:'' When working with file or folder names with spaces, add quotation marks around the path or name with [[CHR$]](34).
''Example 1:'' When working with file or folder names with spaces, add quotation marks around the path and/or file name with [[CHR$]](34).
{{CodeStart}} '' ''
{{CodeStart}} '' ''
{{Cl|SHELL}} {{Cl|_HIDE}} "dir " + {{Cl|CHR$}}(34) + "free cell.ico" + {{Cl|CHR$}}(34) + " /b > temp.dir" '' ''  
{{Cl|SHELL}} {{Cl|_HIDE}} "dir " + {{Cl|CHR$}}(34) + "free cell.ico" + {{Cl|CHR$}}(34) + " /b > temp.dir" '' ''  

Revision as of 17:19, 9 September 2011

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 screen info can be "piped" to a file (recommended).
  • 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.
  • QB64 automatically uses CMD /C or COMMAND /C when using SHELL.
  • QB64 program screens will not get distorted, minimized or freeze the program like Qbasic fullscreen modes can.
  • For a list of DOS commands see: MSDOS
  • NOTE: Use CHDIR instead of CD to change directories until a QB64 SHELL can do that!
  • NOTE: QB64 will not display DIR lists or other DOS screen info! Send output to a temp file to get the information.

Example 1: When working with file or folder names with spaces, add quotation marks around the path and/or file name with CHR$(34).

SHELL _HIDE "dir " + CHR$(34) + "free cell.ico" + CHR$(34) + " /b > temp.dir" SHELL "start Notepad temp.dir" ' display temp file contents in Notepad window

Contents of temp.dir text file:

free cell.ico

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

INPUT "Enter a file name to read in Notepad: ", filename$ SHELL "CMD /C start /max notepad " + filename$ ' display in Notepad full screen in XP or NT 'SHELL "start /min notepad /p " + filename$ ' taskbar print using QB64 CMD /C not necessary

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 QBasic program, fullscreen modes will lose the current screen information and freeze. Switch to another screen mode and back to the one you were in immediately after the SHELL! Screen 0 windows will work OK.

Example 3: Function that returns the program's current working path.

currentpath$ = Path$ ' function call saves a path for later program use PRINT currentpath$ FUNCTION Path$ SHELL _HIDE "CD > D0S-DATA.INF" 'code to hide window in QB64 OPEN "D0S-DATA.INF" FOR APPEND AS #1 'this may create the file L% = LOF(1) 'verify that file and data exist CLOSE #1 IF L% THEN 'read file if it has data OPEN "D0S-DATA.INF" FOR INPUT AS #1 LINE INPUT #1, line$ 'read only line in 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 "D0S-DATA.INF" 'deleting the file is optional END FUNCTION

Code by Ted Weissgerber
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 can use _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 just adding a file name. Save the Path$ to another variable for later use when the program has moved to another directory.

Example 4: Determining if a drive or path exists. Cannot use with a file name specification!

LINE INPUT "Enter a drive or path (no file name): ", DirPath$ IF PathExist%(DirPath$) THEN PRINT "Drive Path exists!" ELSE PRINT "Drive Path does not exist!" END FUNCTION PathExist% (Path$) PathExist% = 0 IF LEN(Path$) = 0 THEN EXIT FUNCTION 'no entry IF LEN(ENVIRON$("OS")) THEN CMD$ = "CMD /C " ELSE CMD$ = "COMMAND /C " SHELL _HIDE CMD$ + "If Exist " + Path$ + "\nul echo yes > D0S-DATA.INF" OPEN "D0S-DATA.INF" FOR APPEND AS #1 IF LOF(1) THEN PathExist% = -1 'yes will be in file if path exists CLOSE #1 KILL "D0S-DATA.INF" 'delete data file optional END FUNCTION

Code by Ted Weissgerber
Explanation: IF Exist checks for the drive path. \Nul allows an emply folder at end of path. Echo prints yes in the file if it exists.

See example: FILELIST$ (function) (member file search routine)

See Library: File Exist C++ Function that does not create a temp file. FileExist Library Function

See also:

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