How to open a program with cmd.exe from Python? - windows

I am using python 3.3 and in my code I need something to open cmd.exe with the following arguments and run it. The desired line in cmd.exe should be:
C:\Program Files (x86)\GlobalMapper15>global_mapper.exe script.gms variable
I saw different answers but the most I managed with subprocess.call is to open either cmd.exe, or global_mapper.exe. I did not manage to obtain the line above in cmd.exe.
I tried so far:
#import subprocess
import os
os.system("cmd.exe C:\Program Files (x86)\GlobalMapper15\global_mapper.exe script.gms")
#subprocess.call(["cmd.exe C:\Program Files (x86)\GlobalMapper15\global_mapper.exe", "script.gms"])
Neither of them worked well.
Of course, it would be great if the line would be also executed. Can anyone help me make this happen?
Thank you all,

To run global_mapper.exe in given directory:
#!/usr/bin/env python
from subprocess import call
dir = r"C:\Program Files (x86)\GlobalMapper15"
cmdline = "global_mapper.exe script.gms variable"
rc = call(cmdline, cwd=dir) # run `cmdline` in `dir`
If you want to start the command in a new console window:
rc = call("start cmd /K " + cmdline, cwd=dir, shell=True)

maybe with this:
import os
os.system("program.exe -parameter")

