Batch file to conditionally execute ifortvars.bat - windows

I am using a make.bat file to compile my Fortran code. To compile using the ifort command, I first need to initialize the compiler by executing ifortvarsbat intel64 command where I had to setup the path variable.
This works fine, however, the path keeps increasing in size and says command too long after 3-4 runs. Now, I open a command prompt, run the make.bat file once and then comment the ifort setup section so my successive run of the make.bat do not set the path multiple times.
I was hoping I could write an IF ELSE command to check whether ifort command executes or not and only set the path and call ifortvars if its not already set. This would save me commenting and uncommenting the make.bat file every time I open a new command prompt.
Unfortunately, I don't know much about batch script writing. Can someone help me with this.
#echo on
#echo ==============================================
#echo Setting Intel Fortran Compiler, please wait ...
#echo ==============================================
::set MY_PATH=C:\Program Files (x86)\Intel\Composer XE 2013\bin
::call "%MY_PATH%"\ifortvars.bat intel64
::call "%MY_PATH%"\ifortvars.bat ia32
#echo ==============================================
#echo Compiling program:
#echo ==============================================
:: -O3 option is used to optimize the code
ifort -O3 "Main.F90"

Instead of checking the path variable, directly check if the compiler is available
where ifort >nul 2>&1 || call "%MY_PATH%\ifortvars.bat" intel64
where command will search for the indicated program inside the path and if found it echoes to console its path. If it does not find the program it echoes an error and sets errorlevel variable.
In the code all the output from where is hidden redirecting the stdout and stderr streams to nul device (>nul 2>&1) and conditional execution is used. The || operator means "execute the following command if the previous one failed"
So, if ifort can not be found then call the configuration batch.
A faster solution could be to directly check a "flag" variable that you define the first time the environment is configured.
if not defined FCompilerSetup (
call "%MY_PATH%\ifortvars.bat" intel64
set "FCompilerSetup=1"
)
note: From my point of view, the recomendation from miltonb and i_am_jorf is the correct way to handle the problem. setlocal / endlocal will remove the problem with the path and is cleaner and easier. But the batch file to set the environment will be called each time you compile your program and the changes removed on end. If any of the changes made to the environment is needed after compilation, it will not be available.

Using setlocal will not change your path beyond the scope of the batch file, so that when it runs again it will modify the path again.
Before running: PATH=C:\Mypath;
setlocal
::set MY_PATH=C:\Program Files (x86)\Intel\Composer XE 2013\bin
....other lines....
endlocal
After running: PATH=C:\Mypath;

Related

octave-gui invoked by bat script does not work unless you run octave(-gui) before (or "run octave-cli.exe with qt")

