batch file - CMD tree to JSON with -
i've found somenthing in stack: cmd tree json need alike, showing files in subfolders children. know how change it?
just because relished challenge, thought might make solution using json methods imported javascript jscript batch + jscript hybrid. others have stated, future questions hope you'll show efforts in code.
be advised script build object containing complete recursive hierarchy in memory prior outputting anything. such, can slow. flat text solution have linked above faster. purpose in making script satisfy own curiosity. :)
syntax:
batfile.bat directory where directory optional. if omitted, script treats current directory root of 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(); (var fc = new enumerator(dir.subfolders); !fc.atend(); fc.movenext()) { var obj = {}; obj[fc.item()] = recurse(fc.item()); contents.push(obj[fc.item()]); } (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'));
Comments
Post a Comment