Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

C# - Searching for Existing Files

  • 09-09-2009 11:18am
    #1
    Registered Users, Registered Users 2 Posts: 99 ✭✭


    I have tried to code a C# method to look in different directories for existing files with a certain sequence in the filename, i need to verify whether the file exists & increment a counter accordingly. I can work it using hard-coded filenames, however the names contain a sequence no that changes daily, so I would be searching for something like the following:

    TXT_DDDD-17.dat (where DDDD is variable seq no)
    I need to search for this file in different places & increment a file counter for use elsewhere. My attempt so far has amounted to the following piece of sample code:

    {
    int fileCount = 0;


    if (File.Exists("C:\\Directory\\SUBDIR1\\TXT_*-17.dat") == true)
    fileCount++;

    if (File.Exists("C:\\Directory\\SUBDIR2\\TXT_*-17.dat") == true)
    fileCount++;


    return fileCount;
    }

    I've also tried using regular expressions, replacing the 4-digit seq no in filename as '....' & also using the /b tag to search for files where name ends in "-17.dat"

    Im not very experienced & I cannot find anything of use in any help files. I dont even know if Im looking in the right ballpark!!

    Please help.


Comments

  • Registered Users, Registered Users 2 Posts: 2,781 ✭✭✭amen


    why not just recursively search through the directory getting each filename
    and compare the file to the file pattern you want to match?
    if there is match increment it not then exit

    there are c# functions got getting directory/file names


  • Registered Users, Registered Users 2 Posts: 515 ✭✭✭NeverSayDie


    The File.Exists() method doesn't support wildcards, that's probably why you're not getting any hits.

    You could use one of the overloads of the Directory.GetFiles() method to search each of your directories for matching files, that one will let you use wildcards. See here;
    http://msdn.microsoft.com/en-us/library/wz42302f(VS.80).aspx


  • Registered Users, Registered Users 2 Posts: 2,931 ✭✭✭Ginger




  • Registered Users, Registered Users 2 Posts: 99 ✭✭fun.bobby1981


    Thanks to everyone for your feedback.

    I've managed to work a solution using some of the above suggestions & some ideas of my own.

    These boards really do work!! :) (Im new to using them too!!)

    Cheers,
    FB


Advertisement