Important information: this site is currently scheduled to go offline indefinitely by end of the year.

Rename mass amount of game files related to .xml files

The Original Forum. Game archives, full of resources. How to open them? Get help here.
Post Reply
oux
n00b
Posts: 14
Joined: Fri Jan 27, 2017 8:45 pm
Has thanked: 4 times

Rename mass amount of game files related to .xml files

Post by oux »

Hello, I am a foreigner, I am sorry for bad language and/or wrong topic.

I extracted all music files from The Witcher 3 but it seems that all files named as their ID from .xml file, the .xml file also contain filename related to its ID.
I need programmer to write a code, that will rename all files to their true name from .xml file. Could someone please help?
You do not have the required permissions to view the files attached to this post.
User avatar
traderain
beginner
Posts: 39
Joined: Thu Aug 28, 2014 6:48 pm
Location: Hungary
Has thanked: 6 times
Been thanked: 10 times
Contact:

Re: Rename mass amount of game files related to .xml files

Post by traderain »

Could you please provide a part of that xml?
Anyway here is some untested code:

Code: Select all

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;

namespace FileRenamer
{
    class Program
    {
        static void Main(string[] args)
        {
            //var arguments = args;
           var arguments = new string[] { "soundbanksinfo.xml", "ogs"};

            if (arguments.Count() < 2)
            {
                Console.WriteLine("\nUsage: this.exe test.xml \"game\\musicfolder\\\"");
                Console.ReadLine();
                Environment.Exit(0x01);
            }
            var x = XDocument.Load(arguments[0]);
            var fileidpairs = x.Descendants("File")
                .ToArray()
                .Select(y =>new Tuple<int, string>(safeparseint(y.Attribute("Id").Value), y.Descendants("ShortName").First().Value));
            var files = Directory.GetFiles(arguments[1],"*.ogg");
            foreach (var fildescription in fileidpairs)
            {
                if (files.Any(z => Path.GetFileNameWithoutExtension(z) == fildescription.Item1.ToString()))
                {
                    var file = files.First(z => Path.GetFileNameWithoutExtension(z) == fildescription.Item1.ToString());
                    var destname = Path.GetDirectoryName(file) + "\\" +
                                   GetNonFileNameWithoutExtension(fildescription.Item2);
                    if (File.Exists(file))
                    {
                        File.Move(file, destname);
                    }
                }
            }
            
        }
        public static string GetNonFileNameWithoutExtension(string s)
        {
            return !s.Contains('\\') ? s : s.Substring(s.LastIndexOf('\\')+1);
        }

        public static int safeparseint(string i)
        {
            int res;
            if (int.TryParse(i, out res))
            {
                return res;
            }
            else
            {
                return 0;
            }
        }
    }
}
Edit: With a test xml this works for me.
Last edited by traderain on Sat Jan 28, 2017 10:44 pm, edited 1 time in total.
oux
n00b
Posts: 14
Joined: Fri Jan 27, 2017 8:45 pm
Has thanked: 4 times

Re: Rename mass amount of game files related to .xml files

Post by oux »

Here full .xml file: http://rgho.st/657sy5qsV
I am not a specialist, should I copy your code to the .bat file and run it?
User avatar
traderain
beginner
Posts: 39
Joined: Thu Aug 28, 2014 6:48 pm
Location: Hungary
Has thanked: 6 times
Been thanked: 10 times
Contact:

Re: Rename mass amount of game files related to .xml files

Post by traderain »

Its coded in c# you can either compile it yourself in visual studio or use this.
https://www.dropbox.com/s/jhy9q2vzzi0oa ... r.exe?dl=0
oux
n00b
Posts: 14
Joined: Fri Jan 27, 2017 8:45 pm
Has thanked: 4 times

Re: Rename mass amount of game files related to .xml files

Post by oux »

I am sorry, I don't want to be annoying, but I have installed Code::Blocks. So I might just compile your code above to .exe file and will it work for the .xml file I've provided?
Thank you for your help in any case!
User avatar
traderain
beginner
Posts: 39
Joined: Thu Aug 28, 2014 6:48 pm
Location: Hungary
Has thanked: 6 times
Been thanked: 10 times
Contact:

Re: Rename mass amount of game files related to .xml files

Post by traderain »

oux wrote:I am sorry, I don't want to be annoying, but I have installed Code::Blocks. So I might just compile your code above to .exe file and will it work for the .xml file I've provided?
Thank you for your help in any case!
You can't compile c# code in Code::Blocks you need to install either Visual Studio or Monodevelop.
oux
n00b
Posts: 14
Joined: Fri Jan 27, 2017 8:45 pm
Has thanked: 4 times

Re: Rename mass amount of game files related to .xml files

Post by oux »

I am doing something incorrect?
I put some .ogg files to "ogs" folder and renamed .xml file to test.xml, then I start FireRenamer.exe programm, but it shows the error:
You do not have the required permissions to view the files attached to this post.
User avatar
traderain
beginner
Posts: 39
Joined: Thu Aug 28, 2014 6:48 pm
Location: Hungary
Has thanked: 6 times
Been thanked: 10 times
Contact:

Re: Rename mass amount of game files related to .xml files

Post by traderain »

Fixed some errors It should work now!
Image
New code:

Code: Select all

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;

namespace FileRenamer
{
    public class Filecombo
    {
        public int id;
        public string newname;

        public Filecombo(int i, string s)
        {
            id = i;
            newname = s;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            //var arguments = args;
            var arguments = new string[] { "soundbanksinfo.xml", "ogs"};
            Console.Title = "Witcher 3 Soundbanks renamer by traderain";
            Console.WriteLine("Please give the name of the xml eg.:soundbanks.xml!");
            arguments[0] = Console.ReadLine();
            if (!File.Exists(arguments[0]) || Path.GetExtension(arguments[0]) != ".xml")
            {
                Console.Clear();
                Console.WriteLine("Invalid file!");
                Console.ReadLine();
                Environment.Exit(0x01);
            }
            Console.WriteLine("Please give the name of the folder which contains the .ogg files! eg.: sounds");
            arguments[1] = Console.ReadLine();
            if (!Directory.Exists(arguments[1]))
            {
                Console.Clear();
                Console.WriteLine("Invalid filder!");
                Console.ReadLine();
                Environment.Exit(0x01);
            }
            if (arguments.Count() < 2)
            {
                Console.WriteLine("\nUsage: this.exe test.xml \"game\\musicfolder\\\"");
                Console.ReadLine();
                Environment.Exit(0x01);
            }
            var x = XDocument.Load(arguments[0]);
            var fileidpairs = x.Descendants("File").ToArray();
            var filecombolist = (from fileidpair in fileidpairs let id = safeparseint(fileidpair.Attribute("Id")?.Value) let name = GetNonFileNameWithoutExtension(fileidpair.Element("ShortName")?.Value) select new Filecombo(id, name)).ToList();
            var files = Directory.GetFiles(arguments[1],"*.ogg");
            int count = 0;
            foreach (var fildescription in filecombolist)
            {
                if (files.Any(z => Path.GetFileNameWithoutExtension(z) == fildescription.id.ToString()))
                {
                    var file = files.First(z => Path.GetFileNameWithoutExtension(z) == fildescription.id.ToString());
                    var destname = Path.GetDirectoryName(file) + "\\" +
                                   GetNonFileNameWithoutExtension(fildescription.newname);
                    if (File.Exists(file))
                    {
                        File.Move(file, destname);
                        count++;
                        Console.WriteLine("Renamed file! -> " + "ID: " + fildescription.id + " New name: " + fildescription.newname);
                    }
                }
            }
            Console.WriteLine("Done! Renamed " + count + " file(s).");
            Console.ReadKey();
        }
        public static string GetNonFileNameWithoutExtension(string s)
        {

            s = s ?? "";
            if (s.Contains('\\'))
            {
                s = s.Substring(s.LastIndexOf('\\') + 1,s.Length- (s.LastIndexOf('\\') + 1));
            }
            if (s.Length > 4 && s.Contains(".wav"))
            {
                s = s.Substring(0,s.Length-4);
                s += ".ogg";
            }

            return s;
        }

