RunProgramm cmd.exe Example

Just starting out? Need help? Post your questions and find answers here.
MBV
New User
New User
Posts: 5
Joined: Sun Jun 25, 2023 2:29 pm
Location: Germany

RunProgramm cmd.exe Example

Post by MBV »

From within a Windows program, I want to execute a command line operation using RunProgram: for example, "Cmd.exe /C copy a.txt b.txt."
Does anyone have an example syntax for this? Thanks.
User avatar
Bisonte
Addict
Addict
Posts: 1305
Joined: Tue Oct 09, 2007 2:15 am

Re: RunProgramm cmd.exe Example

Post by Bisonte »

it mostly depends on what you are planning to do...
In this case you posted i would use CopyFile instead of opening a console ;)

RunProgram("cmd.exe", Parameter$, WorkDir$) is the minimum... the example in the docs are ok with it.

ups....

Code: Select all

RunProgram("cmd.exe", "copy C:\text.txt D:\text.txt", "")
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

Re: RunProgramm cmd.exe Example

Post by Marc56us »

Bisonte wrote: Fri Apr 25, 2025 12:31 pm it mostly depends on what you are planning to do...
In this case you posted i would use CopyFile instead of opening a console ;)

RunProgram("cmd.exe", Parameter$, WorkDir$) is the minimum... the example in the docs are ok with it.

ups....

Code: Select all

RunProgram("cmd.exe", "copy C:\text.txt D:\text.txt", "")
Small oversight :wink: : the shell parameter (/c or /k) as first parameter

Code: Select all

RunProgram("cmd.exe", "/c copy C:\text.txt D:\text.txt", "")
/c to do command and then exit
/k to do command and keep shell open

and best

Code: Select all

RunProgram(GetEnvironmentVariable("ComSpec"), "/c ...
User avatar
Piero
Addict
Addict
Posts: 865
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: RunProgramm cmd.exe Example

Post by Piero »

Code: Select all

; needs admin mode
RunProgram("cmd.exe","/C copy "+~"\""+"C:\my text.txt"+~"\" \""+"C:\my text2.txt"+~"\"","",#PB_Program_Wait|#PB_Program_Hide)
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

Re: RunProgramm cmd.exe Example

Post by Marc56us »

Piero wrote: Fri Apr 25, 2025 4:22 pm

Code: Select all

; needs admin mode
RunProgram("cmd.exe","/C copy "+~"\""+"C:\my text.txt"+~"\" \""+"C:\my text2.txt"+~"\"","",#PB_Program_Wait|#PB_Program_Hide)
With ~ " " you can simplify (A single tild is enough)

Code: Select all

Debug  "/C copy "+~"\""+"C:\my text.txt"+~"\" \""+"C:\my text2.txt"+~"\""
Debug ~"/C copy \"C:\\my text.txt\" \"C:\\my text2.txt\""

Code: Select all

/C copy "C:\my text.txt" "C:\my text2.txt"
/C copy "C:\my text.txt" "C:\my text2.txt"
:wink:
Fips
User
User
Posts: 35
Joined: Sun Feb 20, 2022 1:03 pm

Re: RunProgramm cmd.exe Example

Post by Fips »

I believe you also could call the command directly like this:

Code: Select all

RunProgram("copy", "C:\text.txt D:\text.txt", "")
(I dont know about windows though, but i'm pretty sure i've called terminal commands like that with Linux.)
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

Re: RunProgramm cmd.exe Example

Post by Marc56us »

Fips wrote: Fri Apr 25, 2025 6:44 pm I believe you also could call the command directly like this:

Code: Select all

RunProgram("copy", "C:\text.txt D:\text.txt", "")
(I dont know about windows though, but i'm pretty sure i've called terminal commands like that with Linux.)
If parameters contains spaces, you need to use quotes

In Linux (all unix), the official 'copy' command (cp) is a standalone program (/usr/bin/cp) so you can call it as first with run program.
In Windows, 'copy' is an internal command of the shell (defaut: cmd.exe for NT systems (Win NT, 8, 10, 11) or command.com in Windows 9.x and DOS) Shell contain commands like, copy, del, ... but not all (i.e xcopy is a standalone program (xcopy.exe))
Type cmd /? to show all

:wink:
MBV
New User
New User
Posts: 5
Joined: Sun Jun 25, 2023 2:29 pm
Location: Germany

Re: RunProgramm cmd.exe Example

Post by MBV »

Lot of helpful Information! Thank you!
Axolotl
Addict
Addict
Posts: 802
Joined: Wed Dec 31, 2008 3:36 pm

Re: RunProgramm cmd.exe Example

Post by Axolotl »

MBV wrote: Sat Apr 26, 2025 8:09 am Lot of helpful Information! Thank you!
The answers always depend on the particular application.
If you also want to run the application on other computers, I recommend the following option.
You avoid unexpected reactions.

Code: Select all

/D      Disable execution of AutoRun commands from registry
Use it like this

Code: Select all

cmd.exe /D/C copy .... 


Maybe it is a good idea to read the help of cmd.exe

Code: Select all

cmd.exe /?
BTW: With the above mentioned RunProgram parameter #PB_Program_Wait you should be aware that now it is a blocking call. The command to be executed should therefore not take “too long”. Solution: Thread or Timer ....
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
User avatar
Piero
Addict
Addict
Posts: 865
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: RunProgramm cmd.exe Example

Post by Piero »

Marc56us wrote: Fri Apr 25, 2025 5:09 pm With ~ " " you can simplify (A single tild is enough)

Code: Select all

Debug  "/C copy "+~"\""+"C:\my text.txt"+~"\" \""+"C:\my text2.txt"+~"\""
Debug ~"/C copy \"C:\\my text.txt\" \"C:\\my text2.txt\""
:wink:

Code: Select all

; needs admin mode
f.s = "C:\my text.txt" ;                          %~dpn means Disk Path Name  
c.s = ~"( FOR %I IN (\"" + f + ~"\") DO copy %I \"%~dpnI 2%~xI\" && echo Done! ) || echo Error!!!"
RunProgram("cmd.exe","/D/K "+c,"") ; copy "C:\my text.txt" "C:\my text 2.txt"
:wink:
Post Reply