0

I need to rename multiple CSV file names. I want to remove everything after the "-" so for example "File-1.csv" will become just "File.csv". Here's my code that's not working:

DirectoryInfo d = new DirectoryInfo(@"C:\folder");
FileInfo[] infos = d.GetFiles();
foreach (FileInfo f in infos)
{
    File.Move(f.FullName, f.FullName.ToString().Substring(0, f.LastIndexOf('-')));
}

I'm getting this error:

Error 2 'System.IO.FileInfo' does not contain a definition for 'LastIndexOf' and no extension method 'LastIndexOf' accepting a first argument of type 'System.IO.FileInfo' could be found (are you missing a using directive or an assembly reference?)

How can I fix this?

4
  • 2
    So what's it doing vs what you want it to be doing? (My guess is that it's dropping the extension - so that's what you should be looking at. That has nothing to do with the "multiple" part... get it working for a single file and I'd expect it to be fine with multiple files.) Commented Aug 22, 2016 at 15:14
  • Hi, im getting this error - Error 2 'System.IO.FileInfo' does not contain a definition for 'LastIndexOf' and no extension method 'LastIndexOf' accepting a first argument of type 'System.IO.FileInfo' could be found (are you missing a using directive or an assembly reference?) Commented Aug 22, 2016 at 15:18
  • Im really new to c# and i think it means theres a namespace missing, but ive tired googling and i cant work it out Commented Aug 22, 2016 at 15:19
  • 1
    No, it doesn't mean there's a namespace missing. Look at the exact error message you're getting. You're trying to call LastIndexOf() on a System.IO.FileInfo. Is that what you meant to do? Did you look for that method in the documentation for System.IO.FileInfo? Commented Aug 22, 2016 at 15:27

2 Answers 2

1

Your current code doesn't compile because you are calling .LastIndexOf() on the FileInfo object when you should be calling it on FileInfo.FullName. Your substring also chops off the extension from the filename string, which I don't think you want. Here is a solution that preserves the extension:

foreach (FileInfo f in infos)
{
    // Get the extension string from the existing file
    var extension = f.FullName.Substring(f.FullName.LastIndexOf('.'), f.FullName.Length - 1);

    // Get the filename, excluding the '-' as well as the extension
    var filename = f.FullName.SubString(0, f.FullName.LastIndexOf('-'));

    // Concatenate the filename and extension and move the file
    File.Move(f.FullName, String.Format("{0}{1}", filename, extension));
}    
Sign up to request clarification or add additional context in comments.

2 Comments

I think this solution would still miss the extension of the file right?
@Sam.C updated with a more verbose solution that preserves the extension.
0

I'm getting this error:

Error 2 'System.IO.FileInfo' does not contain a definition for 'LastIndexOf'

f is not string; try f.FullName.LastIndexOf...

Comments

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.