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


0

我有音频样本的文件夹,我即将删除我不使用的内容,但我想在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

我认为问题在于代码中的分隔 。 另外,我需要代码来处理缺少a 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:'=''!"
    )

@ music2myear我编辑了我的问题。我一直在尝试不同的方向而不是走得很远。我在我脑海里。一个起点是找出如何获得路径的独立部分。
Stack Johan

@ music2myear我在SQLITE数据库版本中得到了更多。我在每个发布者文件夹中创建了一个包含我想要的列的SQLITE数据库,但我不知道用数据填充它们。
Stack Johan

我认为你引用了令牌和变量错误 - 检查接受的答案 - 并记住如果需要逃避反斜杠 - stackoverflow.com/questions/15535424/... 。此外,您不希望使用许多不同的sql数据库,只需使用一个表,因为您可以选择任何列来获取数据的子集。
ivanivan

@ivanivan我看了看答案。哪部分应该对我有帮助? - 我会应用数据库的想法。
Stack Johan

如果你单独询问,你可以使这个问题变得更简单了A)如何制作数据的CSV然后一旦完成,B)如何将CSV导入SQLite
barlop

Answers:


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
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.