You could use the following batch code executed in the directory with all the *.ogg files and containing also the file XmlFile.xml
with the XML data:
Note: The character between <
and >
after delims=
on third line must be a horizontal tab character. The browser displays and copies the tab character according to HTML standard as single space character.
@echo off
setlocal EnableExtensions EnableDelayedExpansion
for /F "usebackq tokens=1* delims=< > " %%A in ("XmlFile.xml") do (
if "%%A" == "File" (
for /F "tokens=2 delims== " %%C in ("%%B") do set "OggFileName=%%~C.ogg"
) else if "%%A" == "Path" (
set "TargetPath=%%B"
set "TargetPath=!TargetPath:~0,-11!"
for /F "delims=" %%F in ("!TargetPath!") do set "NewFileName=%%~nxF.ogg"
if not exist "!TargetPath!" md "!TargetPath!"
if exist "!OggFileName!" (
move /-Y "!OggFileName!" "!TargetPath!\!NewFileName!"
) else (
echo There is no file !OggFileName! in folder %CD%.
)
)
)
endlocal
The most outer FOR processes each line read from XML file and splits each non empty line up into two strings:
The element name is assigned to loop variable A
because of tokens=1
which is File
, ShortName
, Path
and also /File
whereby the last one should be ignored which is the reason why /
is not specified in list of delimiters.
The remaining line after space or >
after element name is assigned to next loop variable B
being next character in ASCII table after A
because of *
appended after tokens=1
which are for this example:
Id="14518742" Language="SFX">
Creatures\Fish_Large_Swim_03.wav</ShortName>
SFX\Creatures\Fish_Large_Swim_03_9344E057.wem</Path>
Case-sensitive comparisons of value of loop variable A
are made next to identify those lines which need to be processed.
A line with element name File
contains the file name of the *.ogg file in value of attribute Id
. Another FOR is used to split up the string assigned to loop variable B
using equal sign and space characters as delimiter.
From the string Id="14518742" Language="SFX">
just the second substring "14518742"
after the equal sign is of interest which is the reason for using tokens=2
. This string without the double quotes and with .ogg
appended is assigned to environment variable OggFileName
for later use.
Another element of interest is Path
containing the folder path and also the file name of the *.ogg file.
So from string SFX\Creatures\Fish_Large_Swim_03_9344E057.wem</Path>
assigned to loop variable B
first the last 11 characters are removed to get target folder path string SFX\Creatures\Fish_Large_Swim_03_9344E057
.
Once more command FOR is used to get assigned to environment variable NewFileName
the string after last backslash which should be the new file name for the *.ogg file. Because the read extension .wem
was already removed before %%~nxF
is used instead of just %%~nF
in case of file name contains one more point which is interpreted on already truncated name of file as file extension separator.
Next the directory is created if not already existing and the *.ogg file is moved into this directory with the new file name if existing at all in current directory.
For moving the file with keeping the ID as file name, use the batch code below on which just line determining file name for environment variable NewFileName
and delayed expanded reference to this environment variable on line with command MOVE was removed in comparison to above batch code.
@echo off
setlocal EnableExtensions EnableDelayedExpansion
for /F "usebackq tokens=1* delims=< > " %%A in ("XmlFile.xml") do (
if "%%A" == "File" (
for /F "tokens=2 delims== " %%C in ("%%B") do set "OggFileName=%%~C.ogg"
) else if "%%A" == "Path" (
set "TargetPath=%%B"
set "TargetPath=!TargetPath:~0,-11!"
if not exist "!TargetPath!" md "!TargetPath!"
if exist "!OggFileName!" (
move /-Y "!OggFileName!" "!TargetPath!\"
) else (
echo There is no file !OggFileName! in folder %CD%.
)
)
)
endlocal
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.
echo /?
endlocal /?
for /?
if /?
move /?
set /?
setlocal /?