1

I am trying to execute pgsql2shp for a remote PostgreSQL db using batch file using the below code

for /f %%i in ('psql -U ihadmin -d ih_gis_production -h <xxxxx> -c "select f_table_name from geometry_columns where f_table_schema='mls_dataset';"') do (
set file_name=D:/Processing/DB_Dump/airdrie/mls_datatset/%%i
set table_name=%%i
pgsql2shp -h <xxxxx> -f %file_name% -u ihadmin -P xxxxx ih_gis_production mls_dataset.%table_name%
)

This will get table names from from the above select query and loop and export those tables to shapefile. When I run this from batch file, I am seeing pgsql2shp help options in command prompt. Any correct way to call pgsql2shp via batch?

Vince
20.5k16 gold badges49 silver badges65 bronze badges
asked May 15, 2020 at 14:14

1 Answer 1

2

Variable assignation/retrieval within a loop should be done differently, as explained here.

As it is, %table_name% is always null. You must use setlocal enabledelayedexpansion and retreive it with !table_name!

rem ENABLEDELAYEDEXPANSION : allow creating variables in FOR loop
setlocal enabledelayedexpansion 
for /f %%i in ('psql -U ihadmin -d ih_gis_production -h <xxxxx> -c "select f_table_name from geometry_columns where f_table_schema='mls_dataset';"') do (
 set file_name=D:/Processing/DB_Dump/airdrie/mls_datatset/%%i
 set table_name=%%i
 echo !table_name!
 pgsql2shp -h <xxxxx> -f !file_name! -u ihadmin -P xxxxx ih_gis_production mls_dataset.!table_name!
)
answered May 15, 2020 at 18:33
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.