[Solved] How to simplify those lines via a loop For ... Do ?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Hackoo
Posts: 103
Joined: 15 Apr 2014 17:59

[Solved] How to simplify those lines via a loop For ... Do ?

#1 Post by Hackoo » 09 Feb 2016 09:21

Hello,
I want to simplify those lines via a loop For ... Do , is this possible?
We see here that the file name = the name of the bases to be loaded.

Code: Select all

@echo off 
Set User=user1
Set BaseDonnee=Test
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U %User% -d %BaseDonnee% -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.ACTID FROM 'C:\Rep\ACTID.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U %User% -d %BaseDonnee% -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.ACTIH FROM 'C:\Rep\ACTIH.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U %User% -d %BaseDonnee% -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.CBUDD FROM 'C:\Rep\CBUDD.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U %User% -d %BaseDonnee% -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.CCOUD FROM 'C:\Rep\CCOUD.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U %User% -d %BaseDonnee% -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.CCOUH FROM 'C:\Rep\CCOUH.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U %User% -d %BaseDonnee% -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.CDEVD FROM 'C:\Rep\CDEVD.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U %User% -d %BaseDonnee% -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.CFINH FROM 'C:\Rep\CFINH.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U %User% -d %BaseDonnee% -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.CIMOD FROM 'C:\Rep\CIMOD.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U %User% -d %BaseDonnee% -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.CPCED FROM 'C:\Rep\CPCED.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U %User% -d %BaseDonnee% -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.CPERD FROM 'C:\Rep\CPERD.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U %User% -d %BaseDonnee% -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.CPFTD FROM 'C:\Rep\CPFTD.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U %User% -d %BaseDonnee% -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.CPFTH FROM 'C:\Rep\CPFTH.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U %User% -d %BaseDonnee% -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.CPMTD FROM 'C:\Rep\CPMTD.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U %User% -d %BaseDonnee% -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.CTVAD FROM 'C:\Rep\CTVAD.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U %User% -d %BaseDonnee% -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.DACTD FROM 'C:\Rep\DACTD.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U %User% -d %BaseDonnee% -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.DFOND FROM 'C:\Rep\DFOND.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U %User% -d %BaseDonnee% -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.DFONH FROM 'C:\Rep\DFONH.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U %User% -d %BaseDonnee% -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.DIVID FROM 'C:\Rep\DIVID.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U %User% -d %BaseDonnee% -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.EOTPD FROM 'C:\Rep\EOTPD.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U %User% -d %BaseDonnee% -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.FONDD FROM 'C:\Rep\FONDD.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U %User% -d %BaseDonnee% -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.FONDH FROM 'C:\Rep\FONDH.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U %User% -d %BaseDonnee% -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.GACHD FROM 'C:\Rep\GACHD.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U %User% -d %BaseDonnee% -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.GMDSD FROM 'C:\Rep\GMDSD.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U %User% -d %BaseDonnee% -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.LOCID FROM 'C:\Rep\LOCID.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U %User% -d %BaseDonnee% -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.MPMTD FROM 'C:\Rep\MPMTD.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U %User% -d %BaseDonnee% -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.OACHD FROM 'C:\Rep\OACHD.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U %User% -d %BaseDonnee% -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.PAYSD FROM 'C:\Rep\PAYSD.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U %User% -d %BaseDonnee% -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.REGID FROM 'C:\Rep\REGID.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U %User% -d %BaseDonnee% -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.SOCID FROM 'C:\Rep\SOCID.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U %User% -d %BaseDonnee% -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.TFONH FROM 'C:\Rep\TFONH.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U %User% -d %BaseDonnee% -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.TIMPD FROM 'C:\Rep\TIMPD.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U %User% -d %BaseDonnee% -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.TOPED FROM 'C:\Rep\TOPED.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U %User% -d %BaseDonnee% -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.TPIED FROM 'C:\Rep\TPIED.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U %User% -d %BaseDonnee% -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.TTIED FROM 'C:\Rep\TTIED.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U %User% -d %BaseDonnee% -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.AXE1D FROM 'C:\Rep\AXE1D.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U %User% -d %BaseDonnee% -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.CFIND FROM 'C:\Rep\CFIND.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U %User% -d %BaseDonnee% -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.FIMOD FROM 'C:\Rep\FIMOD.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U %User% -d %BaseDonnee% -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.LOCMD FROM 'C:\Rep\LOCMD.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U %User% -d %BaseDonnee% -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.NATDD FROM 'C:\Rep\NATDD.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U %User% -d %BaseDonnee% -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.PROJD FROM 'C:\Rep\PROJD.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U %User% -d %BaseDonnee% -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.TFOND FROM 'C:\Rep\TFOND.TXT' DELIMITERS '|';"

