0

I have a unity project that i need to rename basically all my files in it to contain a - as a way of easily identifying what assetbundle i need to load it from.

I would need to insert a - at the end of the identifier so for example, testtest.png would become test-test.png and so on.

I currently have this (just want to get the identifier from the file name itself for now) however, the first string in temp is always empty with the second one containing the rest of the file name

foreach (string file in Directory.GetFiles(Directory.GetCurrentDirectory()))
{
    string name = file.Split('\\').Last();
    if (name.StartsWith(args[0]))
    {
        string[] temp = name.Split(new[]{args[0]},StringSplitOptions.None);
        foreach (string s in temp)
        {
            Console.WriteLine(s);
        }
    }
}
D:\renamething\bin\Debug>renamething.exe test
.pdb

I tried Regex for it as well however it produced the same result, empty string in the first one, rest of it in the second.

Dai
  • 141,631
  • 28
  • 261
  • 374
  • what is args[0] – Zee Mar 12 '22 at 06:13
  • Arguments, its a console application. Should probs clarify that in the main post –  Mar 12 '22 at 06:21
  • Posted a code below, Please accept this as the answer if it works – Zee Mar 12 '22 at 06:23
  • 2
    `string name=file.Split('\\').Last();` <-- Don't do this. Use `Path` and `FileInfo` instead. – Dai Mar 12 '22 at 06:24
  • completely forgot about FileInfo, would've been nice if i remembered that earlier. ty –  Mar 12 '22 at 06:34
  • Why aren't you just using [a renamer program](https://github.com/sampctech/ab_renamer) (linked is my personal favorite) and doing something like a "find `test.`, replace `-test.`" – Caius Jard Mar 12 '22 at 09:50
  • Wanted to try to figure it out myself, not use a already existing program –  Mar 12 '22 at 14:21

2 Answers2

0

I don't think that would work, especially the args[0]

Try this:

foreach (string file in Directory.GetFiles(Directory.GetCurrentDirectory()))
    {
        var fileInfo = new FileInfo(file); //Get FileInfo

        if (!fileInfo.Name.StartsWith(args[0], StringComparison.OrdinalIgnoreCase)) //If File doesn't start with the specified arguments, don't process
        {
            continue;
        }

        //Consider file -> C:/TestFolder/test.png
        var directory = fileInfo.DirectoryName;//Gives C:\TestFolder\
        var extension = fileInfo.Extension; //gives ".png"
        var fileName = Path.GetFileNameWithoutExtension(fileInfo.Name); //Gives "test"

        var modifiedFileName = $"{fileName}-test{extension}"; //Gives "test-test.png

        var modifiedFullPath = $"{directory}/{modifiedFileName}";// C:\TestFolder\test-test.png

        fileInfo.MoveTo(modifiedFullPath);
    }
Zee
  • 622
  • 4
  • 13
  • 1
    `!file.StartsWith(args)` <-- This won't work because `file` will contain the absolute path to the file, including `C:\etc\etc` part. Use `FileInfo.Name` instead. – Dai Mar 12 '22 at 06:27
  • Also, `StartsWith` is case-sensitive but Windows/NTFS is case-insensitive, so what you also _really_ need `fileInfo.Name.StartsWith( prefix, StringComparison.OrdinalIgnoreCase )`. – Dai Mar 12 '22 at 06:32
  • `var extension = fileInfo.Extension; //gives "test"` <-- This is also incorrect. The return value will be `.png` ([it includes the dot](https://learn.microsoft.com/en-us/dotnet/api/system.io.filesysteminfo.extension?view=net-6.0)). – Dai Mar 12 '22 at 06:33
  • isn't exactly whats needed, i have for example a file in the project called QueenTexture.png, i need to insert a - between Queen and Texture so it becomes Queen-Texture.png (not asking for you to write what i want for free tho). I'll base what i have from this. ty –  Mar 12 '22 at 06:38
  • @Silentstorm well, you got it most of it. You probably oould figure out the rest. you can split the names by Capital Letters from this post https://stackoverflow.com/a/4489046/6048531 – Zee Mar 12 '22 at 06:41
0

With the code from @Zee (and @Dai from having a second look over), here is what i ended up in the odd chance that anyone else in the future comes here and needs it

public static void Main(string[] args){
            foreach(string file in Directory.GetFiles(Directory.GetCurrentDirectory())){
                FileInfo fileInfo=new FileInfo(file);
                if(fileInfo.Name.StartsWith(args[0])&&fileInfo.Name.EndsWith(".png"){
                    string[]temp=Regex.Split(fileInfo.Name, @"(?<!^)(?=[A-Z])");
                    temp[0]+="-";
                    File.Move(fileInfo.FullName,String.Concat(temp));
                }
            }
        }
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 12 '22 at 15:26