Run commands of Command Prompt

Just starting out? Need help? Post your questions and find answers here.
User avatar
Haruks
User
User
Posts: 30
Joined: Wed Sep 23, 2009 1:41 am
Location: Brasil (Brazil)

Run commands of Command Prompt

Post by Haruks »

Hello,

I am trying learn the PureBasic... :D
Currently I know write on CMD (Command Prompt of Windows)
But now I want learn the PureBasic, but as I am starting, if I can run commands of CMD on PB this will help me a lot! :!: !

> Someone know how can i run commands of CMD on the PB?
I think in use the "OpenFile" to open a .BAT, but this open a CMD window...

Can someone help me?

(Sorry for my English, I'm learning too =] )
=D
thanos
Enthusiast
Enthusiast
Posts: 423
Joined: Sat Jan 12, 2008 3:25 pm
Location: Greece
Contact:

Re: Run commands of Command Prompt

Post by thanos »

> Someone know how can i run commands of CMD on the PB?
I think in use the "OpenFile" to open a .BAT, but this open a CMD window...
Could you give an example of the commands you want to run?
Regards.

Thanos
» myPersonal Banker :: Because you do not need to have a master degree in economics in order to organize your finances!
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Re: Run commands of Command Prompt

Post by Kaeru Gaman »

if you want to execute a shell command, start command.com with RunProgram and pass the command as Parameter$.
if you want to execute batch files, start them with RunProgram.
oh... and have a nice day.
User avatar
Haruks
User
User
Posts: 30
Joined: Wed Sep 23, 2009 1:41 am
Location: Brasil (Brazil)

Re: Run commands of Command Prompt

Post by Haruks »

Hello thanos, thanks for your reply!

Examples of commands:
> Create a local user with password:
net user USER PASSWORD /add /expires:never

> Add user to a group:
net localgroup GROUP USER /add

> Create a registry key:
reg add "HKLM\SOFTWARE\Example" /v VALUE /t REG_DWORD /d 0 /f

> Query Services:
sc query SERVICE


Now I thought of one thing... I can use the "OpenFile" to run the command and use the parameters... i will test this now!
because the commands "NET" "REG" and "SC" is files on C:\Windows\System32


- Haruks
=D
User avatar
Haruks
User
User
Posts: 30
Joined: Wed Sep 23, 2009 1:41 am
Location: Brasil (Brazil)

Re: Run commands of Command Prompt

Post by Haruks »

Kaeru Gaman wrote:if you want to execute a shell command, start command.com with RunProgram and pass the command as Parameter$.
if you want to execute batch files, start them with RunProgram.
yes... I will test this!

Thanks Thanos and Kaeru Gaman
=D
thanos
Enthusiast
Enthusiast
Posts: 423
Joined: Sat Jan 12, 2008 3:25 pm
Location: Greece
Contact:

Re: Run commands of Command Prompt

Post by thanos »

Just a snippet, if the PATH includes the reg.exe directory:

Code: Select all

Param$ = "ADD " + Chr(34) + "HKLM\SOFTWARE\Example" + Chr(34) + " /v VALUE /t REG_DWORD /d 0 /f"
Result = RunProgram("reg", Param$, "")
Debug Result
if it is not in the PATH you should add the directory which contains the reg.exe:

Code: Select all

Param$ = "ADD " + Chr(34) + "HKLM\SOFTWARE\Example" + Chr(34) + " /v VALUE /t REG_DWORD /d 0 /f"
Result = RunProgram("reg", Param$, "C:\Windows\System32")
Debug Result
Regards

Thanos
» myPersonal Banker :: Because you do not need to have a master degree in economics in order to organize your finances!
User avatar
Haruks
User
User
Posts: 30
Joined: Wed Sep 23, 2009 1:41 am
Location: Brasil (Brazil)

Re: Run commands of Command Prompt

Post by Haruks »

Thanos

then, when I will use " I need create a variable...
Thanks for this information!!

Only a question:
on the code below, the ,"" is the path correct? (C:\Windows\System32 by default)

Code: Select all

OpenConsole()
PrintN("Query the service BROWSER")
RunProgram("sc.exe", "query browser","")
input()
End
=D
thanos
Enthusiast
Enthusiast
Posts: 423
Joined: Sat Jan 12, 2008 3:25 pm
Location: Greece
Contact:

Re: Run commands of Command Prompt

Post by thanos »

No. I think that the "WorkingDirectory" is the program's directory by default.
If you want to run the sc.exe, the file must be either in the program's directory or in one of the PATH's directories.
Otherwise you must include the directory in the WorkingDirectory parameter. E.g. The directory which contains tour program is the C:\CodeBase and the sc.exe is in the C:\Haruks directory which is not included in the PATH variable:

Code: Select all

RunProgram("sc.exe", "query browser", "C:\Haruks")
Regards.

Thanos
» myPersonal Banker :: Because you do not need to have a master degree in economics in order to organize your finances!
User avatar
p2hicy
User
User
Posts: 11
Joined: Fri Apr 25, 2003 5:31 pm
Location: Iceland
Contact:

Re: Run commands of Command Prompt

Post by p2hicy »

Code: Select all

ImportC "msvcrt.lib"
  system(str.p-ascii)
EndImport

OpenConsole()

system("echo oh hai")
system("pause")
That oughta do it. Now you can issue your commands directly as if you would type them into a command prompt window.
User avatar
Haruks
User
User
Posts: 30
Joined: Wed Sep 23, 2009 1:41 am
Location: Brasil (Brazil)

Re: Run commands of Command Prompt

Post by Haruks »

> thanos
Humm... don't need use the path C:\windows\system32 for run a command... so, when i use the "RunProgram" this is equal if I use the "Run" (of the windows: start>run) but we can use many options!! (run hide for example...)

> p2hicy

Ual! Thanks a lot!!!!
one question about this lib.. Can I run command with this lib without show any screen?

for example:
RunProgram("sc.exe", "query browser","",#PB_Program_Hide)
=D
User avatar
p2hicy
User
User
Posts: 11
Joined: Fri Apr 25, 2003 5:31 pm
Location: Iceland
Contact:

Re: Run commands of Command Prompt

Post by p2hicy »

If you can live with a short pop-up of the console window, you can use this:

Code: Select all

ImportC "msvcrt.lib"
  system(str.p-ascii)
EndImport

a$ = Space(500)

If OpenConsole()

	If GetConsoleTitle_(@a$, Len(a$))
		ShowWindow_(FindWindow_(0, a$), #SW_HIDE)
	EndIf

	system("sc query browser")
	
EndIf
User avatar
Haruks
User
User
Posts: 30
Joined: Wed Sep 23, 2009 1:41 am
Location: Brasil (Brazil)

Re: Run commands of Command Prompt

Post by Haruks »

Where i get more information about the lib MSVCRT?

Thanks!
=D
thanos
Enthusiast
Enthusiast
Posts: 423
Joined: Sat Jan 12, 2008 3:25 pm
Location: Greece
Contact:

Re: Run commands of Command Prompt

Post by thanos »

Haruks wrote:Where i get more information about the lib MSVCRT?

Thanks!
Try this link:
http://msdn.microsoft.com/en-us/library ... S.80).aspx
Regards

Thanos
» myPersonal Banker :: Because you do not need to have a master degree in economics in order to organize your finances!
Post Reply