I suggest the following batch file for this task:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "SourceDir=E:\Test"
for /F "eol=| delims=" %%I in ('dir "%SourceDir%\meta.xml" /A-D /B /S 2^>nul') do (
set "Country="
set "Year="
for /F "tokens=1,2 delims=< > " %%A in ('%SystemRoot%\System32\findstr.exe /R "<Country>[^<][^<]*</Country> <Year>[^<][^<]*</Year>" "%%I"') do (
if "%%A" == "Country" if not defined Country set "Country=%%B"
if "%%A" == "Year" if not defined Year set "Year=%%B"
)
if defined Country if defined Year (
set "FilePath=%%~dpI"
setlocal EnableDelayedExpansion
echo !FilePath:~0,-1!| %SystemRoot%\System32\findstr.exe /E /I /L /C:" (!Year!) [!Country!]" >nul
for %%J in ("!FilePath:~0,-1!") do if not exist "%%~J (!Year!) [!Country!]" ren "%%~J" "%%~nxJ (!Year!) [!Country!]"
endlocal
)
)
endlocal
ATTENTION: The delimiters of second FOR are a left angle bracket <
, a horizontal tab character, a right angle bracket >
and a normal space character. So make sure to have those four characters in batch file after copying and pasting the code from the browser window.
The environment variable SourceDir
is defined with a directory path without backslash at end because it is concatenated with \meta.xml
in the next line. It would be syntactically not 100% correct to define this variable with E:\Test
as this results in the file name string E:\Test\\meta.xml
containing two backslash instead of just one between file path and file name.
For each file meta.xml
found by command DIR in source directory or any of its subdirectories the command FOR executes the executable FINDSTR which searches case-sensitive (XML tags are case-sensitive) for country OR year elements with a string value of at least one character. The space character in search regular expression is interpreted by FINDSTR as OR which is not really good documented.
If environment variables Country
and Year
are defined both after searching for country and year strings in meta.xml
, the file path of the current XML file is assigned to environment variable FilePath
which always ends with a backslash.
Now it is time to enable delayed expansion. It should not be enabled before in case of a folder name contains one or more exclamation marks as in this case with delayed expansion already enabled at beginning of the batch file the batch file would not work at all. Please read this answer for details about the commands SETLOCAL and ENDLOCAL as much more happens than just enabling delayed environment variable expansion.
The entire folder name with path is output with ECHO and redirected to handle STDIN of FINDSTR to check if the folder name does not already end with (correct) year in round brackets and (correct) country in square brackets. This additional test is inspired by code written by LotPings.
The third FOR executed only if folder name does not already end with (correct) year and country is used to get from file path without the backslash at end the last folder name referenced with %%~nxJ
which references the string after last backslash independent if this string represents a file name with file extension or a folder name. %%~J
references the current folder name with full path without backslash at end.
Note 1: The batch file is not designed for being executed with source directory being root directory of a drive.
Note 2: The code does not remove a wrong year/country from folder name if the file meta.xml
contains now a different year or country. So a folder named already Folder1 (2015) [US]
is not renamed to Folder1 (2019) [India]
if meta.xml
in folder Folder1 (2015) [US]
contains now <Country>India</Country>
and <Year>2019</Year>
.
Note 3: The line parsing done by FOR does not work if the XML file contains multiple XML elements on one line. Country
and Year
elements must be on separate lines with no other XML elements. So this batch code cannot be used if meta.xml
contains for example <Country>India</Country><Year>2019</Year>
in one line. Windows command processor has no features to parse XML content in XML manner like PowerShell supports.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
dir /?
echo /?
endlocal /?
findstr /?
for /?
if /?
ren /?
set /?
setlocal /?