FB Command Prompt example using dir, cudir, chdir, and exec functions

Post your FreeBASIC tips and tricks here. Please don’t post your code without including an explanation.
Posts: 136
Joined: Dec 25, 2008 20:21

FB Command Prompt example using dir, cudir, chdir, and exec functions

Postby ShawnLG » Aug 30, 2019 21:19

I didn't know until today that freebasic has the functionality to parse files and folders on the hard disk. Never know what you would find when you RTM. Code below is a very basic command prompt program example demonstrating this. You can search files through your hard drive and execute exes! Add new commands or make a Linux console version. I hope someone gets something out of this.

Code: Select all

'Basic command prompt program
'This demonstrates the use of DIR, CURDIR, CHDIR and EXEC fb commands.
'Other file system command not covered here: MKDIR, RMDIR, KILL, FILEDATETIME, FILEEXISTS, and more buried deeply in the FB's documentation.
'Linux or PC switch by counting pine.
#IFDEF __FB_PCOS__ 'for windows or DOS
  #define SLASH "\" '' backslash
#ELSE 'for linux
  #define SLASH "/" '' forward-slash
#include "dir.bi" '' provides constants to match the attributes against
declare sub listdir()
declare sub execute(cmd as string)

'' set input attribute mask to allow files that are normal, hidden, system or directory
Const attrib_mask = fbNormal Or fbHidden Or fbSystem Or fbDirectory ' = &h37

dim as string cmd

    print curdir & ">"; 'print current directory path with arrow prompt.
    input "", cmd 'get user's command
    select case cmd
    case "dir"
    case "cls"
    case "cd .."
        chdir ".." + SLASH + ".."
    case "cd.."
        chdir ".." + SLASH + ".."
    case "exit"
        exit do
    case "quit"
        exit do
    end select
    if left(cmd,3) = "cd " then 'change dir to sibling
        chdir curdir + right(cmd,len(cmd)-3)
    end if
    'all else, assume program execution command
    execute(cmd)'execute program name. .EXE must be included in string. Parameters are allowed

sub listdir()
    dim as string fname' file/directory name returned with
    dim as integer filecount, dircount
    Dim As UInteger out_attr '' unsigned integer to hold retrieved attributes

    fname = Dir("*.*", attrib_mask, out_attr) '' Get first file name/attributes, according to supplied file spec and attribute mask

    Do Until Len(fname) = 0 '' loop until Dir returns empty string

        Print fname,

        If (out_attr And fbDirectory) <> 0 Then
            Print "- directory";
            dircount += 1
            Print "- file";
            filecount += 1
        End If
        If (out_attr And fbReadOnly) <> 0 Then Print ", read-only";
        If (out_attr And fbHidden) <> 0 Then Print ", hidden";
        If (out_attr And fbSystem) <> 0 Then Print ", system";
        If (out_attr And fbArchive) <> 0 Then Print ", archived";
        fname = Dir(out_attr) '' find next name/attributes
    Print " "& filecount & " File(s)"
    print " "& dircount & " Dir(s)"
end sub

sub execute(cmd as string)
    dim as string exename, parameters
    dim as integer paramloc
    paramloc = instr(cmd," ")
    if paramloc = 0 then
        exename = cmd
        parameters = ""
        exename = left(cmd,paramloc)
        parameters = right(cmd,len(cmd)-paramloc)
    end if
    exec(exename, parameters)
end sub

Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 3 guests