Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
DAY 033: COMMAND$
#1
Ever wish we could travel the world on a transporter beam? Well, we can't, so for now, let's stay home and play with COMMAND$...

SYNTAX: cmd$ = COMMAND$[(count%)]

USE: Passing arguments to other apps and drag and drop applications.

And as a bonus, if you act now, we'll throw in _COMMANDCOUNT to evaluate that count% variable.

Okay, let's get our demo of the day going...

Works for Windows, untested for Linux or Mac...
Code: (Select All)
WIDTH 120, 25
_SCREENMOVE 0, 0
PRINT
cmd$ = COMMAND$(0) ' Gets the complete path and name of your current running program.
j = _INSTRREV(cmd$, "\")
program$ = MID$(cmd$, j + 1) ' Parse to just the program name.

IF j THEN
    DrivePath$ = MID$(cmd$, 1, LEN(cmd$) - LEN(program$))
    j = 1
ELSE
    DrivePath$ = COMMAND$(1)
    j = 2
END IF

PRINT "Drive and path of program passing arguments... "; DrivePath$: PRINT
PRINT "Program name passing the arguments............ "; program$: PRINT

IF LEN(COMMAND$(1)) THEN
    ' Parse and add back spaces because command$ needs passing spaces enclosed in quotes to preserve them.
    k = _COMMANDCOUNT ' The number of arguments passed.
    FOR i = j TO k
        cmd$ = COMMAND$(i) ' Parse out an argument.
        msg$ = msg$ + cmd$ + " " ' Concatenate string and add space back into string.
    NEXT
    msg$ = MID$(msg$, 1, LEN(msg$) - 1) ' Cut off the trailing space we added.
    COLOR 14: PRINT msg$: PRINT: COLOR 7
END IF

PRINT "[1] Pete is a genius!": PRINT
PRINT "[2] Steve is a genius!": PRINT
PRINT "[3] Pete and Steve are both idiots, and I wore out my [3] key!": PRINT
PRINT
DO
    INPUT "1-3: ", ans%: PRINT
    IF ans% = 0 THEN SYSTEM
    IF ans% > 0 AND ans% < 4 THEN EXIT DO
LOOP

' RUN and pass our sentences as a command$() parameter.
' Note we need a space between each argument we are going to be passing.
format$ = " " + CHR$(34) + _CWD$ + CHR$(34) + "\ "
SELECT CASE ans%
    CASE 1: RUN program$ + format$ + "Pete is a genius!"
    CASE 2: RUN program$ + format$ + "Steve is a genius!"
    CASE 3: RUN program$ + format$ + "Pete and Steve are both idiots, and I wore out my [3] key!"
END SELECT

So the example above is a self-contained program. You don't have to name it or save it, just run it. Well how in the hell is that possible, you ask? Well, let's get into that, pronto...

COMMAND$(0): This is a special parameter of COMMAND$() that retrieves the current drive, path, and name of the running app. Pretty cool, right?

Edit: Steve caught the fact COMMAND$(0) doesn't grab the drive and path of the passing app, it grabs where it is being started from along with the name, of course. (See posts below for more info.) So I adjusted the code above to also include a way to pass the drive and path with _CWD$. Our example only needs the name, but it's nice to know the drive and path, if needed, could be passed as well.

Now that our example program knows its name, it let's you pick a selection then runs itself. It passes the choice you made through COMMAND$() and parses it out in the first conditional block statement... IF LEN(COMMAND$(1)) THEN

The (1) of COMMAND$(1) is string we passed in the RUN statement. So COMMAND$(0) got us or drive, path, and program name, and COMMAND$(1) got us the string argument we passed in the RUN statement.

Note: A difference between QB64 and QuickBASIC is QB made the arguments uppercase. QB64 maintains the case of the passed string.

_COMMANDCOUNT then kicks in and tells us how many parameters. COMMAND$() separates our text message by spaces. In other words a space acts as a string delimiter. So if the message was: "Call me Betty" we would have three arguments: Call, me, Betty. By knowing we have 3, we can save a little coding parsing those arguments out as COMMAND$(1), COMMAND$(2), and COMMAND$(3) by using our FOR/NEXT loop routine.

So even though we posted just one elf-contained program, for ease of use, keep in mind we will primarily use COMMAND$() when RUNning or SHELLing to another application, which needs info passed to it from the previous or calling program.

Note: Other methods of accomplishing for RUN and/or SHELL would be CHAIN, but not used in SHELL statements, Piping, used in SHELL statements, _CLIPBOARD$, usable in both, database sharing, used in both, TCP/IP, usable in both, and SCREEN writing and reading, used in both.

Oh, and here is a fun little shortcut example you can make to view files from Explorer in Notepad...

Instructions: name it anything you want. Make exe only. Find exe and make Desktop shortcut.
Code: (Select All)
cmd$ = COMMAND$(0) ' Gets the complete path and name of your current running program.

IF LEN(COMMAND$(1)) THEN
    ' Parse and add back spaces because command$ needs passing spaces enclosed in quotes to preserve them.
    j = _COMMANDCOUNT ' The number of arguments passed.
    FOR i = 1 TO j
        cmd$ = COMMAND$(i) ' Parse out an argument.
        msg$ = msg$ + cmd$ + " " ' Concatenate string and add space back into string.
    NEXT
    msg$ = MID$(msg$, 1, LEN(msg$) - 1) ' Cut off the trailing space we added.
    COLOR 14: PRINT msg$: PRINT: COLOR 7
    PRINT: PRINT "SHELL notepad " + cmd$
    SHELL _HIDE _DONTWAIT "notepad " + cmd$
END IF

So what now? Open Explorer and find a text file. Drag it into the desktop icon you just created. It will open that dragged file in Notepad.

...And if you want to make it stealth, code it as...
Code: (Select All)
' Make a desktop shortcut to this and drag files into it.
_SCREENHIDE
cmd$ = COMMAND$(0) ' Gets the complete path and name of your current running program.
REM PRINT cmd$: PRINT
IF LEN(COMMAND$(1)) THEN
    ' Parse and add back spaces because command$ needs passing spaces enclosed in quotes to preserve them.
    j = _COMMANDCOUNT ' The number of arguments passed.
    FOR i = 1 TO j
        cmd$ = COMMAND$(i) ' Parse out an argument.
        msg$ = msg$ + cmd$ + " " ' Concatenate string and add space back into string.
    NEXT
    msg$ = MID$(msg$, 1, LEN(msg$) - 1) ' Cut off the trailing space we added.
    REM COLOR 14: PRINT msg$: PRINT: COLOR 7
    REM PRINT: PRINT "SHELL notepad " + cmd$
    SHELL _HIDE _DONTWAIT "start notepad " + cmd$
END IF
SYSTEM

Now imagine what other drag and drop apps you could use this for. Paint.net is one I use this on. I use SENDKEYS WIn32 API routines in my COMMAND$() desktop app to control PAINT.net after it opens and loads the image I dragged into the file. That saves me several steps. I can even resize photos without touching the keyboard once!

If you guys have other uses or programs of a similar nature to share, please feel free to either add them here as examples, or add the link to the forum page.

Thanks, an now back to work on my transporter beam. I think I just sent my CapsLock toe to the South of France.

Pete
Reply


Messages In This Thread
DAY 033: COMMAND$ - by Pete - 12-13-2022, 06:48 PM
RE: DAY 033: COMMAND$ - by SMcNeill - 12-13-2022, 07:14 PM
RE: DAY 033: COMMAND$ - by Pete - 12-13-2022, 07:53 PM
RE: DAY 033: COMMAND$ - by SMcNeill - 12-13-2022, 09:12 PM
RE: DAY 033: COMMAND$ - by mnrvovrfc - 12-13-2022, 07:58 PM
RE: DAY 033: COMMAND$ - by SMcNeill - 12-13-2022, 09:26 PM
RE: DAY 033: COMMAND$ - by Dav - 12-14-2022, 01:21 AM
RE: DAY 033: COMMAND$ - by vince - 12-14-2022, 01:31 AM
RE: DAY 033: COMMAND$ - by SMcNeill - 12-14-2022, 01:43 AM
RE: DAY 033: COMMAND$ - by mnrvovrfc - 12-14-2022, 08:27 AM



Users browsing this thread: 1 Guest(s)