Batchfile - extract path from file contents into cmd variable

BLS

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

MC ND
@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...
  • we set the variable to the full path of the element

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Extract file name from path, no matter what the os/path format

From Java

How can I extract the folder path from file path in Python?

From Dev

Extract absolute file path from a QRessource loaded file

From Dev

Extract file path from android Intent.ACTION_SEND

From Dev

Extract a file name from the full file path

From Dev

How to update system PATH variable permanently from cmd?

From Dev

Grep IP address from variable containing file contents outside the if statement

From Dev

Batchfile: read last lines from logfiles and copy them to a new file

From Dev

Put a file on FTP site with contents from string variable (no local file)

From Dev

Printing a path to a file from a variable in C++

From Dev

Extract date from file path in java

From Dev

Extract just file path from string

From Dev

How to extract the file name from a file path?

From Dev

file path in variable from user input

From Dev

Extract file name from path in awk program

From Dev

How to update system PATH variable permanently from cmd?

From Dev

Batchfile: read last lines from logfiles and copy them to a new file

From Dev

Extract the contents from CSV into an array

From Dev

Extract information from both the filename and the contents of the file

From Dev

extract xml tag value from the file to Variable

From Dev

Running a .vbs file from .bat with variable path

From Dev

Extract contents of a file using ContentHandler

From Dev

Bash extract string from file to use as variable

From Dev

Extract file extension from URL path in R

From Dev

How to read a .txt file from terminal, and then extract contents from file?

From Dev

Read variable values from file in CMD

From Dev

extract path to directory from path to file

From Dev

Extract folder contents from file.tar using PHP

From Dev

Remove unwanted directory from PATH using cmd file

Related Related

  1. 1

    Extract file name from path, no matter what the os/path format

  2. 2

    How can I extract the folder path from file path in Python?

  3. 3

    Extract absolute file path from a QRessource loaded file

  4. 4

    Extract file path from android Intent.ACTION_SEND

  5. 5

    Extract a file name from the full file path

  6. 6

    How to update system PATH variable permanently from cmd?

  7. 7

    Grep IP address from variable containing file contents outside the if statement

  8. 8

    Batchfile: read last lines from logfiles and copy them to a new file

  9. 9

    Put a file on FTP site with contents from string variable (no local file)

  10. 10

    Printing a path to a file from a variable in C++

  11. 11

    Extract date from file path in java

  12. 12

    Extract just file path from string

  13. 13

    How to extract the file name from a file path?

  14. 14

    file path in variable from user input

  15. 15

    Extract file name from path in awk program

  16. 16

    How to update system PATH variable permanently from cmd?

  17. 17

    Batchfile: read last lines from logfiles and copy them to a new file

  18. 18

    Extract the contents from CSV into an array

  19. 19

    Extract information from both the filename and the contents of the file

  20. 20

    extract xml tag value from the file to Variable

  21. 21

    Running a .vbs file from .bat with variable path

  22. 22

    Extract contents of a file using ContentHandler

  23. 23

    Bash extract string from file to use as variable

  24. 24

    Extract file extension from URL path in R

  25. 25

    How to read a .txt file from terminal, and then extract contents from file?

  26. 26

    Read variable values from file in CMD

  27. 27

    extract path to directory from path to file

  28. 28

    Extract folder contents from file.tar using PHP

  29. 29

    Remove unwanted directory from PATH using cmd file

HotTag

Archive