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 called findstr 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

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -