-4

I have several hundred scanned in files within a Desktop directory. I need to loop thru them and change the file names. The files must retain the first 6 characters of their name. Everything after and including the dash needs to be removed. The file extension (.pdf) is needed too.

The file names are like this:

000001-067.pdf
000034-003.pdf
000078-123.pdf
000009-011.pdf

What I need to do is remove the the dash and final three characters in file name. So the results will be:

000001.pdf
000034.pdf
000078.pdf
000009.pdf

I wrote the following code but it throws an error on File.Move. Any ideas how to fix it?

DirectoryInfo d = new DirectoryInfo(@"C:\Users\BrewMaster\Desktop\ScannedFilesToProcess\");
FileInfo[] infos = d.GetFiles();
foreach (FileInfo f in infos)
{
    string fileName = f.Name;

    int indexOfDash = fileName.LastIndexOf('-'); // find the position of -
    int indexOfPeriod = fileName.LastIndexOf('.'); // find the position of .

    // find remove the text between - and .
    string newFileName = fileName.Remove(indexOfDash, indexOfPeriod - indexOfDash);

    //File.Move(f.FullName, f.FullName.Replace("-", "")); //This only removes the dash. The 3 characters after it remain

    File.Move(f.Name, newFileName); //This throws and error. System.IO.FileNotFoundException ' Could not find file C:\Users\BrewMaster\source\repos\ChangeFileName\bin\Debug\000001-067.pdf
}
4
  • Using the free, built-in Step Debugger to debug your own code is easier than you think Commented Oct 4, 2019 at 18:42
  • 2
    f.Name does not include the full path. Try Directory.GetFiles() instead Commented Oct 4, 2019 at 18:45
  • Is there a question here? I cannot find one. Please don't just tell a story and leave it to us to figure out what the question is. Commented Oct 4, 2019 at 19:00
  • Eric the question is there above the code snippet.How to get the code to work without throwing an error. Commented Oct 4, 2019 at 19:25

3 Answers 3

1

Here was my solution:

DirectoryInfo d = new DirectoryInfo(@"C:\Users\BrewMaster\Desktop\ScannedFilesToProcess\");
FileInfo[] infos = d.GetFiles();
foreach (FileInfo f in infos)
{
    string fileName = f.Name;     
    int indexOfDash = fileName.LastIndexOf('-'); // find the position of '-'
    int indexOfPeriod = fileName.LastIndexOf('.'); // find the position of '.'
    // find remove the text between '-' and '.'
    string newFileName = fileName.Remove(indexOfDash, indexOfPeriod - indexOfDash);
    File.Move(f.FullName, f.FullName.Replace(f.Name, newFileName));     
}
Sign up to request clarification or add additional context in comments.

Comments

0

I think the issue is that f.Name only returns the name of the file, not the fullpath needed to move it. An easy idea would be get the f.FullName and the f.Name, so once you move it, you specify the original path of the archive, and them combine the new directory path with the new file name (which you already redefined) to move it.

1 Comment

Your comment lead me to the final solution: File.Move(f.FullName, f.FullName.Replace(f.Name, newFileName));
0

It is trying to rename/move the file in the current directory, and not in the directory which you originally scanned. Try:

String path = @"C:\Users\BrewMaster\Desktop\ScannedFilesToProcess\";
DirectoryInfo d = new DirectoryInfo(path);
FileInfo[] infos = d.GetFiles();
foreach (FileInfo f in infos)
{
    string fileName = f.Name;

    int indexOfDash = fileName.LastIndexOf('-'); // find the position of -
    int indexOfPeriod = fileName.LastIndexOf('.'); // find the position of .

    // find remove the text between - and .
    string newFileName = fileName.Remove(indexOfDash, indexOfPeriod - indexOfDash);

    //File.Move(f.FullName, f.FullName.Replace("-", "")); //This only removes the dash. The 3 characters after it remain

 File.Move(Path.Combine(path, f.Name), Path.Combine(path, newFileName));
}

3 Comments

In general, I recommend using Path.Combine over String.Format.
Fair enough @Brian, I have fixed it. Is that what you mean?
Yes, that's what I meant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.