Home > Net >  Unable to export shapefile for each date using pgsql2shp on Windows 10
Unable to export shapefile for each date using pgsql2shp on Windows 10

Time:06-21

I have a table in my PostgreSQL 11 database, same linestring geometry but different attributes (hourly data) for each date. I am trying to export the attributes for each date into a separate shapefile. I am on Windows 10 (x64) PC and trying to use the pgsql2shp utility.

The following line of code works (assume I want first 50 dates so 1..50), but it outputs the whole table (1..50.shp) instead of separate tables for each date:

for %i in (1..50) ; do pgsql2shp -f %i.shp -h localhost -p my_port -u postgres -P my_code postgres public.my_table '"select * from my_table where date = $i"'; done

How can I fix the above line to export shapefiles for each date?

CodePudding user response:

for /L %i in (1,1,50) do pgsql2shp -f %i.shp -h localhost -p my_port -u postgres -P my_code postgres public.my_table '"select * from my_table where date = %i"'

Notes :

The for /L syntax as mentioned by Stephan.

The semicolon is seen as a separator, same as Space

The $i in the SQL statement should be %i to substitute %i's value.

The ; done I don't know about - it may be required.

If this is executed as a line in a batch file, each %i needs to be %%i. That might save a whole lot of error-prone re-typing.

  • Related