I want to run octave-gui (Octave 5.1 installed with installer and "C:\Octave\mingw64\bin" is in path variable) scripts run by Windows Task Scheduler. I have to run octave-gui since I want to use the qt toolkit for plotting that octave (without gui) does not support. Therefore I normally use simple bat files like "octave-gui --no-gui c:\path\myfile.m".
But the problem is that I cannot run this bat file by clicking in the Windows Explorer or running from command line. Even the most simple bat file with the content "octave-gui --no-gui" gives me the following error:
But the funny part is that I can make it work somehow:
open command line
run "octave" or "octave-gui" and close/quit it
then I can the bat file from the command line
But this could not be the solution, could it? This only works in the (interactive) command line. How does it work in the Task Scheduler?
So, do you have a solution to run either batch files using octave-gui or octave with qt toolkit.
Here is the workaround with "where" as asked by Gerhard:
The command octave is technically incorrect.
It works only from your Command Prompt window because its extension .bat is listed within the values assigned to an unmodified environment variable %PATHEXT%. It also assumes that there are no other files named octave.com or octave.exe, anywhere within the any of the directories listed under your environment variable %PATH%. Additionally it also assumes that there is not an executable file named octave with any extension listed under %PATHEXT% in the current directory when invoked.
You should, for safety, use octave.bat instead.
octave.bat
Octave.bat will parse any input arguments, set up the required environment, and then run either start octave-gui.exe --gui %* or octave-cli.exe %* if it detected --no-gui as one of the input arguments.
Additionally when running a batch file from another, (in this case start_my_octave_script.bat), you should Call it if you're wanting control to return to it afterwards, which will almost certainly be the case.
call octave.bat <command line options>
If you're satisfied that your %PATHEXT% environment variable is unmodified or at least holds the default values, you can omit the .batextension, but please bear in mind the previous advice.
call octave <command line options>
I made a workaround thanks to the hints made by Compo. It seems to me that a solution must be done in the "octave.bat" and so I did. I made a copy and named it "octave-gui-nogui-withqt.bat" and removed all the gui checking stuff and only run "octave-gui.exe --no-gui" (scroll down):
:; # if running from bash, recall using cmd.exe
:; cmd.exe //c "$0" "$#"; exit $?
#echo off
Rem Find Octave's install directory through cmd.exe variables.
Rem This batch file should reside in Octaves installation bin dir!
Rem
Rem This trick finds the location where the batch file resides.
Rem Note: the result ends with a backslash.
set OCT_HOME=%~dp0\.\..\
Rem Convert to 8.3 format so we don't have to worry about spaces.
for %%I in ("%OCT_HOME%") do set OCT_HOME=%%~sI
Rem Set up PATH. Make sure the octave bin dir comes first.
set PATH=%OCT_HOME%qt5\bin;%OCT_HOME%bin;%PATH%
Rem Set up any environment vars we may need.
set TERM=cygwin
set GNUTERM=wxt
set GS=gs.exe
Rem QT_PLUGIN_PATH must be set to avoid segfault (bug #53419).
IF EXIST "%OCT_HOME%\qt5\bin\" (
set QT_PLUGIN_PATH=%OCT_HOME%\qt5\plugins
) ELSE (
set QT_PLUGIN_PATH=%OCT_HOME%\plugins
)
Rem set home if not already set
if "%HOME%"=="" set HOME=%USERPROFILE%
if "%HOME%"=="" set HOME=%HOMEDRIVE%%HOMEPATH%
Rem set HOME to 8.3 format
for %%I in ("%HOME%") do set HOME=%%~sI
Rem Start Octave (this detaches and immediately returns).
Rem make this call in order to have qt on the cli
octave-gui.exe --no-gui %*
Is this the most elegant one? I guess that upstream Octave should allow a new option like "--no-gui-but-use-qt" or similar. What do you think?
It still confuses me that "octave-cli.exe" and "octave-gui.exe" have more differences besides the visible gui.

Why is an argument passed to a Perl script not working on running it from within a batch file?

I wrote a batch called pippo.bat for launching a program with an argument. However, it fails in getting such argument. In other words, it acts as I don't give it any argument.
pippo.bat
#echo off
mode con: cols=150 lines=5000
title Link Setting
echo.
echo LINK SETTING
echo.
cd /d E:\Program Files (x86)\pippo\bin
pippoedit.ovpl –f C:\Work\pippo.xml
pause
What's wrong?
You're executing pippoedit.ovpl –f C:\Work\pippo.xml.
Note the extension, .ovpl, which isn't registered as an executable. This means you let the OS figure out and start the application associated with that extension, and start that program with that filename as argument. Here, a small repro:
#echo off
foo.txt bar.txt
This starts Notepad or whichever application is associated with the .txt extension and displays the file foo.txt.
When you do this, Windows doesn't do anything with the arguments beyond the first. Instead you should start the application directly, and pass it the arguments:
pippoedit.exe pippoedit.ovpl –f C:\Work\pippo.xml
Or whatever the executable is called.

Execute a batch file before executing in a shortcut (.lnk)

I have multiple versions of a program called Siemens NX. NX uses environmental variables for configuration. I need NX 10.0 to use a different set of environmental variables than my NX 7.5 which uses the system environmental variables. Therefore, I have written a batch file that setups the environmental variables that I need. However, There is a lot of different programs that go with NX 10.0. I don't want to have to create a batch file for each program. Instead, I just want to ammend the shortcuts (.lnk) to execute a batch file before starting. For instance, this is easily done by
C:\Siemens\NX10\UGII\setup_NX10_environment.bat && C:\Siemens\NX10\UGII\ugraf.exe -nx
However, the command window is left open. How can I call the batch script and it closes and then calls my program?
Supply program with parameters to your batch script as follows
C:\Siemens\NX10\UGII\setup_NX10_environment.bat "C:\Siemens\NX10\UGII\ugraf.exe" -nx
and improve that batch as follows:
rem all the original setup_NX10_environment.bat stuff here
%*
exit
or
rem all the original setup_NX10_environment.bat stuff here
call %*
exit
or
rem all the original setup_NX10_environment.bat stuff here
start "" %*
exit
The console window may remain open if you call the executable like this:
executable.exe
However, prepending start to the executable will detach it from the console.
Thus the console will not remain open if you call the executable like this:
start executable.exe
In conclusion, rewrite your command as follows:
C:\Siemens\NX10\UGII\setup_NX10_environment.bat && start C:\Siemens\NX10\UGII\ugraf.exe -nx

Passing parameters to another process inside a batch file

I am writing a batch file for the gdb debugger.
I run the gdb process but I want to know how to pass the parameters to the gdb process from the batch file.
Here is my sample batch file:
#echo off
cd /d "c:\MinGW\bin\"
set PATH=c:\MinGW\bin\
echo %PATH%
gcc -g start.c -o start.exe
gdb start.exe
gdb > set logging file output.txt
gdb > b 1
pause
As shown above when I start gdb for start.exe, I need to pass the set logging file and breakpoint parameters to it via the batch file. But above code is not working. Please help me on this.

Windows Batch File - Batch File stops executing mid way after a command that takes a while to complete

Following are the contents of the file "vs.bat"
call "C:\Program Files (x86)\Microsoft Visual Studio 8\Common7\Tools\vsvars32.bat"
call path.bat
dea usev bis
cd ana
call b-env-i.bat
When I execute this batch file, execution stops after the following step.
dea usev bis
Can anyone please help in pointing out what went wrong here and how I can get all the commands to execute. Note that the aforementioned command (dea usev bis) works fine (both in the batch and if executed separately). dea is the name of the executable and "usv bis" are runtime parameters to the "dea" exe.
I'm running Windows 7.
Clarification:
When I run vs.bat, after the third call "dea usev bis" has executed successfully, the batch file stops executing further. That is both the following calls (which are part of VS.bat) don't get executed
cd ana
call b-env-i.bat
Note that the call "dea usev bis" takes around 20 secs to execute, both when run individually and when run as part of the script.
Update:
I've tried paxdiablo's suggestions, with the following results:
[C:\dea]for %i in (dea.cmd) do #echo %~$PATH:i
ECHO is on.
[C:\dea]for %i in (dea.bat) do #echo %~$PATH:i
ECHO is on.
[C:\dea]for %i in (dea.exe) do #echo %~$PATH:i
C:\dea\bin\dea.exe
[C:\dea]where dea.exe
C:\dea\bin\dea.exe
C:\dea\bin\dea.exe.1
C:\dea\bin\dea.exe.ia64
When I run it explicitly via the following, I still encounter the same issue
c:\dea\bin\dea.exe usev bis
And, as I said earlier, changing the script to call dea does not fix the issue either.
Is there anything else that I can try?
It looks like you're trying to call another batch/script file which, if you leave off call, will simply be chained to rather than called (chained to, in this context, means it doesn't return). I'd suggest changing that line to:
call dea usev bis
By way of example, consider the scripts go2.cmd:
#echo off
echo %1
and go.cmd:
#echo off
call go2 1
go2 2
echo 3
Executing go will only give you:
1
2
because the go2 2 line chains to, rather than calls that script. The call go2 1 line, however, works fine.
And, even if you have a dea.exe file, that does not necessarily mean it's the one being run. Your script calls dea so leaves the choice as to what actually gets run to the shell (searching the path, trying different extensions and so on).
To check whether or not the thing you're actually calling is a batch file or not, you can do the following. First, execute the following commends:
echo %PATH%
for %i in (dea.cmd) do #echo %~$PATH:i
for %i in (dea.bat) do #echo %~$PATH:i
for %i in (dea.exe) do #echo %~$PATH:i
(or I think you can just use where dea* if you're running Win 7+, see here for details).
This will show you if there is a dea.cmd/bat/exe in your path, and tell you where in the path the relevant directories are. If there is a script version, it may exist in the path before the exe version. You can simply examine your %PATH% environment variable to figure out which directory comes first.
Second, if you have the exe in c:\dea\bin, try running that explicitly from your script with:
c:\dea\bin\dea.exe usev bis
If that returns okay, it adds support to the fact a script version is being used instead.
Thirdly, you could just change the script as suggested (add call) and see if that fixes it. If it is referencing a script, that will fix it.
If none of those work (and this appears to be the case based on your response that dea is definitely an exe file), you need to start looking into exactly where the failure occurs. This can be done by placing an echo statement in between every single line so that you can see what's causing the issue. In other words, something like:
echo DEBUG a & time <nul:
call "C:\Program Files (x86)\Microsoft Visual Studio 8\Common7\Tools\vsvars32.bat"
echo DEBUG b & time <nul:
call path.bat
echo DEBUG c & time <nul:
dea usev bis
echo DEBUG d & time <nul:
cd ana
echo DEBUG e & time <nul:
call b-env-i.bat
echo DEBUG f & time <nul:
That will both give you timings for the things that do work and hopefully make it obvious what's not working.

Resources