Batchfile - extract path from file contents into cmd variable -
i'm hoping may able help.
dropbox stores path in file called 'info.json'. contents of file follows:
{"personal": {"path": "c:\\users\\sam\\dropbox", "host": 241656592}} {"business": {"path": "c:\\common\\allusers\\dropbox", "host": 45143292}}
i need extract 'personal' path windows cmd variable %dropboxpath% using windows batch script. have tried using , findstr couldn't make work.
the other issue stripping away backslashes in path leave 'c:\users\sam\dropbox'.
i believe findstr should locate 'personal' line , should extract after "path": before , "host" , somehow, remove double backslashes.
hope makes sense , appreciate give.
many thanks, sam
@echo off setlocal enableextensions disabledelayedexpansion set "file=info.json" /f usebackq^ tokens^=2^,6^ delims^=^" %%a in ("%file%") ( if /i "%%a"=="personal" set "dropboxpath=%%~fb" ) echo %dropboxpath%
using quotes delimiters, split line, delimiters , tokens are
{"personal": {"path": "c:\\users\\sam\\dropbox", "host": 241656592}} ^ ^ ^ ^ ^ ^ ^ ^ 1 2 3 4 5 6 7 8 9
we request fourth , sixth tokens %%a
, %%b
, if %%a
personal
, %%b
path
or, can try
@echo off setlocal enableextensions disabledelayedexpansion set "file=info.json" /f "delims=" %%a in ('findstr /l /i /c:"{\"personal\"" "%file%"' ) %%b in (%%a ) /f "tokens=4 delims=\:" %%c in ("%%~b" ) set "dropboxpath=%%~fb" echo %dropboxpath%
findstr
used find required line in input file. line retrieved ...for /f %%a
calledfindstr
command. each retrieved line ...for %%b
iterate on elements in line stored in%%a
, each ...for /f %%c
split element using colon , backslash delimiters , trying find fouth token. element in line 4 tokens path...- we set variable full path of element