-4

i've found somenthing like this in stack: Cmd tree to Json I need something alike, but also showing files in subfolders as children. Does anyone know how to change it?

Community
  • 1
  • 1
chotkos
  • 171
  • 1
  • 9
  • 3
    That is not a coding question we can answer, and is dangerously close to *"please write my code for me"* – abelenky Apr 07 '17 at 16:23
  • That's a nice piece of code, got a +1 from me. You should study it and extend yourself. –  Apr 07 '17 at 16:34

1 Answers1

1

Just because I relished the challenge, I thought I might make a solution using JSON methods imported from JavaScript into JScript as a Batch + JScript hybrid. As others have stated, for future questions I hope you'll show your efforts in code.

Be advised that this script will build an object containing the complete recursive hierarchy in memory prior to outputting anything. As such, it can be slow. A flat text solution as you have linked above will probably be faster. My purpose in making this script was to satisfy my own curiosity. :)

Syntax:

batfile.bat directory

Where directory is optional. If omitted, the script treats the current directory as the root of the hierarchy.

The script:

@if (@CodeSection == @Batch) @then
@echo off & setlocal

cscript /nologo /e:JScript "%~f0" "%~1"

goto :EOF
@end

var fso = WSH.CreateObject('Scripting.FileSystemObject'),
    htmlfile = WSH.CreateObject('htmlfile'),
    JSON = tree = Array = {},
    path = WSH.Arguments(0) || '.';

htmlfile.write('<meta http-equiv="x-ua-compatible" content="IE=9" />');
JSON = htmlfile.parentWindow.JSON;
Array = htmlfile.parentWindow.Array;
htmlfile.close();

function recurse(path) {
    var dir = fso.GetFolder(path),
        contents = new Array();

    for (var fc = new Enumerator(dir.SubFolders); !fc.atEnd(); fc.moveNext()) {
        var obj = {};
        obj[fc.item()] = recurse(fc.item());
        contents.push(obj[fc.item()]);
    }

    for (var fc = new Enumerator(dir.Files); !fc.atEnd(); fc.moveNext())
        contents.push(fso.GetFileName(fc.item()));

    var obj = {};
    obj[dir] = contents;
    return obj;
}

tree = recurse(path);

WSH.Echo(JSON.stringify(tree, null, '\t'));
rojo
  • 24,000
  • 5
  • 55
  • 101