locked
Retrieving files by name and sending to stored procedure RRS feed

  • Question

  • User-996252219 posted

    I have a folder that contains x amount of files with different extensions, I need a way to retrieve all the files with the name 0000001 regardless of their extension, i then have to work with these and then retrieve all files with the name 0000002. I know I can increment the filename variable but when the file name becomes 0000010 how do I keep it the same amount of digits when incrementing, will it just add an extra digit such as 00000010.

    So far I am accessing the files and saving them all in an array

    1. String[] files = Directory.GetFiles(@"D:\test\ElectionFiles");

    and then looping through each file and pulling the information from it and sending it to the database

    1. foreach (String fi in files)
    2. {
    3. FileInfo f = new FileInfo(fi);
    4. string filename = f.Name;
    5. XmlDocument doc = new XmlDocument();
    6. doc.Load(fi);
    7. string document = doc.InnerXml;
    8. SqlCommand cmd = new SqlCommand("submit");
    9. cmd.Parameters.AddWithValue("@document", document);
    10. cmd.Parameters.AddWithValue("@filename", filename);
    11. cmd.Parameters.Add(new SqlParameter()
    12. {
    13. ParameterName = "RETURN_VALUE",
    14. Direction = ParameterDirection.ReturnValue
    15. });
    16. }

    Just want some advice if this looks correct and how I can go about only retrieving the 4 files at a time and how I can increment the filename to retrieve the correct files.

    Thanks

    Wednesday, April 8, 2015 4:12 AM

All replies

  • User2008642861 posted

    Hi coder91,

    From your post, I agree with your idea. As I understand, you want to retrieve files without their extension. If so, please check the below case.

    http://stackoverflow.com/questions/4804990/c-sharp-getting-file-names-without-extensions

    Above case suggests that you could use Path.GetFileNameWithoutExtension Method .

    coder91

     

    Just want some advice if this looks correct and how I can go about only retrieving the 4 files at a time and how I can increment the filename to retrieve the correct files.

    Besides, about above requirement, I think it depend on the files' names. If the names of files is like the example you give, that's to say, their names are just number, I suggest that you could convert the name to int, and plus 1, then concert it to string as the parameter.

    int filenum = convert.Int32(filename);
    filenum +=1 ;
    filename = filenum,toString();

    If the files' names are not number, I think it may not be incremented. Or you could convert it to int.

    Hope this could be helpful to you.

    Best regards,

    Archer

    Wednesday, April 8, 2015 10:35 PM