Page 1 of 1
RunProgramm cmd.exe Example
Posted: Fri Apr 25, 2025 12:24 pm
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.
Re: RunProgramm cmd.exe Example
Posted: Fri Apr 25, 2025 12:31 pm
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", "")
Re: RunProgramm cmd.exe Example
Posted: Fri Apr 25, 2025 2:41 pm
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

: 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 ...
Re: RunProgramm cmd.exe Example
Posted: Fri Apr 25, 2025 4:22 pm
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)
Re: RunProgramm cmd.exe Example
Posted: Fri Apr 25, 2025 5:09 pm
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"

Re: RunProgramm cmd.exe Example
Posted: Fri Apr 25, 2025 6:44 pm
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.)
Re: RunProgramm cmd.exe Example
Posted: Sat Apr 26, 2025 7:31 am
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

Re: RunProgramm cmd.exe Example
Posted: Sat Apr 26, 2025 8:09 am
by MBV
Lot of helpful Information! Thank you!
Re: RunProgramm cmd.exe Example
Posted: Sat Apr 26, 2025 12:11 pm
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
Maybe it is a good idea to read the help of 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 ....
Re: RunProgramm cmd.exe Example
Posted: Mon Apr 28, 2025 4:10 am
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\""
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"
