.bat file and two commands in one cmd line


Joined
Dec 20, 2012
Messages
4
Reaction score
0
Hello everybody,

I am trying to execute three commands in one go in cmd.exe, i.e.:

cd C:\xy & SDKShell & where ogr2ogr

i.e. I am changing to the directory where a file "SDKShell.bat" lies, execute it, then I want to execute some other command. Unfortunately, the execution of the line stops after the execution of the bat-file. This code is just an example for a little more complicated stuff I want to do (the *.bat is setting the environment (variables) for the third command), so just typing "where ogr2ogr" after the execution of the bat is not an option. So my questions are:
1. is it possible at all to include a *.bat in such a command chain?
2. maybe sth. must be changed in the *.bat file. This file looks as follows:

@echo off
if "%1" == "setenv" goto setenv

%comspec% /k %~dp0\SDKShell.bat setenv %1
goto exit

:setenv
@echo Setting environment for using the GDAL and MapServer tools.

if "%2"=="hideoci" goto hideoci

set ocipath=0
set _path="%PATH:;=" "%"
for %%p in (%_path%) do if not "%%~p"=="" if exist %%~p\oci.dll set ocipath=1

if "%ocipath%"=="0" goto hideoci
@echo WARNING: If you encounter problems with missing oci libraries then type:
@echo SDKShell hideoci
goto setenv2

:hideoci
@echo Hiding the OCI plugin library.
if not exist %~dp0\bin\gdal\plugins-optional mkdir %~dp0\bin\gdal\plugins-optional
if exist %~dp0\bin\gdal\plugins\ogr_OCI.dll move %~dp0\bin\gdal\plugins\ogr_OCI.dll %~dp0\bin\gdal\plugins-optional\ogr_OCI.dll
if exist %~dp0\bin\gdal\plugins\gdal_GEOR.dll move %~dp0\bin\gdal\plugins\gdal_GEOR.dll %~dp0\bin\gdal\plugins-optional\gdal_GEOR.dll

:setenv2
SET PATH=%~dp0\bin;%~dp0\bin\gdal\python\osgeo;%~dp0\bin\proj\apps;%~dp0\bin\gdal\apps;%~dp0\bin\ms\apps;%~dp0\bin\gdal\csharp;%~dp0\bin\ms\csharp;%~dp0\bin\curl;%PATH%
SET GDAL_DATA=%~dp0\bin\gdal-data
SET GDAL_DRIVER_PATH=%~dp0\bin\gdal\plugins
SET PYTHONPATH=%~dp0\bin\gdal\python\osgeo;%~dp0\bin\ms\python
SET PROJ_LIB=%~dp0\bin\proj\SHARE


:exit

Unfortunately I am a greenhorn when it comes to cmd commands and *.bat-files, so any help would be appreciated. Another option would be to rewrite the *bat in a way that all changes listed there are permanently. I have tried to do so (with setx, etc), but obviously I miss something.

regards,
Ludwig
 
Ad

Advertisements

clifford_cooley

VIP Member
Joined
Mar 8, 2009
Messages
5,063
Reaction score
1,184
Hello everybody,

I am trying to execute three commands in one go in cmd.exe, i.e.:

cd C:\xy & SDKShell & where ogr2ogr
I can only think of two possible problems.
  1. where is not a valid command
  2. you are not using the Call command "Call SDKShell.bat"

Without calling a bat from another bat, the first bat is terminated when the new one starts. Calling a bat will allow the code to return when the second bat has finished.

Type from CMD command-line "Help Call"
Code:
Calls one batch program from another.

CALL [drive:][path]filename [batch-parameters]

  batch-parameters   Specifies any command-line information required by the
                     batch program.

If Command Extensions are enabled CALL changes as follows:

CALL command now accepts labels as the target of the CALL.  The syntax
is:

    CALL :label arguments

A new batch file context is created with the specified arguments and
control is passed to the statement after the label specified.  You must
"exit" twice by reaching the end of the batch script file twice.  The
first time you read the end, control will return to just after the CALL
statement.  The second time will exit the batch script.  Type GOTO /?
for a description of the GOTO :EOF extension that will allow you to
"return" from a batch script.

