0

I am fairly new to using batch files asides from the basics. I am trying to see if it is possible to run through 2 different loops at the same time to get my exports in ogr2ogr (postresql to geopackage, geojson etc).

I have no issues figuring out 1 loop.

For %%x IN (a,b,c) DO ogr2ogr -f "gpkg" C:\results\%%x_1.gpkg PG:"host=localhost
user=user1 dbname=testdb password=admin" -sql "SELECT * FROM %%x  WHERE columnx = '1'"

I am trying to figure out if I can get another loop in where I can cycle through columnx from my select query. ie (1,2,3). So my example above it could generate 9 geopackages a_1,a_2,a_3,b_1,b_2,b_3,c_1,c_2,c_3.

Possibly something like this but of course it doesnt work.

For %%x IN (a,b,c) & FOR %%y IN (1,2,3) DO ogr2ogr -f "gpkg" C:\results\%%x_1.gpkg 
PG:"host=localhost user=user1 dbname=testdb password=admin" -sql "SELECT * 
FROM %%x  WHERE columnx = '%%y'"

Not sure if it is possible or not? Any suggestion would be great.

Will C.
  • 41
  • 6
  • Well, "it doesn't work" is the worst error description, so please be precise; what exactly do you want, and what do you actually get? please consult [mcve]… – aschipfl Nov 20 '20 at 01:45

1 Answers1

1

The & symbol is a mechanism to run a command after the previous command finished. A FOR command ALWAYS needs a corresponding DO. That is made quite clear in the help syntax usage for the command.

In theory you could put all this code on one line but that makes it very unreadable so it is better to use parentheses to create a command block just like you were shown in one of your previous batch-file questions.

For %%x IN (a,b,c) DO (
    FOR %%y IN (1,2,3) DO (
        ogr2ogr -f "gpkg" C:\results\%%x_1.gpkg PG:"host=localhost user=user1 dbname=testdb password=admin" -sql "SELECT * FROM %%x  WHERE columnx = '%%y'"
    )
)
Squashman
  • 13,649
  • 5
  • 27
  • 36