how to run a cmd file from matlab - matlab

I am trying to run a cmd file from MATLAB but unable to execute it. Can anybody see nay problem in the below code?
this is what I have inside my cmd file:
echo on
>test.log 2>&1 (
C:/testProj/Make/makeit.cmd param1
)
And this is the MATLAB code:
Out = 'C:/testProj/test.cmd';
system(Out);
But this actually does not run the cmd file.

Well for somereason it would not run if i would give the complete path of the cmd in bat file. so I had a cd command to change the directory and then run. now it runs fine, Thanks all appreciate your help!

What about using eval, like this:
eval(['!test.cmd']);
I have succesfully used this to run .bat files (and this output of the .bat script showed in my matlab command line). I also found this dos command, but I am not sure if it works allright:

You can just type the following strings to get things down:
!(c:/testProj/test.cmd)
This is actually no different from
system('c:/testProj/test.cmd')
I think you should check if the path is wrong. As to your code in the cmd file, that's beyond my ability to help.

Related

How to run octave script from shell at launch

I'm using Ubuntu system. To run MATLAB script at launch, I can type matlab -nodesktop -r "run ./my_program.m".
How can I achieve the same function on Octave, like octave --no-gui -some_command?
I've read this post and this post. They did not answer my question.
As mentioned in one of the comment, one solution is:
octave --persist my_program.m
Currently looks like it is enough to say for script (not plain function)
octave my_program.m
Also if you have octave installed in your path you could and in the beginning of script as the first line the desired interpreter (works for python, bash, whatever), for example for default location :
#!/usr/bin/octave
and you can run them without even adding the octave in front... just the script name....
For Windows users:
I see it's a bit harder, since there's no "octave.exe" file.
I do have:
"C:\Program Files\GNU Octave\Octave-6.4.0\mingw64\bin\octave-cli-6.4.0.exe"
which works after adding both parent directories
"C:\Program Files\GNU Octave\Octave-6.4.0\mingw64\bin"
and
"C:\Program Files\GNU Octave\Octave-6.4.0\mingw64"
to PATH.
All this happens automatically in the startup script which is the normal entry point of the SW:
"C:\Program Files\GNU Octave\Octave-6.4.0\octave.vbs"
Finaly, after adding all to path I run it like this:
"C:\Program Files\GNU Octave\Octave-6.4.0\mingw64\bin\octave-cli-6.4.0.exe" "C:\MatlabScripts\matlabScript1.m" c:\temp\myCuteImage [example for prm]

Running Executable from Shell Script

I am having trouble launching an executable that I have created from a shell script. I would like to automate testing by running the program many times with different command line options to verify it is working.
When I type echo $SHELL, /bin/sh is displayed.
The following is my shell script:
#!/bin/sh
clear
echo "Running first test."
./myProgram
exit 0
When I run the script (sh myScript.sh), with myProgram in the same directory, I see the following output:
Running first test.
: not foundsh: line 4:
When executing the program ./myProgram, it runs as expected with no command line options.
I have also tried:
myProgram
./myProgram &
myProgram &
based on answers to somewhat similar questions, but they all result in the above error message.
Your newlines are goofed. Use dos2unix to fix.
why don't you try using the full path?
e.g., if myProgram is in /home/user1/bin, you can try /home/user1/bin/myProgram instead of ./myProgram. This should work.
You can also add the path to path variable, $PATH and directly call myProgram from anywhere.
Run "export PATH=$PATH:/home/user1/bin" on your terminal without the quotes. Note that this affects only your current termial session. If you want to permanently add the path, update your .bashrc file in your home directory with the following line:

Perl running a batch file #echo command not found

