Ver Mensaje Individual
  #7 (permalink)  
Antiguo 04/04/2013, 15:08
jsanchezt
 
Fecha de Ingreso: abril-2013
Ubicación: Ciudad de México
Mensajes: 4
Antigüedad: 11 años, 1 mes
Puntos: 1
Respuesta: Importar Varios archivos csv a Postgresql

Que tal nuevamente yo. Les comento ya resolvi mi duda de importar varios archivos csv a una tabla en postgres a la vez, dejo la solucion que realice por si mas adelante alguien la necesita.

Realice dos archivos .bat uno llamado principal y otro llamado anidado.

ARCHIVO principal.bat

for /R C:\Users\LAP-52\Documents\ %%i IN (*.CSV) DO (
@rem
anidado.bat C:\Users\LAP-52\Documents\%%~nxi
MOVE %%i C:\Users\LAP-52\Documents\TEMP
)

--------------------------------------------------------------------------------------------------------

ARCHIVO anidado.bat:

@echo off

SET server=localhost
SET /P server="Server [%server%]: "

SET database=postgres
SET /P database="Database [%database%]: "

SET port=5432
SET /P port="Port [%port%]: "

SET username=postgres
SET /P username="Username [%username%]: "

for /f "delims=" %%a in ('chcp ^|find /c "932"') do @ SET CLIENTENCODING_JP=%%a
if "%CLIENTENCODING_JP%"=="1" SET PGCLIENTENCODING=SJIS
if "%CLIENTENCODING_JP%"=="1" SET /P PGCLIENTENCODING="Client Encoding [%PGCLIENTENCODING%]: "

SET cadena="\copy portafolio FROM '%1%' WITH DELIMITER AS ',' CSV HEADER;"

REM Run psql
"C:\Program Files\PostgreSQL\9.2\bin\psql.exe" -h %server% -U %username% -d %database% -p %port% -c %cadena%

--------------------------------------------------------------------------------------------------------
NOTA: Se pueden establecer los parametros de autenticacion a postgres por default, solo que en el ejemplo se los deje como estan en las variables de entorno.

ej:

"C:\Program Files\PostgreSQL\9.2\bin\psql.exe" -h "192.168.XXX.XXX" -U "Miusuario" -d "MibaseDatos" -p "Puerto" -c %cadena%


Si hay alguna sugerencia para mejorar este codigo comentenmela porfavor al igual si surgen dudas.

Saludos.