4

I'm trying to run a ogr2ogr command with cmd. When I run the cmd the Osgeow command prompt just flashes and nothing happens. When I enter my ogr2ogr script directly to the Command Prompt everythinh works fine. My cmd script looks like this:

@ECHO OFF
CALL C:\OSGeo4W64\OSGeo4W.bat ogr2ogr -sql "SELECT * FROM 'D:\Testi2\own.SHP'.own WHERE Owner like 'Harr%'" D:\Testi2\own.shp D:\Testi2\All.SHP

What could be the problem?

  • does it work when you type it at the command line? I suspect you need a return between the bat file and ogr2ogr – Ian Turton Dec 13 '19 at 17:49
  • Hi. Yes, it does work when I type it directly to cmd. What do you mean by 'return'? – woundari Dec 13 '19 at 17:58
7

To debug the command, it can be helpful to echo it instead of running it, to see if any special characters are throwing things off. From inside a bat file, do:

echo ogr2ogr -sql "SELECT * FROM 'D:\Testi2\own.SHP'.own WHERE Owner like 'Harr%'" D:\Testi2\own.shp D:\Testi2\All.SHP

Which will print:

ogr2ogr -sql "SELECT * FROM 'D:\Testi2\own.SHP'.own WHERE Owner like 'Harr\Testi2\own.shp D:\Testi2\All.SHP

Notice the end of the command is getting screwed up starting at the "%" symbol. Turns out that from within a batch file, you have to escape it by doubling it (but not so on the command line!). So the command becomes:

ogr2ogr -sql "SELECT * FROM 'D:\Testi2\own.SHP'.own WHERE Owner like 'Harr%%'" D:\Testi2\own.shp D:\Testi2\All.SHP

And echoing it looks okay now.

And a note about setting up batch files that leverage OSGeo utils-- if you look in the OSGeo4W.bat, you'll see this towards the top:

call "%~dp0\bin\o4w_env.bat"

That's what actually sets up the environment. It can simplify things by leaning on that instead:

@echo off

REM set up the environment
call C:\OSGeo4W64\bin\o4w_env.bat

REM anything from here on out will run in that context
ogr2ogr -sql "SELECT * FROM 'D:\Testi2\own.SHP'.own WHERE Owner like 'Harr%%'" D:\Testi2\own.shp D:\Testi2\All.SHP

@echo on
  • Hi. It's still doing the same thing. Somehow it doesnt understand the sql when its being run via .bat file. – woundari Dec 13 '19 at 18:13
  • Ahh, it was the "%" symbol. I updated my response to answer your actual question :p – mikewatt Dec 13 '19 at 18:41
  • It works now. Thank you. – woundari Dec 13 '19 at 19:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.