I am using mr on Windows and it allows running arbitrary commands before/after any repository action. As far as I can see this is done simply by invoking perl's system function. However something seems very wrong with my setup: when making mr run the following batch file, located in d:
#echo off
copy /Y foo.bat bar.bat
I get errors on the most basic windows commands:
d:/foo.bat: line 1: #echo: command not found
d:/foo.bat: line 2: copy: command not found
To make sure mr isn't the problem, I ran perl -e 'system( "d:/foo.bat" )' but the output is the same.
Using xcopy instead of copy, it seems the xcopy command is found since the output is now
d:/foo.bat: line 1: #echo: command not found
Invalid number of parameters
However I have no idea what could be wrong with the parameters. I figured maybe the problem is the batch file hasn't full access to the standard command environment so I tried running it explicitly via perl -e 'system( "cmd /c d:\foo.bat" )' but that just starts cmd and does not run the command (I have to exit the command line to get back to the one where I was).
What is wrong here? A detailed explanation would be great. Also, how do I solve this? I prefer a solution that leaves the batch file as is.
The echo directive is executed directly by the running command-prompt instance.
But perl is launching a new process with your command. You need to run your script within a cmd instance, for those commands to work.
Your cmd /c must work. Check if you have spaces in the path you are supplying to it.
You can use a parametrized way of passing arguments,
#array = qw("/c", "path/to/xyz.bat");
system("cmd.exe", #array);
The echo directive is not an executable and hence, it errors out.
The same is true of the copy command also. It is not an executable, while xcopy.exe is.

How to write Command Prompt to a file AND to the screen at the same time

I am trying to write the output of a powershell command to a file. I have completed this:
powershell psake "Dev08, Deploy-things" >> C:\Deployments\DeploymentLog.txt 2>&1
This is dumping all the screen output to a File... GREAT!!
NOW, I also want it to dump to the console screen too. I have used the argument >CON
but it will dump everything to the screen, but not the file.
powershell psake "Dev08, Deploy-things" >> C:\Deployments\DeploymentLog.txt 2>&1 >CON
Maybe >CON is not the way to go.. Here is a test I am using:
dir > c:\test\directory.txt 2>&1
Anyone got an idea on how to do this?????
Instead of having CMD output the file, let your Powershell command do it with the Tee-Object cmdlt.
So for your case, something like:
powershell ^& "'C:\Program Files\7-Zip\7z.exe'" ^| tee B:\test.log
Note that the ^ is required before | so that way it passes it to Powershell and isn't interpreted literally.
Well, as it turns out, I cannot find anything that says you can write to 2 different outputs in Command Prompt. Once you redirect the output to a file, you redirect it. That's it. I cannot find any way to write to the screen as well using commands. SO, the solution that I used was to download "TailXP". Install it on the box and configure it to point to the file i am writing. This will read the file as it's being generated and write it to it's own console screen. It served our purpose and I have it all wrapped up in a .cmd file.

Calling a command line program

I have a executable that when double clicked opens in a command line window.
Now there is a input file (i.e named "sphere_15000.inp") in the same directory where the executable apame_win64.exe is located. So we can inter the file name in the command line.
The question is how this can be done from mathematica front end? With the RunThrough command I tried to do it with no avail.
RunThrough["Executable Location", "sphere_15000"]
Do I need to put this file location in my Windows 7 environment path variable? Hope for some suggestion in this regard.
UPDATE
Found a solution to my problem.
First set the Mathematica directory to the folder where the executable is located.
path="C:\Users\FlowCrusher\Desktop\CUSP solver\Apame_build_2011_01_09\solver";
SetDirectory[path];
Then use the following style of input.
Run["\"\"apame_win64.exe\" \"input\"\""]
Here "apame_win64.exe" is the executable one want to run and "input" is the input file for the executable. This solves the problem. But a new item in the wishlist.
Is there a way to hide the console window in the background?
Here is how it looks on my screen.
As Chris suggested if we use minimized console in the Run command we get a minimized window but the program does not execute.
I hope that a solution exists.
BR
Yes, you might put the folder of you executable into the Path variable, or provide the full path name.
However, RunThrough seems to have been superseeded (on Windows) by
Import["!command ","Text"], which will execute command and read the comaand line output into Matheamtica as a string.
E.g.:
Export["testit.txt", "bla", "Text"];
Import["!dir" <> " testit* > dir.log", "Text"];
FilePrint["dir.log"]
--
Otherwise, I also had good results in the past using NETLink (not sure if WScript.shell
still works on Windows7/8 or if one should use something else).
Emulating Run (RunThrough is not really needed I think):
Run2[cmd_String] := Module[{shell},
Switch[$OperatingSystem,
"Windows",
Needs["NETLink`"];
shell = NETLink`CreateCOMObject["WScript.shell"];
shell # run[cmd,0,True],
"Unix",
Run # cmd,
"MacOSX",
Run # cmd ] ];
Can you run your application with input from a basic command window instead of the application console? This might be the form of command you would need:
apame_win64 -input sphere_15000.inp
or simply
apame_win64 sphere_15000.inp
You can probably check the available switches by running apame_win64 -help
A multi-part command can be run from Mathematica, e.g.
Run["type c:\\temp\\test.txt"]
Alternatively, also returning output to the Mathematica session:
ReadList["!type c:\\temp\\test.txt", String]
I landed here wanting to run abaqus command line on windows.
The solutions provided here worked out for me (Windows 7, Mathematica 9):
SetDirectory#path;
Run["start /min abaqus job=" <> fileName <> " interactive ask_delete=OFF >> log.txt"]
(Here the abaqus option ask_delete=OFF overwrites an existing simulation results and the >> redirects all the output to a file)
I think, minimizing the window did not run in your case since the executable throws open that window. In that case, this might be of some help