Cignitor
Cignitor

Reputation: 1097

C# mass File renamer

i want to create C# mass file renamer, here is my UI enter image description here

i have created tes folder, inside of tes there's a file which is 1.txt.

i want to create my program to add prefix and suffix to the files, so 1.txt will become prefix1suffix

but then i got an error enter image description here

it's said file already exist though there's only one file on tes folder, which is 1.txt how do i make it work ? where's the error comes from ?

i have tried the following code

private void Rename(string prefix, string filepath, string suffix)
    {            
        //i don't use prefix suffix yet to make sure if my function works
        DirectoryInfo d = new DirectoryInfo(filepath);
        FileInfo[] file = d.GetFiles();

        try
        {
            foreach (FileInfo f in file )
        {
            File.Move(f.FullName,"stackoverflow");
        }
        }
        catch (Exception e)
        {
            cmd.cetakGagal(e.ToString(), title);
        }
        cmd.cetakSukses("Rename Success", title);
    }

and it returns same error as the second picture above.

the following picture is tes folder, there's nothing in tes folder except 1.txt

enter image description here

Upvotes: 0

Views: 297

Answers (4)

tinstaafl
tinstaafl

Reputation: 6958

Since your goal is renaming, I would suggest using a test folder filled with files rather than using a piecemeal approach. See if something like this helps:

private void Rename(string prefix, string filepath, string suffix)
    {            
        //i don't use prefix suffix yet to make sure if my function works
        DirectoryInfo d = new DirectoryInfo(filepath);
        FileInfo[] file = d.GetFiles();

        try
        {
            foreach (FileInfo f in file )
        {
            f.MoveTo(@filepath + @"\" + prefix + f.Name.Insert(f.Name.LastIndexOf('.'),suffix));
        }
        }
        catch (Exception e)
        {
            cmd.cetakGagal(e.ToString(), title);
        }
        cmd.cetakSukses("Rename Success", title);
    }

on a side note using a listview to display the filenames and the changes before they're committed will help prevent unwanted changes.

Upvotes: 0

Harrison
Harrison

Reputation: 3963

your File.Move is changing them all to StackOverflow not using the prefix and suffix. If you only have one file in the directory it shouldn't be an issue. Are you sure there is only 1 file?

public static void Move(
    string sourceFileName,
    string destFileName
)

Looking at this answer might be the clue as you are specifying relative path for the destination file. To obtain the current working directory, see GetCurrentDirectory

The sourceFileName and destFileName arguments are permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory.

You should change

File.Move(f.FullName,"stackoverflow");

to

string fileName = f.Name.Replace(f.Extenstion,string.Empty);
string newFileName = string.Format("{0}{1}{2}",prefix,fileName,suffix);
string newFileWithPath = Path.Combine(f.Directory,newFileName);
if (!File.Exists(newFileWithPath))
{
     File.Move(f.FullName,newFileWithPath);
}

Upvotes: 1

Joel Rondeau
Joel Rondeau

Reputation: 7586

You are calling File.Move() with a full path for your sourceFileName and a relative path for your destFileName. The relative file path is relative to the current working directory and not to the source file path. I expect that a stackoverflow file exists in the current working directory, most likely created the first time you ran this code.

Upvotes: 3

Tim Ebenezer
Tim Ebenezer

Reputation: 2724

The code above will give you that error since, after the first run through, "stackoverflow" exists as a file. Make sure that you check if the destination file exists (using File.Exists) before calling File.Move.

Upvotes: 0

Related Questions