I have made a program that renames files in a folder, and it works, but there is one problem, if i add more files and then rename it with the program it overwrites the other files, so i have tried if(!File.Exists)
but it keeps not working. anyone can help with the if(!File.Exists)
part? cus this one doesn't work and always returns true
Console.WriteLine("1. Rename all\n2. Rename Custom");
int Choice = int.Parse(Console.ReadLine());
Console.Clear();
if(Choice==1)
{
Console.WriteLine("Enter the file path:");
string path = Console.ReadLine();
Console.WriteLine("Enter the new file type");
string type = Console.ReadLine();
DirectoryInfo d = new DirectoryInfo(@path);
FileInfo[] infos = d.GetFiles("*.*");
int i = 1;
foreach (FileInfo f in infos)
{
// Do the renaming here
if (!File.Exists(@path+i+"."+type))
File.Move(f.FullName, Path.Combine(f.DirectoryName, "" + i + "." + type));
i++;
}
}
First of all, you don't need @path
. path
without the @
will work just fine. Second, the file you're checking the existence of doesn't match the destination path for moving. Try this instead:
string destination = Path.Combine(f.DirectoryName, string.Format("{0}.{1}", i, type));
if (!File.Exists(destination))
{
File.Move(f.FullName, destination);
i++; // Unclear if you want this to increment every time or just when moving
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.