<snip to shorten my comment>
 
Joined
Dec 20, 2012
Messages
4
Reaction score
0
Hi clifford_cooley,

thank you for the fast reply. Unfortunately, also cd C:\xy & CallSDKShell.bat & where ogr2ogr does not work either. "where ogr2ogr" is a valid command, as it works when I type in the commands separately (it gives me the location of ogr2ogr.exe). Do I need to change anything more than just Call "SDKShell.bat"? That might be a stupid question, but I do not understand the cmd Syntax very well yet.

I am actually working on a workaround which requires to replace the semicolons in the PATH environment variable (which are separating the different entries) by quotespacequote and set the result to a new environment variable called "-path" permanently, but the command: setx _path "%PATH:;=" "%" does only produce an error (see image in the attachment).

Still very thankful for any tipps,

Ludwig
 

clifford_cooley

VIP Member
Joined
Mar 8, 2009
Messages
5,063
Reaction score
1,184
Do I need to change anything more than just Call "SDKShell.bat"? That might be a stupid question, but I do not understand the cmd Syntax very well yet.
Unfortunately, I'm no expert either and those two suggestions where all I knew to give. If you can run the program then it should run from the bat file. As long as you are in the same folder as the program or you type in the full path to the program, it should run. The only other way would be to add the programs folder to the Path environment variable then you could start the program from anywhere. Be cautious though as running just any program could have undesirable effects, if ran in the wrong location. Adding to the Path environment variable can lead to loss of control, if you get in the habit of running programs from a different location than they are intended to run.
 

TrainableMan

^ The World's First ^
Moderator
Joined
May 10, 2010
Messages
9,343
Reaction score
1,584
I'm confused. Are you trying to do all three commands on the same line?
You mention this "cd C:\xy & SDKShell & where ogr2ogr" but then I don't see it anywhere in your batch file.

Is the actual goal to call a batch file "sdkshell" located in "C:\xy" and pass it the results of the where search as a parameter???

I'll talk about the parameter in a bit but first, you don't have to do a CD to call it, you can simply do:
Call C:\xy\SDKShell.bat

but if you do want to do it then use multiple lines:
C:
cd \xy
Call SDKShell.bat
(note you actually need 3 lines because if you aren't sure you will actually be on the C: drive then you need to add that first line as well, otherwise it will change the directory for C: but you may still be at a D: command prompt and SDKShell will not be found)

As for the parameter:
The where command works similar to a Dir command which means you can't really use it's results to pass as a parameter, it simply displays stuff on the screen and returns a 0, 1, or 2 and all you can do is check the error level for found, not found, or error.

What you would have to do is output the results to a file and then write code to parse that file for the answer you want.
where /r C:\xy ogr2ogr >> whereout.lst
if errorlevel 0 goto :foundit
goto :notfound
:foundit

then you have to read the first line in the resulting file whereout.lst which is several lines of coding I haven't done in probably over 15 years so I can't remember any more but you would set that result to a variable %p1%

REM then call the batch file:
C:
cd \xy
Call SDKShell.bat %p1%
echo Welcome back from SGKShell
REM of course if there is nothing you want to do after SDKShell then just don't use the word "CALL" and then when it goes to SDKShell it never comes back here.
goto :exit

:notfound
echo ogr2ogr Not Found

:exit
 
Last edited:
Joined
Dec 20, 2012
Messages
4
Reaction score
0
Dear trainable man, dear clifford_cooley,

thank you again for your answers. Unfortunately I have not managed to solve my problem yet. I try to give some more information:

1. The reason why I need to send the code in one line is that I generate to code from within an R-Script automatically (http://en.wikipedia.org/wiki/R_(programming_language)) Within R, there is a function that calls to cmd exe and you give the command to that function as a string. The problem is that I seems not to work to call cmd.exe from R three times to execute the tree commands cd & Call SDKShell.bat & ogr2ogr [options] as my cmd.exe seems to forget (or it is closed) between the three calls to it from within R.
2. SDKShell.bat sets the environment variables for ogr2ogr.exe for this active cmd.session (as you can see in the code of the file posted in my first post) so I cannot use the Programm if I have not called SDKShell.bat in the same session.
3. As a result, an alternative would be to rewrite the SDKShell.bat in a way that makes the setting of the environment variables permanent. So I would just run it one time and then I will not have to call it from within my R-code. I have tried to do so, below is what I have got so far. Everthing seems to work, but not the line beginning with "for" (which seems to replace the ; (semicolon) in the _path enviroment variable with " " (quotespacequote)). If I manage to do this, my permanent environment variables should look exactly as the ones in the session after a call to SDKShell.bat.

Thank you once again!
Ludwig


@echo off
if "%1" == "setenv" goto setenv

%comspec% /k %~dp0\SDKShell_permanent.bat setenv %1
goto exit

:setenv
@echo Setting environment for using the GDAL and MapServer tools.

if "%2"=="hideoci" goto hideoci

setx ocipath 0
setx _path "%PATH:;=" "%"
for %%p in (%_path%) do if not "%%~p"=="" if exist %%~p\oci.dll setx ocipath 1

if "%ocipath%"=="0" goto hideoci
@echo WARNING: If you encounter problems with missing oci libraries then type:
@echo SDKShell hideoci
goto setenv2

:hideoci
@echo Hiding the OCI plugin library.
if not exist %~dp0\bin\gdal\plugins-optional mkdir %~dp0\bin\gdal\plugins-optional
if exist %~dp0\bin\gdal\plugins\ogr_OCI.dll move %~dp0\bin\gdal\plugins\ogr_OCI.dll %~dp0\bin\gdal\plugins-optional\ogr_OCI.dll
if exist %~dp0\bin\gdal\plugins\gdal_GEOR.dll move %~dp0\bin\gdal\plugins\gdal_GEOR.dll %~dp0\bin\gdal\plugins-optional\gdal_GEOR.dll

:setenv2
SETX PATH "%~dp0\bin;%~dp0\bin\gdal\python\osgeo;%~dp0\bin\proj\apps;%~dp0\bin\gdal\apps;%~dp0\bin\ms\apps;%~dp0\bin\gdal\csharp;%~dp0\bin\ms\csharp;%~dp0\bin\curl;%PATH%"
SETX GDAL_DATA %~dp0\bin\gdal-data
SETX GDAL_DRIVER_PATH %~dp0\bin\gdal\plugins
SETX PYTHONPATH %~dp0\bin\gdal\python\osgeo;%~dp0\bin\ms\python
SETX PROJ_LIB %~dp0\bin\proj\SHARE

:exit
 
Ad

Advertisements

TrainableMan

^ The World's First ^
Moderator
Joined
May 10, 2010
Messages
9,343
Reaction score
1,584
I think you would need to have R open a file and write the lines out one by one to generate a multi-line batch file, then call that batch file.
 
Joined
Dec 20, 2012
Messages
4
Reaction score
0
Thank you, that is what I "feared" but if you say so I will just stop to pursue the "easier" solution we were discussing.

thanks and have a nice christmas!
 

clifford_cooley

VIP Member
Joined
Mar 8, 2009
Messages
5,063
Reaction score
1,184
I'm scratching my head, its been so long since I've been with the commandline.

Is it possible you are terminating the CMD within the first BAT file, which is why it doesn't return to the third command?

The EXIT command will terminate the commandline interface, regardless of how many BAT files you have calling each other.
 
Ad

Advertisements

TrainableMan

^ The World's First ^
Moderator
Joined
May 10, 2010
Messages
9,343
Reaction score
1,584
Well no matter if you could get the commands to operate all in one line, you need the where results BEFORE you call SDKShell because you need a result to then pass as a parameter. And even if you could make sure where executes first, it won't return the parameter you want, it just returns a 0, 1, or 2.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top