Home > other >  How to rename a file in the innermost subdirectory of a directory tree (4 levels in) to match the to
How to rename a file in the innermost subdirectory of a directory tree (4 levels in) to match the to

Time:11-13

I have a file named default.xex that is located in XBLA_Unpacked/DirectoryName/Subdirectory/000D0000/default.xex. I am trying to rename default.xex to be DirectoryName.xex. I managed to accomplish this with File.Move() but it pulled the .xex file up into XBLA_Unpacked, so both DirectoryName and DirectoryName.xex are located there. I need to be able to rename the .xex file while also keeping it inside the 000D000 subdirectory.

This is my current code which renames the .xex file and moves it up to the XBLA_Unpacked directory, as well as the code I wrote to try to move it back after it was renamed that doesn't work.

 static void ReNamePirs()
            {
                string homePath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
                string unpackedPath = homePath   "\\XBLA_Unpacked\\";
                string reNamePath = homePath   "\\XBLA_Unpacked";
                var files = GetAllFiles(unpackedPath);
                var folders = GetAllFolders(unpackedPath);
                List<string> sourceName = Directory.GetFiles(unpackedPath, "default.xex", SearchOption.AllDirectories).ToList();
                List<string> destinationName = Directory.GetDirectories(unpackedPath, "*", SearchOption.TopDirectoryOnly).ToList();
                List<string> finalDestination = Directory.GetDirectories(unpackedPath, "000D0000", SearchOption.AllDirectories).ToList();

                for (int i = 0; i < sourceName.Count; i  )
                {
                    destinationName[i] = destinationName[i]   ".xex";

                    File.Move(sourceName[i], destinationName[i]);
                    
                }
                sourceName = Directory.GetFiles(reNamePath, ".xex", SearchOption.AllDirectories).ToList();

                for (int i = 0; i < sourceName.Count; i  )
                {
                    destinationName = Directory.GetDirectories(unpackedPath, "000D0000", SearchOption.AllDirectories).ToList();
                    File.Move(sourceName[i], destinationName[i]);

                }

                return;
            }    

CodePudding user response:

The following should work

DirectoryInfo directoryInfo = new DirectoryInfo(renamePath);
FileInfo fileInfo = directoryInfo.GetFiles("default.xex", SearchOption.AllDirectories).FirstOrDefault();

if (fileInfo != null)
{
    string newFileName = fileInfo.FullName.Replace( Path.GetFileNameWithoutExtension(fileInfo.Name), fileInfo.Directory.Parent.Parent.Name);
    fileInfo.MoveTo(newFileName);
}

CodePudding user response:

just Edit this part of the code:

        for (int i = 0; i < sourceName.Count; i  )
        {
            destinationName[i] = destinationName[i]   ".xex";
            File.Move(sourceName[i], destinationName[i]);                    
        }

        sourceName = Directory.GetFiles(reNamePath, ".xex", SearchOption.AllDirectories).ToList();

        for (int i = 0; i < sourceName.Count; i  )
        {
            destinationName = Directory.GetDirectories(unpackedPath, "000D0000", SearchOption.AllDirectories).ToList();
            File.Move(sourceName[i], destinationName[i]);

        }

In the first loop, you can change the name and move it to the XBLA_Unpacked, and there is no need for the second loop. Anyway:

        for (int i = 0; i < sourceName.Count; i  )
        {
            String _dest=  sourceName[i].Replace("default",sourceName[i].Split("\\")[sourceName[i].Split("\\").Length-3])
            File.Move(sourceName[i], _dest);                    
        }

        // move to XBLA_Unpacked
        DirectoryInfo d = new DirectoryInfo(unpackedPath); 
        var Files = d.GetFiles("*.xex",SearchOption.AllDirectories).ToList(); 
        for (int i = 0; i < Files.Count; i  )
        {
            File.Move(Files[i].FullName, unpackedPath   Files[0].Name);
        }

CodePudding user response:

did you try using the mv shell command?

it can take a source file (as it's full path)

and another path (as a full path- with a new name for the file itself)

and "move" (also renames) the file to the new path. you will have to implement a way to use mv because its a bash command.

you will have to generate a string to represent the bash command, it will be something like : "mv XBLA_Unpacked/DirectoryName/Subdirectory/000D0000/default.xex XBLA_Unpacked/DirectoryName/Subdirectory/000D0000/.DirectoryName.xex"

i dont write in C# so i done know how to run bash commands, in c is by using "system" function. i found something that might do it:

public string RunCommandWithBash(string command)
{
    var psi = new ProcessStartInfo();
    psi.FileName = "/bin/bash";
    psi.Arguments = command;
    psi.RedirectStandardOutput = true;
    psi.UseShellExecute = false;
    psi.CreateNoWindow = true;

    using var process = Process.Start(psi);

    process.WaitForExit();

    var output = process.StandardOutput.ReadToEnd();

    return output;
}
  •  Tags:  
  • c#
  • Related