0

First, I'm reallly beginner at batch or cmd syntax. So my syntax looks very foolish, please read this with a broad mind. Thx!

I want to get a value from my specific local xml file and i want to get a part of that value. Because, I have to compare with that value and specific other value.

well, I found it with all my effort. But I couldn't 100%. Please read this code.

fc /b "C:\e\e.xml" "C:\Users\%username%\Documents\e\e.xml" return errorlevel

set usr_name=%username:~-2,2%
set _name=

IF %ERRORLEVEL% EQU 0 (         
    for /f "tokens=2delims=<>" %%i in ('find "KEY" "c:\e\e.xml"') do echo %%i
    REM echo ^<username^>%usr_name%^</Setup_Date^>^<substr^>%%i^</substr^> > "C:\e\test.xml"    
            
    echo ^<username^>%usr_name%^</username^>^<substr^>%_name%^</substr^> > "C:\e\test.xml"
    if %%i:~-4,2 == %usr_name% (
    
    REM echo ^<username^>%usr_name%^</username^>^<substr^>%%i:~-4,2%^</substr^> > "C:\e\test.xml"
    echo YES
    ELSE (
echo NO
cscript no.vbs  
)

And this is my xml file. I want to get D4 of KEY tag (D4, It occupies 2 letters from the 4th from the back.)

<Date>2020-08-13</Date><Setup>Zs#ol9fEV9pA</Setup>
<KEY>WY(T5XD4h4<KEY>

the problem is this

REM echo ^<username^>%usr_name%^</Setup_Date^>^<substr^>%%i^</substr^> > "C:\e\test.xml"    
            
echo ^<username^>%usr_name%^</username^>^<substr^>%_name%^</substr^> > "C:\e\test.xml"

if value i = abcdef I can get the value from xml file abcdef, using REM sentence. But I can't get the part of variable i.

when i use second sentence or other expressions such as %i:~-4,2% etc. (because i want to compare using that part of value. %i:~-4,2% = cd) I can't print any values, just print nothing.

Is there a way to solve this problem? As i said earlier, I have a very tiny knowledge about batch, So let me know everything you know. Or if this way couldn't solve without using other library or xpath.bat, please tell me as I have to find another way.

Thank you.

8
  • Does it have to be a batch file? Could it also be VBScript? Or PowerShell? (Doing this correctly with on-board tools in batch is needlessly hard, and I would advise using something else, if at all possible.) Commented Aug 13, 2020 at 6:29
  • Also, include the XML (or a condensed sample of it) and point out exactly which parts you want to extract. Commented Aug 13, 2020 at 6:32
  • @Tomalak added my xml format. And I prefer VBScript, and if possible I've never thought about powershell, but it looks good. Please let me know if you have any suggestions. Commented Aug 13, 2020 at 6:39
  • Then let's do VBScript, since you already seem to be using it. Please add more of the XML - a complete file, not just two elements, but you can remove repetitive parts as long as the file still works afterwards. Commented Aug 13, 2020 at 6:42
  • 1
    It can be simple, there is no problem with that. But what you are showing here is not XML. And until you either turn it into XML (it's missing a top-level element) or decide to use a less restrictive file format there is no way to move this question forward. Commented Aug 13, 2020 at 7:12

1 Answer 1

1

%%i is only defined inside the for loop. As you wrote it, the whole loop consists of only echo %%i. Then the loop ends and %%i isn't defined anymore.

As others already noted, cmd isn't really a good tool to work with when it comes to XML files (and look-alikes).

But if you are willing to use an external tool, it gets easy. Just a simple REGEX search for a string between <KEY> and <KEY> (shouldn't the second be </KEY>?):

for /f "delims=" %%a in ('^<"c:\e\e.xml jrepl ".*<KEY>(.*)<KEY>.*" "$1" /a') do set "key=%%a"
echo Key: %key%, Subkey:%key:~-4,2%

jrepl.bat was programmed by dbenham.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.