Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Counting files in a directory

Status
Not open for further replies.

dand11

Programmer
Jun 24, 2008
63
US
I am altering an application to fit with a clients specs. There are jpegs being held in a directory, I'll call C:\images. The format of the names of these are F######_000.jpg (The ######) are a unique ID numbers and the _000 portion is for the first image and the second image if there is one would be F######_001.jpg. I have the unique id and I need to be able to check for the number of files in the C:\images directory that have the matching ###### (Unique Ids) to see how many to insert into a dataset in C# code. How can I go about doing this?
 

Code:
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(@"C:\images");
        System.IO.FileInfo[] files = di.GetFiles("*_001.jpg");

 
Thanks KDavie, but now I have another problem. I'm basically trying to load all the jepg files with a common id into a datatable. The following is what I have but I'm getting an error thats states the following:

Input string was not in a correct format.


Code:
 protected void FillMugShotDataTable(DataTable dt, string ID)
        {
            string strFilename = "F" + ID.Trim() + "_*";
            
            

            DirectoryInfo myDirectory = new DirectoryInfo(@"C:\WEBS\CTS.WS.DATASEARCH\images");

            FileInfo[] files = myDirectory.GetFiles(strFilename);
            DataRow myRow;
            
            foreach (FileInfo file in files)
            { 
            
                // Load files into datatable
                myRow = dt.NewRow();
                myRow["ECSOID"] = ID;
                myRow["MugID"] = "";
                myRow["DATE"] = "01/01/1900";
                myRow["TIME"] = "00:00";
                myRow["IMAGE_TYPE"] = "F";
                myRow["Image01"] = file.OpenRead().;
                myRow["Total"] = files.GetLength(0);
                
                dt.Rows.Add(myRow);

                
            }

                        

        }
 
Is ID a string? If not that is your problem... I created a folder (C:\images) and added the following files...

F123_001.jpg
F123_002.jpg
F123_003.jpg
F456_001.jpg

And used this code....

Code:
string ID = "123";
string strFilename = "F" + ID.Trim() + "_*";
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(@"C:\images");
System.IO.FileInfo[] files = di.GetFiles(strFilename);

Everything works as expected for me.

hope this helps.


 
Thx KDavie. Yeah, my ID is a string. My problem is that the column in the datatable I'm trying to place it in is looking for a byte type and I'm passing in a Stream. How can I convert the stream into a byte using the FileInfo object?
 
My bad K. Let me clear things up a little. I had a "." in there where it shouldn't be then changed my code to the following :



Code:
protected void FillMugShotDataTable(DataTable dt, string ID)
        {
            string strFilename = "F" + ID.Trim() + "_*";
            
            

            DirectoryInfo myDirectory = new DirectoryInfo(@"C:\WEBS\CTS.WS.DATASEARCH\images");

            FileInfo[] files = myDirectory.GetFiles(strFilename);
            DataRow myRow;
            
            foreach (FileInfo file in files)
            { 
            
                // Load files into datatable
                myRow = dt.NewRow();
                myRow["ECSOID"] = ID;
                myRow["MugID"] = "";
                myRow["DATE"] = "01/01/1900";
                myRow["TIME"] = "00:00";
                myRow["IMAGE_TYPE"] = "F";
                myRow["Image01"] = file.OpenRead();
                myRow["Total"] = files.GetLength(0);
                
                dt.Rows.Add(myRow);

                
            }

                        

        }

[\code]

And I'm getting the following error:

Type of value has a mismatch with column typeCouldn't store <System.IO.FileStream> in Image01 Column.  Expected type is Byte[].
 
Change your foreach block to this...

Code:
foreach (System.IO.FileInfo file in files)
{

    // Load files into datatable
    myRow = dt.NewRow();
    myRow["ECSOID"] = ID;
    myRow["MugID"] = "";
    myRow["DATE"] = "01/01/1900";
    myRow["TIME"] = "00:00";
    myRow["IMAGE_TYPE"] = "F";
    using (System.IO.FileStream fs = file.OpenRead())
    {
        if (fs.CanRead)
        {
            byte[] bytes = new byte[fs.Length];
            fs.Read(bytes, 0, bytes.Length);
            myRow["Image01"] = bytes;
        }
    }
    
    myRow["Total"] = files.GetLength(0);
    dt.Rows.Add(myRow);
}

 
Also, keep in mind that this is actually storing the image (in bytes) in your DataTable... I'm assuming that is what your requirement is... If not, and you only want to store the name of the file (for example), you can skip the whole using block and just use...

