1

I'm new to winbatch scripting. I would like to pares XML files in folder and rename them with values from three nodes. Ex: in C drive I have three files:

1.xml
2.xml
3.xml

Batch script should rename files as follows:

1.xml = AB_D_HR.xml
2.xml = ZZ_PK_IT.xml
3.xml = TS_MR_IT.xml

I reached till the point where i can get the values of two nodes however not sure about further steps. please suggest:

XML1:
<company name="AAA">
    <department>
        <depname>HR</depname>
    </department>
    <Employee>      
        <lastname>D</lastname>
        <firstname>AB</firstname>
    </Employee>
</company>

XML2:
<company name="AAA">
    <department>
        <depname>IT</depname>
    </department>
    <Employee>      
        <lastname>PK</lastname>
        <firstname>ZZ</firstname>
    </Employee>
</company>

XML3:
<company name="AAA">
    <department>
        <depname>IT</depname>
    </department>
    <Employee>      
        <lastname>MR</lastname>
        <firstname>TS</firstname>
    </Employee>
</company>
@echo off

for /f "delims=" %%T in ('dir /b *.xml') do (
   for /f "tokens=3 delims=<>" %%A in (
     'find /i "<lastname>" ^< "%%T"'
    )do set K=%%A 


for /f "tokens=3 delims=<>" %%B in (
     'find /i "<firstname>" ^< "%%T"'
    )do set L=%%B

)

echo %K%_%L%


pause
Compo
  • 36,585
  • 5
  • 27
  • 39
Lucky
  • 23
  • 4

2 Answers2

0

check xpath.bat:

@echo off

setlocal enableDelayedExpansion
for  %%T in (*.xml) do (
   for /f "tokens=* delims=" %%A in (
     'xpath.bat "%%T" "//lastname"'
    ) do set "K=%%A" 


   for /f "tokens=* delims=" %%A in (
     'xpath.bat "%%T" "//firstname"'
    ) do set "L=%%B"

Echo !L! !K!

)

Im not sure how direct renaming will work.May be you should first rename the files with different extension in order to avoid collisions.

npocmaka
  • 55,367
  • 18
  • 148
  • 187
0

Depending upon your filenames, and potentially their contents, the following may be a possibility.

@Echo Off
SetLocal EnableDelayedExpansion
Set "i=0"
For /F "Tokens=1,4 Delims=:<>" %%A In (
    'Findstr/IC:"<depname>" /C:"<lastname>" /C:"<firstname>" "*.xml"') Do (
    Set/A "i+=1"
    If !i! Equ 1 (Set "_=%%B%%~xA") Else (If !i!==2 (Set "_=%%B_!_!"
        ) Else (Ren "%%A" "%%B_!_!"
            Set "i=0")))
Timeout -1
GoTo :EOF
Compo
  • 36,585
  • 5
  • 27
  • 39