如何在批处理文件中递归拆分目录地址?

如何在批处理文件中递归拆分目录地址?

我有音频样本文件夹,我打算删除那些我不用的,但我想在 SQLite 数据库中记录原始内容。

目前,我正在直接写入数据库,但我认为写入文件.CSV并导入数据库会更快,因为有数百个文件。

使用此批处理代码创建一个目录树,如我的文件系统 + 虚拟文件,用于测试。它将在您执行文件的任何位置创建。(在我的例子中,顶部文件夹名为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"

在帮助下,我设法创建了一个数据库并进行了一些输入,但我的输出如下所示(仅是第一行的示例):

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

当我想要的是这个时:

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

我认为问题在于代码中的分隔。另外,我需要代码来处理值的缺失Type(保留它NULL

代码:

@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:'=''!"
    )

答案1

我在 Stackoverflow 上交叉发帖,并得到了一个办法

注意,脚本必须放在示例文件夹的同一目录中,并具有与我的示例相同的示例文件夹结构。

我在批处理文件中放了一个基本的、笨重的解释。

@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

相关内容