Search code examples
xmlbatch-filecmddosrename

change folder name after find text in *.oeaccount file


I have *.oeaccount files (XML files) in many folders. I want a batch file read the text in the XML file and rename the folder that contain the file.

The xml files contain text similar to the following:

    <SMTP_Display_Name type="SZ">[email protected]</SMTP_Display_Name>
    <SMTP_Email_Address type="SZ">[email protected]</SMTP_Email_Address>
    <SMTP_Split_Messages type="DWORD">00000000</SMTP_Split_Messages>
</MessageAccount>

The batch must find [email protected] in this line and ignore all other mail:

    <SMTP_Email_Address type="SZ">[email protected]</SMTP_Email_Address>

At the beginning of the line there are 4 spaces.

The batch must extract the e-mail address [email protected] and rename the folders, every folder as the text in the XML file.

The folders tree is like this:

  • folder / 1.oeaccount
  • folder / 2.oeaccount
  • folder / 3.oeaccount

Someone gave me this code but it doesn't work:

@echo off
setlocal
pushd "YourRootLocation"
set "search=<SMTP_Email_Address .*>[^ ][^ ]*@[^ ][^ ]*\.[^ ][^ ]*</SMTP_Email_Address>"
for /f "eol=: delims=" %%A in (
  'findstr /srmbc:"%search%" file1.txt^|sort /r'
) do for /f "tokens=2 delims=<>" %%B in (
  'findstr /rbc:"%search%" "%%A"'
) do if exist "%%A" for %%F in ("%%A\..") do if "%%~fF" neq "%CD%" (
  echo ren "%%~fF" "%%B"
  ren "%%~fF" "%%B"
)
popd

Solution

  • If I understand you correctly you want to rename the parent folder of each *.oeaccount file to the e-mail address found in that file. For that something like this should work:

    for /r "C:\root\folder" %%f in (*.oeaccount) do (
      for /f "delims=<> tokens=3" %%a in (
        'findstr "<SMTP_Email_Address" "%%~ff"'
      ) do (
        ren "%%~dpf" "%%~a"
      )
    )
    

    However, beware that, although it should work in this particular case, line-oriented processing of XML files (which is what findstr does) usually is not a good idea, because

    <SMTP_Email_Address type="SZ">[email protected]</SMTP_Email_Address>
    

    is just as valid XML as

    <SMTP_Email_Address
        type="SZ">[email protected]</SMTP_Email_Address>
    

    so re-wrapping of lines may foil your search patterns.