none
Delete file from folder based on filename parameter c# RRS feed

  • Question

  • I will be sending 'a1' parameter dynamically,  a1 holds the fileName. for E.g.. a1= AB123 

    My requirement is to delete the file that matches 'a1'. 

    There will be multiple files in the folder say AB123abc.txt, AB523abc.txt, AB123mno.pdf, AB666abc.txt

    So my code should be deleting AB123abc.txt and AB123mno.pdf files only.

    The current below code doesnt work. Please advise what am I doing wrong

     public class DeleteFile
        {
            public static void Remove(string a1)
            {
                string[] fileArray = Directory.GetFiles(@"E:\TestFolder\");
                for (int i = 0; i < fileArray.Length; i++)
                {
                    if (i.ToString().StartsWith(a1))
                    {
                        File.Delete(Path.Combine(i.ToString()));
    
                    }
                
                }
            
            
            }    


    RH

    Thursday, August 22, 2019 12:13 AM

Answers

  • string B = @"E:\TestFolder\";
    
     string[] fileArray = Directory.GetFiles(B);
    
    
    // Or...
    
    string B = "E:\\TestFolder\\";
    
     string[] fileArray = Directory.GetFiles(B);



    • Edited by Ante Meridian Thursday, August 22, 2019 1:12 AM Fixed some formatting.
    • Marked as answer by Dan2890 Thursday, August 22, 2019 1:44 AM
    Thursday, August 22, 2019 1:12 AM

All replies

  • Greetings Dan

    The loop counter i is an integer, so when you do i.ToString(), you will get "0", "1", "2" and so on, which won't start with the string you are looking to match. You can use Path.GetFileName to strip the drive and folder from the strings returned from Directory.GetFiles() so you can test the start, then use the original string s with paths for the deletion.

                for (int i = 0; i < fileArray.Length; i++)
                {
                    if (Path.GetFileName(fileArray[i]).StartsWith(a1)) // Test the name without the path (the "E:\TestFolder\" bit)
                    {
                        File.Delete(fileArray[i]); // Use the full path to delete.
    
                    }
                
                }
    Edit: It might also be an idea to use ToLower() or ToUpper() on the strings you are testing, to make the test case-insensitive, otherwise "abc" won't match "ABC" for example.
    • Edited by Ante Meridian Thursday, August 22, 2019 12:58 AM Added the bit about case sensitivity.
    Thursday, August 22, 2019 12:54 AM
  • is there a way I can store this location and pass it as a parameter ?

    string B = "E:\TestFolder\";

     string[] fileArray = Directory.GetFiles(@B);

    If I do that I a getting error. Please advise


    RH

    Thursday, August 22, 2019 1:07 AM
  • string B = @"E:\TestFolder\";
    
     string[] fileArray = Directory.GetFiles(B);
    
    
    // Or...
    
    string B = "E:\\TestFolder\\";
    
     string[] fileArray = Directory.GetFiles(B);



    • Edited by Ante Meridian Thursday, August 22, 2019 1:12 AM Fixed some formatting.
    • Marked as answer by Dan2890 Thursday, August 22, 2019 1:44 AM
    Thursday, August 22, 2019 1:12 AM
  • thank you !!

    RH

    Thursday, August 22, 2019 1:44 AM