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.
Example: 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
- OPEN "DOS-DATA.INF" FOR INPUT AS #1
- DO WHILE NOT EOF(1) ' just in case file is empty
- CLOSE #1
- IF location THEN
- ELSE : Path = "" ' returns zero length string if path not found
- END IF
- END FUNCTION
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. RTIM$ 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.
- For a list of DOS commands see: MSDOS