Page 1 of 1

Rename mass amount of game files related to .xml files

Posted: Fri Jan 27, 2017 9:09 pm
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?

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

Posted: Fri Jan 27, 2017 9:56 pm
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.

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

Posted: Sat Jan 28, 2017 7:08 am
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?

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

Posted: Sat Jan 28, 2017 6:34 pm
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

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

Posted: Sat Jan 28, 2017 7:58 pm
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!

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

Posted: Sat Jan 28, 2017 8:57 pm
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.

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

Posted: Sun Jan 29, 2017 7:37 am
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:

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

Posted: Sun Jan 29, 2017 3:44 pm
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;
            }
        }
    }
}

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

Posted: Sun Jan 29, 2017 5:42 pm
by oux
Could you provide compiled .exe file please?

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

Posted: Sun Jan 29, 2017 5:58 pm
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)

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

Posted: Sun Jan 29, 2017 6:22 pm
by oux
Thank you very much, it works!

I hope I will learn programming language some day, looking into C++.

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

Posted: Sun Jan 29, 2017 7:36 pm
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!

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

Posted: Sun Nov 05, 2017 1:20 pm
by oux
:ninja: