0

I have folders of audio samples, and I'm about to delete what I don't use, but I'd like to have a record of the original contents in an SQLite database.

Currently, I'm writing directly to the database, but I think writing to a .CSV file and importing to a database would be faster because there are hundreds of files.

Use this batch code to create a directory tree like my file system + dummy files, for testing. It will create within wherever you execute the file. (In my case, the top folder was named Testing :

SET "BigFish=Z. Unsorted\Big Fish Audio\Midnight Jazz Train\Brass and Winds"
SET "Fragments=Z. Unsorted\Loopmasters\Fragments 02\FR2_SOUNDS_&_FX\FR2_BASS"
SET "LCycles=Z. Unsorted\Loopmasters\Hip-Hop Lunar Cycles\LUN_SYNTH_MELODY_LOOPS"

FOR %%A IN (Tenor_Sax, Trumpet) DO MD "%BigFish%\%%A"
MD "%Fragments%"
MD "%LCycles%"

@echo justtestjunk > "%Fragments%\loop.wav"
@echo justtestjunk > "%LCycles%\drum.aiff"
@echo justtestjunk > "%LCycles%\file.txt"
FOR %%A IN (Tenor_Sax, Trumpet) DO echo justtestjunk > "%BigFish%\%%A\data.sql"

With help, I've managed to create a database and make some entries, but my output looks as follows (just a sample of the first row):

loop.wav, Testing, Z. Unsorted, Loopmasters, Fragments 02, 15

When what I want is this:

loop.wav, Loopmasters, Fragments 02, FR2_SOUNDS_&FX, FR2_BASS, 15

I think the problem is with the delimiting in the code. Also, I need the code to handle the absence of a Type value (leaving it NULL)

Code:

@echo off

setlocal enabledelayedexpansion

SET fld="Z. Unsorted"

sqlite3 %fld%\TestDb.sqlite "CREATE TABLE IF NOT EXISTS \"Samples\" (File TEXT NOT NULL, Publisher TEXT, Pack TEXT NOT NULL, Category TEXT, Type TEXT, Size INT);" 

for /r %fld% %%A in (*.aiff *.wav) do (
    call :part "%%~dpA"

    set "File=%%~nxA"
    set "File=!File:'=''!"

    sqlite3 %fld%\TestDb.sqlite "INSERT OR REPLACE INTO \"Samples\" VALUES ('!File!', '!Publisher!', '!Pack!', '!Category!', '!Type!', '%%~zA');"

    @echo '!File!', '!Publisher!', '!Pack!', '!Category!', '%%~zA'
)

PAUSE

:part
    for /f "tokens=6-10 delims=\" %%A in ("%~1") do (
        set "Publisher=%%~A"
        set "Publisher=!Publisher:'=''!"

        set "Pack=%%~B"
        set "Pack=!Pack:'=''!"

        set "Category=%%~C"  
        set "Category=!Category:'=''!"

        set "Type=%%~D"  
        set "Type=!Type:'=''!"
    )
12
  • @music2myear I edited my question. I've been trying different directions and not getting very far. I'm in over my head. A starting point would be finding out how to get the separate parts of the paths. Mar 21, 2018 at 22:52
  • @music2myear I got a little further in the SQLITE database version. I created an SQLITE database in each publisher folder with the columns I want, but I don't know to fill them with data. Mar 21, 2018 at 23:27
  • I think you are referencing the tokens and variables wrong - check accepted answer for this - and remember to escape backslashes if needed - stackoverflow.com/questions/15535424/… . Also, you don't want to use a lot of different sql databases, just one with one table, since you can select by whatever column to get a subset of your data.
    – ivanivan
    Mar 22, 2018 at 3:21
  • @ivanivan I looked over the answer. Which part should help me? - and I'll apply the idea of the databases. Mar 22, 2018 at 3:44
  • You could make this question so much simpler if you asked separately A)How to make a CSV of your data Then once that is done, B)How to import the CSV into SQLite
    – barlop
    Mar 22, 2018 at 21:04

1 Answer 1

1

I cross-posted on Stackoverflow and was given a solution.

Note that the script must be placed in the same directory of the sample folder and have the sample folder structure as my example.

I put a basic, clunky explanation in the batch file.

@echo off

setlocal enabledelayedexpansion

@echo This script will create an SQLite database of .wav or .aiff samples in the folder you select
@echo( 
@echo the script must be placed in the same directory as your sample folder.
@echo(
@echo Columns will be populated based on the following file structure: 
@echo(
@echo SampleFolder\Publisher\PackName\SampleCategory (i.e. Ambience)\SampleType (i.e. Rain)\Filename.wav
@echo(

set /p "fld=What is the name of your sample folder? "

> "%fld%\SampleDb.sql" (
    echo BEGIN;
    echo CREATE TABLE IF NOT EXISTS "Samples" ^(File TEXT NOT NULL, Publisher TEXT, Pack TEXT NOT NULL, Category TEXT, Type TEXT, Size INT^);
)

for /r "%fld%" %%A in (*.aiff *.wav) do (
    set "relative_path=%%~dpA"
    set "relative_path=!relative_path:%cd%=!"
    if "!relative_path:~,1!" == "\" set "relative_path=!relative_path:~1!"

    call :part "!relative_path!"

    set "File=%%~nxA"
    set "File='!File:'=''!'"

    >> "%fld%\SampleDb.sql" echo INSERT OR REPLACE INTO "Samples" VALUES ^(!File!, !Publisher!, !Pack!, !Category!, !Type!, %%~zA^);

    @echo !File!, !Publisher!, !Pack!, !Category!, !Type!, %%~zA
)

>> "%fld%\SampleDb.sql" echo COMMIT;

sqlite3 -cmd ".read '%fld%\SampleDb.sql'" "%fld%\SampleDb.sqlite" ""

pause
exit /b

:part
    for /f "tokens=2-5 delims=\" %%A in ("%~1") do (
        set "Publisher=%%~A"
        if defined Publisher (
            set "Publisher='!Publisher:'=''!'"
        ) else set "Publisher=NULL"

        set "Pack=%%~B"
        if defined Pack (
            set "Pack='!Pack:'=''!'"
        ) else "Pack=''"

        set "Category=%%~C"
        if defined Category (
            set "Category='!Category:'=''!'"
        ) else set "Category=NULL"

        set "Type=%%~D"
        if defined Type (
            set "Type='!Type:'=''!'"
        ) else set "Type=NULL"
    )
exit /b

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .