You know that the data file looks almost like JSON. Just for grins and giggles, I decided to load it into JScript, massage it into actual JSON, create a JScript object from it, and then output the values โโof the objects back to the batch version of the script. It is not as effective as Magoo's solution, but it was an entertaining exercise nonetheless.
@if (@ a==@b ) @end /* Harmless hybrid line that begins a JScript comment @echo off setlocal set "JSON=json.txt" for /f "delims=" %%I in ('cscript /nologo /e:JScript "%~f0" "%JSON%"') do ( rem :: If you want to do stuff with each path returned, rem :: change "delims=" to "tokens=2 delims==" above. rem :: Then for each iteration of the loop, %%I will rem :: contain a path from the text file. set "%%I" ) :: display all variables beginning with LibraryFolders set LibraryFolders goto :EOF :: end batch / begin JScript */ var fso = new ActiveXObject('scripting.filesystemobject'), JSONfile = fso.OpenTextFile(WSH.Arguments(0), 1); var JSON = JSONfile.ReadAll().split(/\r?\n/); JSONfile.close(); // massage the data into valid JSON for (var i=0; i<JSON.length; i++) { if (!i) JSON[i] += ':'; else if (/^\s*(\"[^\"]+\"\s*){2}$/.test(JSON[i])) { JSON[i] = JSON[i].replace(/\"\s+\"/, '": "') + ','; } } JSON = JSON.join('').replace(/,\s*\}/, '}'); // create new object from now valid JSON text eval('var obj = {' + JSON + '}'); // dump "var=val" out to be captured by batch for /f loop for (var i in obj.LibraryFolders) { WSH.Echo('LibraryFolders['+i+']=' + obj.LibraryFolders[i]); }
source share