I'm hoping someone may be able to help.
Dropbox stores its path in a file called 'info.json'. The contents of my file is as follows:
{"personal": {"path": "C:\\Users\\Sam\\Dropbox", "host": 241656592}}
{"business": {"path": "C:\\Common\\Allusers\\Dropbox", "host": 45143292}}
I need to extract the 'personal' path only to a Windows CMD variable %dropboxpath% using only a windows batch script. I have tried using FOR and FINDSTR but couldn't make it work.
The other issue is stripping away the extra backslashes in the path to leave 'C:\Users\Sam\Dropbox'.
I believe FINDSTR should locate the 'personal' line and FOR should extract everything after "path": but before , "host" and then somehow, remove the double backslashes.
Hope this makes sense and really appreciate any help you could give.
Many thanks, Sam
@echo off
setlocal enableextensions disabledelayedexpansion
set "file=info.json"
for /f usebackq^ tokens^=2^,6^ delims^=^" %%a in ("%file%") do (
if /i "%%a"=="personal" set "dropboxpath=%%~fb"
)
echo %dropboxpath%
Using the quotes as delimiters, split the line, so delimiters and tokens are
{"personal": {"path": "C:\\Users\\Sam\\Dropbox", "host": 241656592}}
^ ^ ^ ^ ^ ^ ^ ^
1 2 3 4 5 6 7 8 9
we request the fourth and sixth tokens into %%a
and %%b
and if %%a
is personal
, then %%b
is the path
Or, you can try with
@echo off
setlocal enableextensions disabledelayedexpansion
set "file=info.json"
for /f "delims=" %%a in ('findstr /l /i /c:"{\"personal\"" "%file%"'
) do for %%b in (%%a
) do for /f "tokens=4 delims=\:" %%c in ("%%~b"
) do set "dropboxpath=%%~fb"
echo %dropboxpath%
findstr
is used to find the required line in the input file. This line retrieved by ...for /f %%a
who called the findstr
command. For each retrieved line ...for %%b
will iterate over the elements in the line stored in %%a
and for each ...for /f %%c
will split the element using colon and backslash as delimiters and trying to find the fouth token. As the only element in the line with four tokens is the path...Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments