-2

I have sound files named by ID. The only way to find the sounds is by rummaging through the reference bank which has each of the file names.

What I want to achieve is a script that finds the ID, and the 'Path' to rename the file and organize said file.

Here is the data from the XML that contains the info about the sounds:

<File Id="14518742" Language="SFX">
        <ShortName>Creatures\Fish_Large_Swim_03.wav</ShortName>
        <Path>SFX\Creatures\Fish_Large_Swim_03_9344E057.wem</Path>
</File>

Task requirements: Find <file ... which has the ID 14518742 which is the filename, e.g. 14518742.ogg. I want to move the file to the directory stored in <Path>:

SFX\Creatures\Fish_Large_Swim_03_9344E057

Due to incompetence I can't even get the ID:

@FOR /F "delims=    " %%i IN ('find "<File Id=" Bankinfo.xml') DO @(
    @for /f "delims=    " %%a in ("%%i") do @(
        set %%i=%%a
        set %id%=%%i
        Rem This was an attempt to trim the string, thing is that I can't find out why this doesn't work, because percentage signs.
        set %id%=%id:~-7,7%
        )
    )
pause

I couldn't solve the problem trying to trim strings, I only managed to using delimiters. Even then I probably wouldn't be able to complete this.

Edit #2: Revised due to "put on hold as too broad" and condensed it because it was too long. I'll apologize because I don't know much about scripting (with CMD at least) or any of the proper etiquette regarding how to ask questions on this site.

Mofi
  • 46,139
  • 17
  • 80
  • 143
Frautcres
  • 23
  • 2
  • Although it could be done in batch, xml processing is better suited to other programming languages. – DavidPostill Aug 29 '16 at 17:34
  • 3
    Please note that https://stackoverflow.com is not a free script/code writing service. If you tell us what you have tried so far (include the scripts/code you are already using) and where you are stuck then we can try to help with specific problems. You should also read [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask). – DavidPostill Aug 29 '16 at 17:34

1 Answers1

1

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:

  1. 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.

  2. 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 /?
Mofi
  • 46,139
  • 17
  • 80
  • 143