0

I have many .db sqlite3 files. Files are in separated subfolders and most of them have same file name: base.db. Every file contains a table 'main' and I want to extract table from every .db file to separate csv file, so that every csv is extracted in the same subfolder next to .db file.

I tried with this script

cd C:\sqlite-tools-win32-x86
for /R %%G in (*.db) do sqlite3 -csv -header "%%G" "select * from main" > %%~nG.csv

The problem is, this script is reading .db files from subfolders, but extract csv's in root folder, and also overwrites previous csv file. I hope the explanation is clear. Any idea how to fix it?

0

1 Answer 1

2

You are redirecting the output to %%~nG, which is the base name because of the ~n modifier. To get also the full path to the parent directory use also ~d and ~p:

cd /D "C:\sqlite-tools-win32-x86"
for /R %%G in (*.db) do (
    sqlite3 -csv -header "%%~G" "select * from main" > "%%~dpnG.csv"
)

I also replaced cd by cd /D in order to also change the drive just in case.

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.