I tried like this with echoing the result to a file, just to take a look for the output :

Code: Select all

@echo off
setlocal enabledelayedexpansion
mode con:cols=60 lines=6
Set Bases=Base.ACTID Base.ACTIH Base.CBUDD Base.CCOUD Base.CCOUH Base.CDEVD Base.CFINH Base.CIMOD Base.CPCED Base.CPERD Base.CPFTD Base.CPFTH Base.CPMTD Base.CTVAD Base.DACTD Base.DFOND Base.DFONH Base.DIVID Base.EOTPD Base.FONDD Base.FONDH Base.GACHD Base.GMDSD Base.LOCID Base.MPMTD Base.OACHD Base.PAYSD Base.REGID Base.SOCID Base.TFONH Base.TIMPD Base.TOPED Base.TPIED Base.TTIED Base.AXE1D Base.CFIND Base.FIMOD Base.LOCMD Base.NATDD Base.PROJD Base.TFOND

Set files=C:\Rep\ACTID.TXT C:\Rep\ACTIH.TXT C:\Rep\CBUDD.TXT C:\Rep\CCOUD.TXT C:\Rep\CCOUH.TXT C:\Rep\CDEVD.TXT C:\Rep\CFINH.TXT C:\Rep\CIMOD.TXT C:\Rep\CPCED.TXT C:\Rep\CPERD.TXT C:\Rep\CPFTD.TXT C:\Rep\CPFTH.TXT C:\Rep\CPMTD.TXT C:\Rep\CTVAD.TXT C:\Rep\DACTD.TXT C:\Rep\DFOND.TXT C:\Rep\DFONH.TXT C:\Rep\DIVID.TXT C:\Rep\EOTPD.TXT C:\Rep\FONDD.TXT C:\Rep\FONDH.TXT C:\Rep\GACHD.TXT C:\Rep\GMDSD.TXT C:\Rep\LOCID.TXT C:\Rep\MPMTD.TXT C:\Rep\OACHD.TXT C:\Rep\PAYSD.TXT C:\Rep\REGID.TXT C:\Rep\SOCID.TXT C:\Rep\TFONH.TXT C:\Rep\TIMPD.TXT C:\Rep\TOPED.TXT C:\Rep\TPIED.TXT C:\Rep\TTIED.TXT C:\Rep\AXE1D.TXT C:\Rep\CFIND.TXT C:\Rep\FIMOD.TXT C:\Rep\LOCMD.TXT C:\Rep\NATDD.TXT C:\Rep\PROJD.TXT C:\Rep\TFOND.TXT

For %%A in (%Bases%) Do ( set var1=%%A
   For %%B in (%files%) Do ( set var2=%%B
      echo "C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U %User% -d %BaseDonnee% -c "SET CLIENT_ENCODING TO 'UTF8'; COPY !var1! FROM '!var2!' DELIMITERS '|';"
   )
)>t.txt   
start "" t.txt
pause

But without sucess ?
I get as output result like this :
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U user1 -d Test -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.TFOND FROM 'C:\Rep\ACTID.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U user1 -d Test -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.TFOND FROM 'C:\Rep\ACTIH.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U user1 -d Test -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.TFOND FROM 'C:\Rep\CBUDD.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U user1 -d Test -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.TFOND FROM 'C:\Rep\CCOUD.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U user1 -d Test -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.TFOND FROM 'C:\Rep\CCOUH.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U user1 -d Test -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.TFOND FROM 'C:\Rep\CDEVD.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U user1 -d Test -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.TFOND FROM 'C:\Rep\CFINH.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U user1 -d Test -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.TFOND FROM 'C:\Rep\CIMOD.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U user1 -d Test -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.TFOND FROM 'C:\Rep\CPCED.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U user1 -d Test -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.TFOND FROM 'C:\Rep\CPERD.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U user1 -d Test -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.TFOND FROM 'C:\Rep\CPFTD.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U user1 -d Test -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.TFOND FROM 'C:\Rep\CPFTH.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U user1 -d Test -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.TFOND FROM 'C:\Rep\CPMTD.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U user1 -d Test -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.TFOND FROM 'C:\Rep\CTVAD.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U user1 -d Test -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.TFOND FROM 'C:\Rep\DACTD.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U user1 -d Test -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.TFOND FROM 'C:\Rep\DFOND.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U user1 -d Test -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.TFOND FROM 'C:\Rep\DFONH.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U user1 -d Test -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.TFOND FROM 'C:\Rep\DIVID.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U user1 -d Test -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.TFOND FROM 'C:\Rep\EOTPD.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U user1 -d Test -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.TFOND FROM 'C:\Rep\FONDD.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U user1 -d Test -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.TFOND FROM 'C:\Rep\FONDH.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U user1 -d Test -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.TFOND FROM 'C:\Rep\GACHD.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U user1 -d Test -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.TFOND FROM 'C:\Rep\GMDSD.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U user1 -d Test -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.TFOND FROM 'C:\Rep\LOCID.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U user1 -d Test -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.TFOND FROM 'C:\Rep\MPMTD.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U user1 -d Test -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.TFOND FROM 'C:\Rep\OACHD.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U user1 -d Test -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.TFOND FROM 'C:\Rep\PAYSD.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U user1 -d Test -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.TFOND FROM 'C:\Rep\REGID.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U user1 -d Test -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.TFOND FROM 'C:\Rep\SOCID.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U user1 -d Test -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.TFOND FROM 'C:\Rep\TFONH.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U user1 -d Test -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.TFOND FROM 'C:\Rep\TIMPD.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U user1 -d Test -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.TFOND FROM 'C:\Rep\TOPED.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U user1 -d Test -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.TFOND FROM 'C:\Rep\TPIED.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U user1 -d Test -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.TFOND FROM 'C:\Rep\TTIED.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U user1 -d Test -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.TFOND FROM 'C:\Rep\AXE1D.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U user1 -d Test -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.TFOND FROM 'C:\Rep\CFIND.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U user1 -d Test -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.TFOND FROM 'C:\Rep\FIMOD.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U user1 -d Test -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.TFOND FROM 'C:\Rep\LOCMD.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U user1 -d Test -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.TFOND FROM 'C:\Rep\NATDD.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U user1 -d Test -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.TFOND FROM 'C:\Rep\PROJD.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U user1 -d Test -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.TFOND FROM 'C:\Rep\TFOND.TXT' DELIMITERS '|';"

Thank you for your help !
Last edited by Hackoo on 10 Feb 2016 15:18, edited 1 time in total.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: How to simplify those lines via a loop For ... Do ?

#2 Post by Squashman » 09 Feb 2016 09:33

You seem to be over complicating this. Just use (ACTID ACTIH CBUDD etc.....) as your variable. And you don't need to use delayed expansion at all.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: How to simplify those lines via a loop For ... Do ?

#3 Post by Ed Dyreen » 09 Feb 2016 10:08

try something like this

Code: Select all

@echo off

set "$fullPathFile=C:\Program Files\PostgreSQL\9.4\bin\psql.exe"
set "$user=user1"
set "$basedonnee=Test"

set ^"$bases=^
%=   =%ACTID, ACTIH, CBUDD, CCOUD,^
%=   =%CCOUH, CDEVD, CFINH, CIMOD,^
%=   =%CPCED, CPERD, CPFTD, CPFTH,^
%=   =%CPMTD, CTVAD, DACTD, DFOND,^
%=   =%DFONH, DIVID, EOTPD, FONDD,^
%=   =%FONDH, GACHD, GMDSD, LOCID,^
%=   =%MPMTD, OACHD, PAYSD, REGID,^
%=   =%SOCID, TFONH, TIMPD, TOPED,^
%=   =%TPIED, TTIED, AXE1D, CFIND,^
%=   =%FIMOD, LOCMD, NATDD, PROJD,^
%=   =%TFOND,^
"

for %%f in ( %$bases% ) do for %%b in ( %$bases% ) do (

   echo."%$fullPathFile%" -U %$user% -d %$basedonnee% -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.%%~b FROM 'C:\Rep\%%~f.TXT' DELIMITERS '|';"
)

pause
exit

Code: Select all

....
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U user1 -d Test -c "SET CLIENT_E
NCODING TO 'UTF8'; COPY Base.TFONH FROM 'C:\Rep\TFOND.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U user1 -d Test -c "SET CLIENT_E
NCODING TO 'UTF8'; COPY Base.TIMPD FROM 'C:\Rep\TFOND.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U user1 -d Test -c "SET CLIENT_E
NCODING TO 'UTF8'; COPY Base.TOPED FROM 'C:\Rep\TFOND.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U user1 -d Test -c "SET CLIENT_E
NCODING TO 'UTF8'; COPY Base.TPIED FROM 'C:\Rep\TFOND.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U user1 -d Test -c "SET CLIENT_E
NCODING TO 'UTF8'; COPY Base.TTIED FROM 'C:\Rep\TFOND.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U user1 -d Test -c "SET CLIENT_E
NCODING TO 'UTF8'; COPY Base.AXE1D FROM 'C:\Rep\TFOND.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U user1 -d Test -c "SET CLIENT_E
NCODING TO 'UTF8'; COPY Base.CFIND FROM 'C:\Rep\TFOND.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U user1 -d Test -c "SET CLIENT_E
NCODING TO 'UTF8'; COPY Base.FIMOD FROM 'C:\Rep\TFOND.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U user1 -d Test -c "SET CLIENT_E
NCODING TO 'UTF8'; COPY Base.LOCMD FROM 'C:\Rep\TFOND.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U user1 -d Test -c "SET CLIENT_E
NCODING TO 'UTF8'; COPY Base.NATDD FROM 'C:\Rep\TFOND.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U user1 -d Test -c "SET CLIENT_E
NCODING TO 'UTF8'; COPY Base.PROJD FROM 'C:\Rep\TFOND.TXT' DELIMITERS '|';"
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U user1 -d Test -c "SET CLIENT_E
NCODING TO 'UTF8'; COPY Base.TFOND FROM 'C:\Rep\TFOND.TXT' DELIMITERS '|';"
Druk op een toets om door te gaan. . .

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: How to simplify those lines via a loop For ... Do ?

#4 Post by Aacini » 09 Feb 2016 16:18

Something like this, perhaps?

Code: Select all

@echo off
setlocal

Set User=user1
Set BaseDonnee=Test

(For %%A in (ACTID ACTIH CBUDD CCOUD CCOUH CDEVD CFINH CIMOD CPCED CPERD
             CPFTD CPFTH CPMTD CTVAD DACTD DFOND DFONH DIVID EOTPD FONDD
             FONDH GACHD GMDSD LOCID MPMTD OACHD PAYSD REGID SOCID TFONH
             TIMPD TOPED TPIED TTIED AXE1D CFIND FIMOD LOCMD NATDD PROJD TFOND) Do (
   echo "C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U %User% -d %BaseDonnee% -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.%%A FROM 'C:\Rep\%%A.TXT' DELIMITERS '|';"
)) > t.txt

start "" t.txt
pause

Antonio

Hackoo
Posts: 103
Joined: 15 Apr 2014 17:59

[Solved] How to simplify those lines via a loop For ... Do ?

#5 Post by Hackoo » 09 Feb 2016 20:03

Thank you all members here for your nice help :wink:
The problem is solved thanks to your suggestions and your help :idea:

Code: Select all

@echo off
set fullPathFile=C:\Program Files\PostgreSQL\9.4\bin\psql.exe
set ^"bases=^
%=   =%ACTID, ACTIH, CBUDD, CCOUD,^
%=   =%CCOUH, CDEVD, CFINH, CIMOD,^
%=   =%CPCED, CPERD, CPFTD, CPFTH,^
%=   =%CPMTD, CTVAD, DACTD, DFOND,^
%=   =%DFONH, DIVID, EOTPD, FONDD,^
%=   =%FONDH, GACHD, GMDSD, LOCID,^
%=   =%MPMTD, OACHD, PAYSD, REGID,^
%=   =%SOCID, TFONH, TIMPD, TOPED,^
%=   =%TPIED, TTIED, AXE1D, CFIND,^
%=   =%FIMOD, LOCMD, NATDD, PROJD,^
%=   =%TFOND,^
"
(for %%f in ( %bases% ) do (
   echo "%fullPathFile%" -U %%User%% -d %%Basedonnee%% -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.%%~f FROM 'C:\Rep\%%~f.TXT' DELIMITERS '|';"
))>OutputTest.txt
start "" OutputTest.txt
exit
Last edited by Hackoo on 10 Feb 2016 15:19, edited 1 time in total.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to simplify those lines via a loop For ... Do ?

#6 Post by foxidrive » 10 Feb 2016 06:23

Hackoo wrote:Thank you all members here for your nice help :wink:


They are good blokes! :)


I like Aacini's suggestion as it is simpler to edit and add more items.
It can be re-parsed into a list like this - it has many lines but is easy to edit.

Code: Select all

@echo off
setlocal
Set User=user1
Set BaseDonnee=Test
(For %%A in (
ACTID
ACTIH
CBUDD
CCOUD
CCOUH
CDEVD
CFINH
CIMOD
CPCED
CPERD
CPFTD
CPFTH
CPMTD
CTVAD
DACTD
DFOND
DFONH
DIVID
EOTPD
FONDD
FONDH
GACHD
GMDSD
LOCID
MPMTD
OACHD
PAYSD
REGID
SOCID
TFONH
TIMPD
TOPED
TPIED
TTIED
AXE1D
CFIND
FIMOD
LOCMD
NATDD
PROJD
TFOND
)  Do "C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U %User% -d %BaseDonnee% -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.%%A FROM 'C:\Rep\%%A.TXT' DELIMITERS '|';"
)>file.log

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: How to simplify those lines via a loop For ... Do ?

#7 Post by ShadowThief » 10 Feb 2016 13:10

With that many entries, it may be worth storing them all in a text file and reading that in from a for /f loop.

bases.txt

Code: Select all

ACTID
ACTIH
CBUDD
CCOUD
CCOUH
CDEVD
CFINH
CIMOD
CPCED
CPERD
CPFTD
CPFTH
CPMTD
CTVAD
DACTD
DFOND
DFONH
DIVID
EOTPD
FONDD
FONDH
GACHD
GMDSD
LOCID
MPMTD
OACHD
PAYSD
REGID
SOCID
TFONH
TIMPD
TOPED
TPIED
TTIED
AXE1D
CFIND
FIMOD
LOCMD
NATDD
PROJD
TFOND


Code: Select all

@echo off
setlocal
Set User=user1
Set BaseDonnee=Test
(For /f  %%A in (bases.txt)  Do "C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U %User% -d %BaseDonnee% -c "SET CLIENT_ENCODING TO 'UTF8'; COPY Base.%%A FROM 'C:\Rep\%%A.TXT' DELIMITERS '|';"
)>file.log

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: How to simplify those lines via a loop For ... Do ?

#8 Post by Squashman » 10 Feb 2016 13:43

ShadowThief wrote:With that many entries, it may be worth storing them all in a text file and reading that in from a for /f loop.

+1

Post Reply