        public static int safeparseint(string i)
        {
            int res;
            if (int.TryParse(i, out res))
            {
                return res;
            }
            else
            {
                return 0;
            }
        }
    }
}
oux
n00b
Posts: 14
Joined: Fri Jan 27, 2017 8:45 pm
Has thanked: 4 times

Re: Rename mass amount of game files related to .xml files

Post by oux »

Could you provide compiled .exe file please?
User avatar
traderain
beginner
Posts: 39
Joined: Thu Aug 28, 2014 6:48 pm
Location: Hungary
Has thanked: 6 times
Been thanked: 10 times
Contact:

Re: Rename mass amount of game files related to .xml files

Post by traderain »

oux wrote:Could you provide compiled .exe file please?
Sure. https://www.dropbox.com/s/5c4xlaig6as8b ... g.rar?dl=0 (I zipped my whole folder so you can get an idea how my files are setup)
oux
n00b
Posts: 14
Joined: Fri Jan 27, 2017 8:45 pm
Has thanked: 4 times

Re: Rename mass amount of game files related to .xml files

Post by oux »

Thank you very much, it works!

I hope I will learn programming language some day, looking into C++.
User avatar
traderain
beginner
Posts: 39
Joined: Thu Aug 28, 2014 6:48 pm
Location: Hungary
Has thanked: 6 times
Been thanked: 10 times
Contact:

Re: Rename mass amount of game files related to .xml files

Post by traderain »

oux wrote:Thank you very much, it works!

I hope I will learn programming language some day, looking into C++.
Im glad I was able to help! :) Have a nice day!
oux
n00b
Posts: 14
Joined: Fri Jan 27, 2017 8:45 pm
Has thanked: 4 times

Re: Rename mass amount of game files related to .xml files

Post by oux »

:ninja:
sanderson169
ultra-n00b
Posts: 3
Joined: Sat Sep 03, 2022 6:24 pm
Been thanked: 1 time

Re: Rename mass amount of game files related to .xml files

Post by sanderson169 »

Hi! I am super interested on this, idk if anyone can help me.
I have this BMS script, which does something similar to the tool you shared. I would like someone to help me add that if the file is not found in the XML FILE it leaves it WITHOUT A NAME.

Code: Select all

# file types: Wwise *.txt with same name as *.bnk files or soundbanks.xml
# script type: parser
# note 1: all *.wem files must be in the same folder as this script and the txt
# note 2: script will maintain the original folder structure
#
# (c) by AlphaTwentyThree of Zenhax
# script for QuickBMS http://quickbms.aluigi.org

get EXT extension
if EXT == "xml"
	callfunction soundbanks 1
else
	print "script only for xml"
endif

startfunction soundbanks
	for i = 1
		FindLoc SEARCH string "<File Id="
		if SEARCH == ""
			cleanexit
		endif
		goto SEARCH
		getDstring DUMMY 10
		getCT DIDX string 0x22
		string DIDX += ".wem"
		getDstring TEST 3 # sometimes the file id tag is empty
		if TEST != " />"
			math SEARCH += 12
			goto SEARCH
			FindLoc SEARCH string "<Path>"
			goto SEARCH
			getDstring DUMMY 6
			getCT FNAME string 0x3c
			string FNAME -= 13
			open FDSE DIDX 1 EXIST
			if EXIST == 1
				get SIZE asize 1
				log MEMORY_FILE 0 0
				log MEMORY_FILE 0 SIZE 1
				string FNAME += ".wem"
				log FNAME 0 SIZE MEMORY_FILE
			endif
		endif
		math SEARCH += 12
		goto SEARCH
	next i
endfunction
Post Reply