none
Find FileName With Wildcard RRS feed

  • Question

  • I have a directory full of image files which need to be copied to another directory when the original is updated. So basically I need to do this:

    System.IO.File.Copy(originalDirectoryAndName, newDirectoryAndName, true);

    The only problem is that the original file has some random characters in it's name, and I need to be able to search by wildcard.

    How can this be best accomplished?

    Thanks,
    Russ

    Friday, September 22, 2006 9:18 PM

Answers

  • well the whole System.IO.File.Exists(path\filename) does this for you, you just need to replace the bold text - it checks to see if the given path and file exists.

    The filename is actually like: "11223344_101_12.jpg". The "12" is the random value in this example

    cool, just pass this into the "Exists" method, along with the path you want to check...as suggested above (so it could be 11223344_101_13.jpg, _14....._15......

     

    There will never be more then one file with that matches the "11223344_101_" scheme however.

    ok, so, if you have this returned: 11223344_101_ then in my initial reply (I think), do the whole Directory.GetFiles() thing to get a list of files that match that criteria. Example:

    string[] theFiles = System.IO.Directory.GetFiles(path, theFileName + "*"); //would get all the files that match the filename given.

    so if I had these values returned to me, as is:

    11223344_101_

    then, hardcoded, would be:

    string[] theFiles = System.IO.Directory.GetFiles(path, "11223344_101_*");

     

    or IF the filename/value returned is like this, as is:

    11223344_101_??

    then replace the ?? with a *  (theFileName.Replace("??", "*")) would replace all ?? found with a * instead, then do the above, then go through each of the files found (another iteration) and do the if (File.Exists(currentFile)) etc....

     

    does this help you? (hope it doesnt make you confused!) I think I've now made myself confused

     

    edit: you may wish to look at this for understanding on how the GetFiles() works:

    http://msdn2.microsoft.com/en-us/library/wz42302f.aspx

    Friday, September 22, 2006 11:29 PM
    Moderator

All replies

  • what you could do is get a list of the files you wish to copy. Example:

     

    string[] theFilesToCopy = System.IO.Directory.GetFiles(sourcePath, "*.jpg");

    this gets the list of files that has an extension jpg, or you could specify all files *.* filter - the second parameter in the GetFiles, specifies a search filter - so you can put in this wildcard of yours and it should find them

     

    then go through each item in the string array and copy it to your destination path:

     

    foreach(string currentFile in theFilesToCopy)

    {

       System.IO.File.Copy(currentFile, newDirectory, true);

    }

    does this help?

     

    • Proposed as answer by Ilya Zm Monday, October 15, 2012 5:10 PM
    Friday, September 22, 2006 9:22 PM
    Moderator
  • The problem with that method is that I'm doing this against the PrimaryKey of my database and an iteration for each to see if the file has been updated, and I have to change the filename. So basically the original file format is:

    PrimaryKey_??_.jpg where ?? are the random characters.

    I can only take the updated pictures because additional processing occurs from there. With over 200,000 images, in the original directory at any time, I need to only be able to copy the ones that have been updated.

    Thanks,
    Russ

    Friday, September 22, 2006 9:30 PM
  • ok, that's clearer - detail makes a huge difference :-)

    question is, my apologies if I misunderstood, how are you obtaining the "updated pictures" from the database? Do you mean that if the picture file names in the database (PrimaryKey_??_.jpg for example) exist in the source directory on your computer, you want to be able to move them/copy them to another directory?

    once again I apologise if I have misunderstood

     

    edit: reading more and more of your response I think I understand better. Please correct me

     

  • you have a list of items from the database, file names, with wildcharacters

  • you want to see if the file(s) are updated in a physical path location on your computer

  • if they are updated, then copy them/move them/rename if/do whatever

     

Friday, September 22, 2006 9:35 PM
Moderator
  • Sorry for being unclear in the original post.

    Basically here is a very simplified version my whole process to give you a better understanding:

    1. Vendor program downloads data and images.
    2. I process all data from the vendor program.
    3. Based on the vendor primary key, I copy photos to a new directory if they are brand new (don't exist in my second directory), or have been updated in the vendor image directory (date last modified is newer then mine).

    Is this clearer, or am I still missing the question?

    Thanks for all your help,
    Russ

     

    Friday, September 22, 2006 9:44 PM
  • you have a list of items from the database, file names, with wildcharacters

  • you want to see if the file(s) are updated in a physical path location on your computer

  • if they are updated, then copy them/move them/rename if/do whatever
  • Correct.

    Friday, September 22, 2006 10:02 PM
  • yes thats better, thanks for that. I will try my best here.

    basically you need to store in an array, the list of files that do not exist in the directory based on the filename you are going through in the processing stage (for each)

    however IF the file does exist, then look at the last modified time and if the time in the database is older than the file in the directory, then add this file in the list of "files to copy" array/collection.

    does this sound like more towards your path? if so...then try this.....it's a pseudocode so modify it appropriately if you can

     

    //declare an arraylist, easier to handle

    ArrayList theFilesToCopy = new ArrayList();

     

    //for each loop, get the file name from the database - code will be here for that

    if (System.IO.File.Exists(originalDirectory\currentFileNameFromDatabase) == false)

    {

       //file doesnt exist - ok its time to copy...add it to our list

       //theFilesToCopy.Add(Newpath\currentFileNameFromDatabase);

       System.IO.File.Copy(originalPath\currentFileNameFromDatabase, newPath\fileName, true); //or directly copy it from the source to the destination (new folder?)

    }

    else

    {

       //ok, file DOES exist so check the datetime to see if its newer and if so, copy it otherwise leave it or do whatever you like

       System.IO.FileInfo theFileInformation = new System.IO.FileInfo(originalPath\currentFileNameFromDatabase);

       if (theFileInformation.LastWriteTime < currentFileDateTimeField) //or something like this, check to see if the original date time is older than the file you want to analyze/get time details

       {

          //ok so the file on disk does exist but it is older than what the database states. Add it to the collection

          //theFilesToCopy.Add(pathToCopyTo\currentFileNameFromDatabase);

          System.IO.File.Copy(originalPath\currentFileNameFromDatabase, NewPath\filename, true); //or directly copy

       }

    }

    //end for each loop

     

    Sorry its not accurate but I do hope this is clear and hopefully it works in some way.

    Friday, September 22, 2006 10:05 PM
    Moderator
  • I feel like I am missing something, is it not in the code, or am I not understanding correctly?

    Basically, how am I getting the file names which match the wildcard in order to assign them to the array?

    Thanks,
    Russ

    Friday, September 22, 2006 10:50 PM
  • :-)

    well I commented out a couple of things, I said it was optional for you to store it into an array, whilst typing I thought of a better/more effecient way than what I initially thought so started to put in the optional thing.

    you can copy there and then in the interation loop.

    To get the filenames, well are you talking about physically on the disk? if not, then you would get it from the fieldname from the database that contains the filename (when iterating in the for each loop for the rows/columns)

    sorry, I will clear it up - my apologies. when you are saying "which match the wildcard" - is this in reference to the field/value in the database? if so, then I know you gave an example of the wildcard:

    PrimaryKey_??_.jpg (for example)

    can you give a real instance of this value? (is it like PrimaryKey_g65_.jpg)?

     

    Friday, September 22, 2006 10:56 PM
    Moderator
  • The filename is actually like: "11223344_101_12.jpg". The "12" is the random value in this example. There will never be more then one file with that matches the "11223344_101_" scheme however.

    The random value isn't in my database however, just the PrimaryKey, so I'm still unsure how I look it up.

    Thanks,
    Russ

    Friday, September 22, 2006 11:02 PM
  • ok, ill give it a bash! :-)

    do you know how to work with dataset/datatables etc.... ? Is this the problem you are having, getting to iterate through the rows/columns to get "the value from that field"? if so, let us know and also can you post the current code you have when retrieving items from the database?

    Sorry for the delay but as long as we try to get "there", thats all that counts!

    Friday, September 22, 2006 11:10 PM
    Moderator
  • I don't think I have any problems at working with datastes/datatables. :o)

    My holdup is just trying to find out if there is a file that matches the "11223344_101_??.jpg" format.

    Thanks,
    Russ

    Friday, September 22, 2006 11:16 PM
  • well the whole System.IO.File.Exists(path\filename) does this for you, you just need to replace the bold text - it checks to see if the given path and file exists.

    The filename is actually like: "11223344_101_12.jpg". The "12" is the random value in this example

    cool, just pass this into the "Exists" method, along with the path you want to check...as suggested above (so it could be 11223344_101_13.jpg, _14....._15......

     

    There will never be more then one file with that matches the "11223344_101_" scheme however.

    ok, so, if you have this returned: 11223344_101_ then in my initial reply (I think), do the whole Directory.GetFiles() thing to get a list of files that match that criteria. Example:

    string[] theFiles = System.IO.Directory.GetFiles(path, theFileName + "*"); //would get all the files that match the filename given.

    so if I had these values returned to me, as is:

    11223344_101_

    then, hardcoded, would be:

    string[] theFiles = System.IO.Directory.GetFiles(path, "11223344_101_*");

     

    or IF the filename/value returned is like this, as is:

    11223344_101_??

    then replace the ?? with a *  (theFileName.Replace("??", "*")) would replace all ?? found with a * instead, then do the above, then go through each of the files found (another iteration) and do the if (File.Exists(currentFile)) etc....

     

    does this help you? (hope it doesnt make you confused!) I think I've now made myself confused

     

    edit: you may wish to look at this for understanding on how the GetFiles() works:

    http://msdn2.microsoft.com/en-us/library/wz42302f.aspx

    Friday, September 22, 2006 11:29 PM
    Moderator
  • I ended up getting it working on Saturday. The GetFiles was exactly what I needed.

    Thanks for your help,
    Russ

    Monday, September 25, 2006 3:30 PM