Reputation: 5
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
Upvotes: 0
Views: 246
Reputation: 6042
As you need to modify and evaluate your variables at run time and not at parse time, you will need to use EnableDelayedExpansion
and !...!
instead of %...%
. However, this code will work if the two lines you've posted are correct.
@ECHO OFF
SETLOCAL EnableDelayedExpansion
FOR /F "tokens=*" %%a IN (scanword.txt) DO (
SET line=%%a
IF "!line:~2,8!"=="personal" SET dropboxpath=%%a
)
FOR /F "tokens=3" %%a IN ("!dropboxpath!") DO (
SET dropboxpath=%%a
)
SET dropboxpath=!dropboxpath:"=!
SET dropboxpath=!dropboxpath:,=!
SET dropboxpath=!dropboxpath:\\=\!
ECHO !dropboxpath!
PAUSE
The path will be stored in !dropboxpath!
and not in %dropboxpath%
as you requested but I hope that's ok ;-)
Upvotes: 0
Reputation: 70933
@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...Upvotes: 1
Reputation: 80033
@ECHO OFF
SETLOCAL
FOR /f "tokens=1,3*delims={:" %%a IN (q29817666.txt) DO IF /i "%%~a"=="personal" (
FOR %%p IN (%%c) DO SET "dropboxpath=%%~p"&GOTO process
)
:process
SET "dropboxpath=%dropboxpath:\\=\%"
ECHO dropboxpath=%dropboxpath%
GOTO :EOF
I used a file named q29817666.txt
containing your data for my testing.
The tokens/delimiters combination selects "personal"
to %%a, a space to
%%band
"C:\Users\Sam\Dropbox", "host": 241656592}}to
%%c`
if %%a
(stripped of enclosing quotes) is equal to the string personal
.
%%c
is then processed as a series of elements separated by spaces and commas. Since the first element is of interest and it's quoted, then %%p
will acquire "C:\\Users\\Sam\\Dropbox"
and this, stripped of enclosing quotes will be assigned to the variable dropbox
and the loop is then terminated by the goto
.
Then replace double-backslashes with single. Done.
Upvotes: 1