1

I have a set of sql files

file1.sql
file2.sql
....

that contain table inserts INSERT INTO <tablename> (...) VALUES (....); in a directory. I would like to add a command into a existing batch file so that it iterates over each file. Currently I use the command

<postgreSQLCommandWithParameters> -U <user> -d <database> -f <pathToSQL>\complete.sql -q

I looked into a for loop but I missing the parameter to point to the correct directory.

for %%G in (*.sql) do <postgreSQL> -U <user>  -d <database>  -E -i"%%G"

Therefore how can I use a for loop to iterate over all sql files which contain insert commands? As a side question: And what is the syntax in the for loop in batch scripts?

4

1 Answer 1

0

So I used the following command

FOR /F "tokens=4 USEBACKQ" %%F IN (`dir <pathToSQLFiles> ^|  findstr .sql `) DO (

        <postgreSQL>  %1 -U <user> -d <database> -f  <pathToSQLFiles>\%%F -q

    )   

And here are some cliffnotes

  • "tokens=4 USEBACKQ" takes the numbered items to read from each line and also includes names with space in them
  • ^| findstr .sql filters all SQL files
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.