Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows Cmd树到Json_Windows_Json_Command Line_Tree - Fatal编程技术网

Windows Cmd树到Json

Windows Cmd树到Json,windows,json,command-line,tree,Windows,Json,Command Line,Tree,我有一个文件夹结构的文本文件: +---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 Pi

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

+---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
命令从
windowscmd
收到它。 我想知道是否有一种简单的方法可以将这个结构转换成
json


对于这样的东西,手动执行并不困难,但我需要为
12TB
文件夹执行此操作。

好的,我不太熟悉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

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

漂亮!我所要做的就是将
添加到所有的名字中(例如
echo(%pre%type:“folder”,
=
echo(%pre%“type):“folder”,
),我需要运行一个“replace
` with
/”。非常感谢您:当您执行
“%~f1”操作时,请将
\
\
替换为
**
它给出了文件夹的路径。该路径有
\
,显然
Json
不喜欢它。注意:POK,我可以更改此路径并添加缺少的双引号(需要一些时间,我现在在平板电脑上)……)