you can Create a .bat file and call it
dir = r"C:\Program Files (x86)\GlobalMapper15"
write=''.join((dir,'\\','call.bat'))
f= open(write,'w')
with f:
f.write('#ECHO ON\n Call global_mapper.exe script.gms variable'
subprocess.call(write,cwd=dir)

Related

Advise Request: How to launch three .bat files at the same time?

I got three separate .bat files. Each has the following three commands respectively.
file1.bat: cmd /K "cd C:\PythonScripts\X3865\ & python run_all.py"
file1.bat: cmd /K "cd C:\PythonScripts\X3865_1\ & python run_all.py"
file1.bat: cmd /K "cd C:\PythonScripts\X3865_2\ & python run_all.py"
Currently to execute the program, I go to the Windows file folder and double click on each of them to launch the programs. They come up in three different command windows.
I want to combine the above commands in 1 single .bat file, which when executed will launch all the three programs automatically. How can I do this?
The answers from Mark and ian0411 will run the three python scripts one after the other. If that's what you want, go with either of those. If you want to run the three scripts simultaneously, your batch file should be
start cmd /c python "c:\pythonscripts\x3865\run_all.py"
start cmd /c python "c:\pythonscripts\x3865_1\run_all.py"
start cmd /c python "c:\pythonscripts\x3865_2\run_all.py"
This will start each in a separate window; if there is output that is needed from the command(s), change the /c to /k; this will leave the window open until you close it manually.
This should run the Python scripts in order:
python "C:\PythonScripts\X3865\run_all.py"
python "C:\PythonScripts\X3865_1\run_all.py"
python "C:\PythonScripts\X3865_2\run_all.py"
Their outputs will all be printed to the same cmd window. If you wish to see the outputs at the end, add pause to the end of the bat file so that it won't automatically close.
Save the following into a .bat file
python "C:\PythonScripts\X3865\run_all.py"
python "C:\PythonScripts\X3865_1\run_all.py"
python "C:\PythonScripts\X3865_2\run_all.py"
And then just run that .bat file and it should do it.

Launch .exe from MATLAB m.file, path issues

I want to Launch and .exe file from an m-file in MATLAB. The .exe does not Launch when i try. In restPath, the path of the .exe included. I am coding in a Windows Environment using the command line. My idea was to pass the command to run the .exe to the command line.
command = restPath;
[status,cmdout] = system(command,'-echo');
The error message is; Error file .cfg not found...
Do you have any suggestions?
Best regards
Edit: The .exe is now launched in 2 iterations. 1. cd to file, 2. Launch
addpath(restPath);
command = horzcat('cd ',restPath);
[status,cmdout] = dos(command,'-echo');
execute = 'abc.exe';
[statusExe,cmdoutExe] = system(execute,'-echo');
The main issue that I see here is that you are using two separate commands for the cd and the execution. Once the cd command executes, the command line context is thrown away and you start with a new one when you execute the system command (so the cd has no effect).
I would suggest either concatenating the two commands into one using the '&' notation like the following:
[status,cmdout] = dos([command ' & ' execute],'-echo');
or you could change your Matlab workspace first using a standard cd command in your mscript and then execute the system command.
currentPath = pwd;
cd(restPath);
execute = 'abc.exe';
[statusExe,cmdoutExe] = system(execute,'-echo');
cd(currentPath);
It is also possible that the exe you are calling is expecting an additional input to point to the .cfg file (although this may not be an issue if you have that in the same directory as the exe and it expects it to be there).

How to call an executable file directly in notepad++?

In windows,I use the Notepad++ to write tex file, in the "run..." dialog,I input that:
cmd /k D:\CTEX\MiKTeX\miktex\bin\xelatex $(FULL_CURRENT_PATH)
then run it,however the result shows that 'xdvipdfmx' is not an executable file. But I am sure that I have add its path to the system environment variable,and when I direct run it in the terminal, it's ok.
So,I want to know what I should do to run it in the notepad++ correctly.
Try these improvements:
enclose paths into quotes to avoid problems with spaces
add .exe to executable file name
test the full command in command prompt to see if it works (replace $(FULL_CURRENT_PATH)) with actual file name
please let me know the result
Your example after changes:
cmd /k "D:\CTEX\MiKTeX\miktex\bin\xelatex.exe" "$(FULL_CURRENT_PATH)"
Test it in command prompt like:
cmd /k "D:\CTEX\MiKTeX\miktex\bin\xelatex.exe" "D:\Data\MyDoc1.tex"

powershell command not running in .bat file?

I am trying to make a simple script to set my gcc variables. as a .bat file.
the variable is set like this
$env:Path += ";C:\Users\Brett\Compilers\MinGW\bin"
this runs just fine when I type/paste it into power shell.
but when I paste into a script myscript.bat, and run it through powershell I get this error:
C:\Users\Brett\Compilers>"$env:Path += ";C:\Users\Brett\Compilers\MinGW\bin""
The filename, directory name, or volume label syntax is incorrect.
PS C:\Users\Scruffy\Compilers>
PowerShell is a seperate execution enviroment from with Windows Command Line (cmd.exe)
If you want to run powershell commands from a batch file you need to save the powershell script (.ps1) and pass it into powershell.exe as a command line argument.
Example:
powershell.exe -noexit c:\scripts\test.ps1
More Information is available here on Microsoft TechNet
In general, leave batch stuff to .BAT files and put PowerShell stuff into .ps1 files.
I can duplicate your results here - but those are to be expected. Cmd.exe sees a string then a path and then gets quite confused as the syntax is not one that the command prompt can handle. So it gives that error message.
If you want to add stuff to your path, then why not put the statement inside a .ps1 script file?
As mentioned by others, you need to save the code in a .ps1 file and not .bat.
This line (from Setting Windows PowerShell path variable) will do the trick:
$env:Path = $env:Path + ";C:\Users\Brett\Compilers\MinGW\bin"
Or even shorter:
$env:Path += ";C:\Users\Brett\Compilers\MinGW\bin"

Lua programming - os.execute() is not working in Windows

I'm creating a function in pure-Lua to scan the files from a directory and put they on a another file.
The command I tryed was:
os.execute( "dir /B C:\\Users\\Fernando\\workspace\\Organizator2\\s1 >
C:\\Users\\Fernando\\workspace\\Organizator2\\temp.txt" )
but... dont works! I did many tests with others simpler commands, like "start notepad" or "mkdir C:\test", and they dont worked too! The worse part is that I tryed this same commands directly in the Prompt, and there is all correct.
I tryed use tooo the io.popen(), but it the system returned "illegal operation" for any command i passed (even a empty string!).
here is the all code:
function ScanDirectory(source, str)
local str = str or "temp.txt"
os.execute("dir /B "..source.." > "..str)
directory = io.open(str,"r")
return directory
end
-- main script
do
local source = "C:\\Users\\Fernando\\workspace\\Organizator2\\s1"
local directory = ScanDirectory(source, "C:\\Users\\Fernando\
\workspace\\Organizator2\\temp.txt")
end
I'm using windows 7 and the Luaforwindows, 5.1, and the LuaEclipse
Have someone ever seen a problem like this?
Please try it with this syntax:
os.execute [["dir /B C:\Users\Fernando\workspace\Organizator2\s1 >
C:\Users\Fernando\workspace\Organizator2\temp.txt"]]
Please note that the backslash (\) is not a special character in this case.
(Lua uses cstrings internally, sometimes it leads to some weird and amazing results :P)
Most of the commands you listed appear to be shell commands that only work within a command prompt. Try running cmd.exe directly to see if you get a prompt, and if so, you can try passing commands to cmd.exe via the /c option. You could also try notepad without the start to see if that runs.
os.execute('cmd.exe /c dir /B C:\\> C:\\test.txt')
That works. Useing Linux-style commands in win is a bad idea at all =)
I just tested your code on my computer and it works correct (with my directories, of course). Maybe you are not getting the expected result because your directory string is broken with an newline char, resulting in:
dir /B C:\Users\Fernando\workspace\Organizator2\s1 > C:\Users\Fernando\
workspace\Organizator2\temp.txt
The correct should be:
dir /B C:\Users\Fernando\workspace\Organizator2\s1 > C:\Users\Fernando\workspace\Organizator2\temp.txt
Please try changing the do end to:
local source = "C:\\Users\\Fernando\\workspace\\Organizator2\\s1"
local directory = ScanDirectory(source, "C:\\Users\\Fernando\\workspace\\Organizator2\\temp.txt")

Resources