Search code examples
xmlbatch-filecmdfindstr

Batch looping through directory find all XML and replace a certain (unknown) value with another (known) value


Just a question, I don't know if this is possible but let me explain it by example.

XML (containing several values)

<WORKFLOW DESCRIPTION ="" ISENABLED ="YES" ISRUNNABLESERVICE ="NO" ISSERVICE ="NO" ISVALID ="YES" NAME ="DIMENSIONS" REUSABLE_SCHEDULER ="NO" SCHEDULERNAME ="Scheduler" SERVERNAME ="SERVER_NAME_DUMMY" SERVER_DOMAINNAME ="Domain_DUMMY" SUSPEND_ON_ERROR ="NO" TASKS_MUST_RUN_ON_SERVER ="NO" VERSIONNUMBER ="1">

I want to be able to catch with findstr (unless there is a better way) SERVERNAME = (this occurs only once in the whole document) and replace the value of it by SERVER_NAME_PROPER to get the following result:

SERVERNAME ="SERVER_NAME_PROPER".

The problem is I don't know the value of SERVERNAME so i cant strictly search on SERVER_NAME_DUMMY (i don't know the length either, i saw string manipulation by character sets). Also the lines just continues with other values and elements after it (no end line manipulation). Maybe it is wise to search for SERVERNAME =(promt value) and stop before SERVERDOMAIN_NAME. Because the value is after SERVERNAME= "VALUE" and before SERVERDOMAIN_NAME.

I know that after SERVERNAME = the value needs to be replaced by my hardcoded value. If i can preform this then i can make in batch a FOR command to loop all files in the whole directory and preform this certain command.


Solution

  • I posted a Replace script here 2 days ago If text string contains certain words, wrap those words in a span tag.

    Findstr

    "^(SERVERNAME=\x22^)^([a-z]+^)^(\x22^)"
    

    Replace with

    $1WHATEVERYOUWANT$3
    

    $1 is first set brackets, $2 second, and $3 third. Above carets are escaping it for CMD - it looks like this after parsing (SERVERNAME=")([a-z]+)(")