Reputation: 15
I have 4 directories that host different files in them. I currently have a form that if a certain checkbox is checked, it is suppose to go to that directory, and find all the pdf's in that directory and add a prefix to them.
for example, say folder 1 has 5 pdf's in them. i want it to go through and add "some prefix" to the file name. Before: Filename After: Some Prefix Filename
Upvotes: 0
Views: 4441
Reputation: 223402
Directory.GetFiles
File.Move
to rename the file. It should be like:
var files = Directory.GetFiles(@"C:\yourFolder", "*.pdf");
string prefix = "SomePrefix";
foreach (var file in files)
{
string newFileName = Path.Combine(Path.GetDirectoryName(file), (prefix + Path.GetFileName(file)));
File.Move(file, newFileName);
}
Upvotes: 3
Reputation: 22188
string path = "Some Directory";
string prefix = "Some Prefix";
foreach (var file in Directory.GetFiles(path, "*.pdf"))
{
var newName = Path.Combine(Path.GetDirectoryName(file), prefix + Path.GetFileName(file));
File.Move(file, newName);
}
Upvotes: 1