Cmd树到Json

Dvi*_*evy 0 windows tree command-line json

我有一个文件夹结构的文本文件:

+---A Momentary Lapse of Reason
+---A Saucerful of Secrets
+---Animals
+---Atom Heart Mother
+---Delicate Sound Of Thunder
+---Echoes- The Best of Pink Floyd
|   +---Echoes- The Best of Pink Floyd Disc 1
|   \---Echoes- The Best of Pink Floyd Disc 2
+---Is There Anybody out There- The Wall- Live 1980-1981 Disc 1
+---Is There Anybody out There- The Wall- Live 1980-1981 Disc 2
\---Works
Run Code Online (Sandbox Code Playgroud)

我是通过windows CMD使用tree命令收到的.我想知道是否有一种简单的方法将这种结构转换成json

对于这样的事情,手动操作并不困难,但我需要为12TB文件夹做这件事.

End*_*oro 5

好的,我对JSON不太熟悉.我查看了这个帖子并写了一个批处理脚本.如果我有所改善,请告诉我.

@echo off &setlocal
if "%~1"=="" (set "root=.") else set "root=%~1"
set "pre0=                                    "

pushd %root%
echo(data = [
call:dirtree "%CD%" "1" "1"
popd
echo(];
goto:eof

:dirtree
setlocal
call set "pre=%%pre0:~-%~2%%
set /a ccount=%~3
set /a tcount=%~2+2
set /a dcount=0
for /d %%i in (*) do set /a dcount+=1
echo( %pre%{
echo(  %pre%"type": "folder",
echo(  %pre%"name": "%~nx1",
set "fpath=%~f1"
set "fpath=%fpath:\=/%"
echo(  %pre%"path": "%fpath%",
echo(  %pre%"childno": %ccount%,
if %dcount% equ 0 echo(  %pre%"subchilds": %dcount%
if %dcount% gtr 0 (
    echo(  %pre%"subchilds": %dcount%,
    echo(  %pre%"children": [
    for /d %%i in (*) do (
        for /f %%j in ('call echo "%%dcount%%"') do (
            cd "%%i"
            call:dirtree "%%i" "%tcount%" "%%j"
            cd ..
        )
        set /a dcount-=1
    )
    echo(  %pre%]
)
if %ccount% equ 1 (echo  %pre%}) else echo( %pre%},
endlocal
goto:eof
Run Code Online (Sandbox Code Playgroud)

用法: tree2json [startfolder] [>file.txt]