View Full Version : Execute simple OS command
rtconstant
23-04-2008, 18:06
I am new to ThinBasic, but not to programming. I have a simple question.
I use ThinBasic in a Windows XP console. How do I issue a simple command to the console command program?
That is, if I want the underlying console command interpreter to do something. Two simple examples are:
"dir"
"copy file1.txt file2.txt"
I cannot find information in the help file that will allow me to do something simple like this. Example lines in ThinBasic to do these two things is all that I need.
ErosOlmi
23-04-2008, 21:41
Ciao rtconstant.
Yes, you are right, there are not much info about executing environment commands.
Here some info.
To execute operating system commands thinBasic implements some functions under the OS module. In particular you can check OS_Shell (http://www.thinbasic.com/public/products/thinBasic/help/html/os_shell.htm) and OS_ShellExecute (http://www.thinbasic.com/public/products/thinBasic/help/html/os_shellexecute.htm). For example to fire a DIR command use something like:
uses "OS"
OS_SHELL(OS_ENVIRON("COMSPEC") + " /C DIR *.* > FileName.txt")
This invoke the operating system DIR command and put its output into FileName.txt
To copy a file you can use something similar
uses "OS"
OS_SHELL(OS_ENVIRON("COMSPEC") + " /C COPY filename.txt FileName2.txt")
When invoking operating commands it is better to always use the COMSPEC info before the command. It will give you the assurance to always points to the full path of the command line interpreter.
OS_SHELL can have some additional parameters that are used to specify the window style and the sync or async execution mode. For example the following example will not show any window (in case command is invoked by a GUI script.
uses "OS"
OS_SHELL(OS_ENVIRON("COMSPEC") + " /C COPY filename.txt FileName2.txt", %OS_WNDSTYLE_HIDE)
Hope this can help for a start.
Ciao
Eros
Great question and thanks for the answer Eros. Amazing all the things thinBasic can do!
rtconstant
24-04-2008, 16:05
Eros,
Thanks for the info in your reply. You answered my question completely, saving me a signigicant period of time in trial/error learning and searching help files, forum posts, and internet sources to make sure I am doing things in the way that ThinBasic best supports.
Again, thanks.
Petr Schreiber
24-04-2008, 17:42
rtconstant,
if you do not need to use shell commands you can do the same as copy with FILE_COPY and dir using DIR_LISTARRAY...
Bye,
Petr
dcantera
27-05-2008, 06:15
cool, I could use this too.
one of my next tasks is to launch a brower... but it wouldn't work as I thought... here is the script fragment...
daveC
OS_SHELL(OS_ENVIRON("COMSPEC") + "/C \'c:\program files\internet explorer\iexplore.exe\' google.com")
I tried escaping double quotes as well... no error, no launch..
this command line works
C:\> "c:\program files\internet explorer\iexplore.exe" purebasic.com
ErosOlmi
27-05-2008, 07:36
uses "os"
dim sCmd as string
'---Build the path to IE
sCmd = $DQ & OS_GetSpecialFolder(%CSIDL_PROGRAM_FILES) & "internet explorer\iexplore.exe" & $DQ
'---Add the command line params inside double quote (just in case there are special chars)
sCmd += " " & $DQ & "www.google.com" & $DQ
'---Show the command
msgbox 0, sCmd
'---Shell the full command
OS_SHELL(sCmd)
ErosOlmi
27-05-2008, 08:01
Or another way using the shell and using the default browser:
uses "os"
OS_ShellExecute("open", "http://www.google.com", "", "", 1)
dcantera
28-05-2008, 05:32
how slick is this! just too cool for words, and simple too.
this is another valuable tool for my toolbox...
eros you are the Man! ;)
thanks,
daveC
ErosOlmi
28-05-2008, 07:32
Exagerated ;)
Merit goes to the OS (Microsoft) and to the Compiler we use to produce thinBasic.
Michael Clease
28-05-2008, 21:00
Pretty much anything that can be execute from the windows start RUN menu can be called from OS_ShellExecute.