0

I have backed up the network drive to an xml file in the format below.

<drive>
    <drive letter>X</drive letter>
    <drive path>\\DANIEL-HP\Users\Public\Documents\Downloaded Data Sheets</drive path>
</drive>

<drive>
    <drive letter>Y</drive letter>
    <drive path>\\DANIEL-HP\Users\Public</drive path>
</drive>

I want to run a batch or cmd file to extract the drive letter and path from between the delimiters and then map them.

For simplicity sake let's ignore whether other drives are mapped or not.

Delimiters for drive letter are <drive letter> and </drive letter> . Delimiters for drive path are <drive path> and </drive path>

I am not sure on how to parse the / <> symbols.

DannyBoi
  • 636
  • 1
  • 7
  • 23
  • I meant to post this on super user, is it okay to leave it here? – DannyBoi Jul 30 '16 at 02:33
  • You could try `powershell "([xml]((gc test.xml) -replace 'drive')).selectNodes('//drive') | %{ net use \"$($_['letter'].innerText):\" $_['path'].innerText }"` from the cmd prompt (replacing `test.xml` with the name of your xml file). That'll only work if your XML has a proper `` declaration and a proper root node though. – rojo Jul 30 '16 at 03:26

2 Answers2

2

That's quite straight with a simple for loop:

for /f "tokens=3 delims=<>" %%a in ('find "<drive letter>" test.xml') do echo %%a

No need to escape > and <, because they are safe within the quotes.

edit
build up two "arrays" (1) for letter and path, then join them to get the desired result:

@ECHO off
setlocal enabledelayedexpansion
REM get drives:
set c=0
for /f "tokens=3 delims=<>" %%a in ('find "<drive letter>" t.xml') do (
  set /a c+=1
  set drv-!c!=%%a
)
REM set paths:
set c=0
for /f "tokens=3 delims=<>" %%a in ('find "<drive path>" t.xml') do (
  set /a c+=1
  set pth-!c!=%%a
)

for /l %%x in (1,1,%c%) do echo !drv-%%x! !pth-%%x!

(1) quoted because of the comments on this answer

Community
  • 1
  • 1
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • `test.xml` is causing problems so renamed it to `test.txt` Thanks, that puts me in the right direction. `for /f "tokens=3 delims=<>" %%a in ('find "" test.txt' ) do echo %%a for /f "tokens=3 delims=<>" %%b in ('find "" test.txt') do echo %%b` gives me a result `X Y \\DANIEL-HP\Users\Public\Documents\Downloaded Data Sheets \\DANIEL-HP\Users\Public` I am working on getting the result as `X \\DANIEL-HP\Users\Public\Documents\Downloaded Data Sheets Y \\DANIEL-HP\Users\Public` and then dynamically assign to variables. – DannyBoi Jul 31 '16 at 01:34
  • +1 Thanks @Stephan Perfect answer. Gather the data first then display it ....nice !!!! Finally, concluded my code with `net use !drv-%%x! "!pth-%%x!" /p:yes` brilliant :) Cleared some of my doubts with arrays too. – DannyBoi Jul 31 '16 at 08:31
  • the file extension shouldn't make any difference (a textfile is a textfile, no matter how you name it). Please check for a possible typo. – Stephan Jul 31 '16 at 09:03
1

Simple using JREPL.BAT - a regular expression find/replace utility. It is pure script (hybrid batch/JScript) that runs natively on any Windows machine from XP onward, without the need of any 3rd party exe files.

Disclaimer - Ideally, you should be using an xml parser to read the file. But assuming the file always has an xml layout as you show, then the following should work from the command line.

jrepl "<(drive letter)>(.*?)</\1>\s*<(drive path)>(.*?)</\3>" "$2+': = '+$4" /m /jmatch /f test.xml

Here is the output if I put your sample xml in "test.xml"

X: = \\DANIEL-HP\Users\Public\Documents\Downloaded Data Sheets
Y: = \\DANIEL-HP\Users\Public

I assume you want the values as variables within a batch script. You can use a FOR /F loop to process the paired values:

@echo off
for /f "delims=| tokens=1*" %%A in (
  'jrepl "<(drive letter)>(.*?)</\1>\s*<(drive path)>(.*?)</\3>" "$2+'|'+$4" /m /jmatch /f test.xml'
) do (
  echo Drive letter = %%A
  echo Drive path = %%B
  echo(
)

And the outptut:

Drive letter = X
Drive path = \\DANIEL-HP\Users\Public\Documents\Downloaded Data Sheets

Drive letter = Y
Drive path = \\DANIEL-HP\Users\Public
dbenham
  • 127,446
  • 28
  • 251
  • 390