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

Popular posts from this blog

javascript - Confirm a form & display message if form is valid with JQuery -

Retrieving ETA (estimated time of arrival) with Google Distance Matrix API and public transit as transport mode -

android - ConstraintLayout: Realign baseline constraint in case if dependent view visibility was set to GONE -