Follow us on Facebook: https://www.facebook.com/xentax/ :)

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

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: 9 times
Contact:

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

Post by traderain » Fri Jan 27, 2017 9:56 pm

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

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: 9 times
Contact:

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

Post by traderain » Sat Jan 28, 2017 6:34 pm

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

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: 9 times
Contact:

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

Post by traderain » Sat Jan 28, 2017 8:57 pm

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

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: 9 times
Contact:

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

Post by traderain » Sun Jan 29, 2017 3:44 pm

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: 15
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 » Sun Jan 29, 2017 5:42 pm

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: 9 times
Contact:

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

Post by traderain » Sun Jan 29, 2017 5:58 pm

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: 15
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 » Sun Jan 29, 2017 6:22 pm

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: 9 times
Contact:

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

Post by traderain » Sun Jan 29, 2017 7:36 pm

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: 15
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 » Sun Nov 05, 2017 1:20 pm

:ninja:

Post Reply