Code:
myRow["Image01"] = file.Name;

 
Thx K. I actually am storing the image into the datatable. Everything is working fine now.

Thanks again
 
I've had to add to the function so that it can be used in two ways. One for pulling all similar files as descripbed above and now I'm wanting to add to it the ability to get a specific image and store it in the datatable. My code now is below and I'm getting the following error (the exetype param value is 0)


error:

Message:
ArrayTypeMismatchException - GetPersonImageSingle
Source array type cannot be assigned to destination array type.
Stack Trace: at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length, Boolean reliable)
at System.Array.CopyTo(Array array, Int32 index)
at CTS.DBOBJECT.CODETHREE.BaseQuery`1.FillMugShotDataTable(DataTable dt, String ID, Int32 exetype)
at CTS.DBOBJECT.CODETHREE.MugShot.GetMug(String mugShotID)
at CTS.DBOBJECT.CODETHREE.DBLogic.GetPersonImages(PersonDescription oPerson, String UserID, String UserName, String MugID)
at CTS.WS.DATASEARCH.BIZLogic.GetPersonImages(PersonDescription oPerson, RequestorSecurityLevel RequestorInfo, UsernameToken uToken, String MugID, Boolean GetThumbNail)
at CTS.WS.DATASEARCH.SEARCH.GetPersonImageSingle(String ECSOID, RequestorSecurityLevel RequestorInfo, String MUGID, Boolean GetThumbNail)
.



Code:
protected void FillMugShotDataTable(DataTable dt, string ID, int exetype)
        {

            string strFilename;


            if (exetype == 1)
                strFilename = "F" + ID.Trim() + "_*";
            else
                strFilename = ID;

            
            byte[] data = new byte[1024];
            DirectoryInfo myDirectory = new DirectoryInfo(@"C:\WEBS\CTS.WS.DATASEARCH\images");

            FileInfo[] files = myDirectory.GetFiles(strFilename);
            DataRow myRow;
            if (exetype == 1)
            {
                foreach (FileInfo file in files)
                {

                    {
                        // Load files into datatable
                        myRow = dt.NewRow();
                        myRow["ECSOID"] = ID;
                        myRow["MugID"] = file.Name;
                        myRow["DATE"] = "01/01/1900";
                        myRow["TIME"] = "00:00";
                        myRow["IMAGE_TYPE"] = "F";
                        myRow["IMAGE_DESC"] = "";

                        using (System.IO.FileStream fs = file.OpenRead())
                        {
                            if (fs.CanRead)
                            {
                                byte[] bytes = new byte[fs.Length];
                                fs.Read(bytes, 0, bytes.Length);
                                myRow["Image01"] = bytes;
                                myRow["ImageSize"] = bytes.Length;
                            }
                        }
                    }
                    dt.Rows.Add(myRow);
                }
            }
            else
            {
                // Load files into datatable
                myRow = dt.NewRow();
                myRow["ECSOID"] = ID;
                myRow["MugID"] = ID;
                myRow["DATE"] = "01/01/1900";
                myRow["TIME"] = "00:00";
                myRow["IMAGE_TYPE"] = "F";

                byte[] bytes = new byte[1024];
                files.CopyTo(bytes,0);


                //using (System.IO.FileStream fs = files.)
                //{
                    //if (fs.CanRead)
                    //{
                    //    byte[] bytes = new byte[fs.Length];
                    //    fs.Read(bytes, 0, bytes.Length);
                        myRow["Image01"] = bytes;
                        myRow["ImageSize"] = bytes.Length;
                    //}
                //}
            }

                        

        }
 
I'm not sure I follow your logic here... Seems to me like the original code should work whether you are retrieving a specific file or a collection of files.

 
Thx K you were right. I tried it that way earlier but I must have placed the full directory in for the filename variable. Its working now.


Dan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top