Cmd树到Json [英] Cmd tree to Json

查看:339
本文介绍了Cmd树到Json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a text file with a folder structure:

+---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

我使用tree命令从windows CMD收到了它. 我想知道是否有一种简单的方法可以将该结构转换为json?

I received it from windows CMD using the tree command. I wanted to know if there is a simple way to convert this structure into json?

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

For something like this it isn't too hard to do it manually, but I need to do it for a 12TB folder.

推荐答案

好的,我对JSON不太熟悉.我查看了线程并编写了一个批处理脚本.如果可以改善,请告诉我.

OK, I'm not so familiar with JSON. I looked at this thread and wrote a batch script. Please let me know, if I can something improve.

@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

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

这篇关于Cmd树到Json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