Command Line, Environment & System Access

SYSTEM n ends the program with exit code n (QB64), useful for scripts.

31.1 Command-line arguments

SYSTEM n ends the program with exit code n (QB64), useful for scripts.

$CONSOLE:ONLY
' run as: wordcount notes.txt -v
PRINT "Full command line: "; COMMAND$
PRINT "Argument count:"; _COMMANDCOUNT
FOR i = 1 TO _COMMANDCOUNT
    PRINT i; COMMAND$(i)
NEXT
IF _COMMANDCOUNT = 0 THEN PRINT "Usage: wordcount <file> [-v]": SYSTEM 1   ' exit code 1

31.2 Environment and shell

Security note: never build SHELL commands from untrusted input — quote file names and validate them.

PRINT ENVIRON$("PATH")                    ' environment variable
PRINT _OS$                                ' e.g. "[WINDOWS][64BIT]" or "[LINUX][64BIT]"
SHELL "dir"                               ' run a command and wait (use "ls" on Linux/macOS)
SHELL _HIDE "ping -n 1 localhost > ping.txt"  ' hide the console window (Windows)
SHELL _DONTWAIT "notepad"                 ' start without waiting
PRINT _CLIPBOARD$                         ' read the clipboard
_CLIPBOARD$ = "Copied from QB64"          ' write the clipboard
PRINT "Started in: "; _STARTDIR$

31.3 Detecting window close

Once your program calls _EXIT, QB64 stops closing the window automatically, so you can save data first.

FreeBASIC: Command(n), Environ("PATH"), Shell, Exec, Run, Chain, End n for exit codes, Now/Format via vbcompat.bi; networking via sockets libraries. VB.NET: Sub Main(args As String()) or Environment.GetCommandLineArgs(), Environment.GetEnvironmentVariable, Process.Start, Environment.Exit(code), DateTime.Now, HttpClient for HTTP(S).

DO
    IF _EXIT THEN                         ' user clicked the close button
        PRINT "Saving before exit..."
        ' SaveGame
        SYSTEM
    END IF
    _LIMIT 30
LOOP