Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How would you write code using a loop to read in the individual files, 1 at a time, from a directory and process them?

Posted on 2016-08-30
3
55 Views
Last Modified: 2016-08-31
I am developing a C# application using VS2010 and .Net Framework 4.0

How would you write code using a loop to read in the individual files with a file name extension of "ard.out",
1 file at a time from a directory (for example: C:\BankABC\).

Then assign these *.ard.out file names to a variable X, so that the files could be read individually by "File.ReadAllBytes(X)" and then additional statements process these files.

byte[] imgBits = File.ReadAllBytes(X);
Do Some Additional Processing
0
Comment
Question by:zimmer9
  • 2
3 Comments
 
LVL 43

Accepted Solution

by:
zephyr_hex (Megan) earned 500 total points
ID: 41776975
Here's a general approach, using GetFiles() to get a list of files and then loop through the list to load to a byte array:

//get a list of files in the directory
//GetFiles SearchOption can be set to look at subfolders.  Here I'm just looking at the current folder
//sourceDir would be "C:\BankABC\" in your case
string[] files = Directory.GetFiles(sourceDir, "*.out", SearchOption.TopDirectoryOnly);
foreach(string file in files) 
{
     string fileName = Path.GetFileName(file);
      byte[] imgBits = System.IO.File.ReadAllBytes(fileName);
     //Do some stuff
}

Open in new window

0
 

Author Comment

by:zimmer9
ID: 41777365
Why am I pointing to two different directory paths in the following code?
The filenames are the same.

My correct path is defined by TARGET_DIR_IN

However, "File.ReadAllBytes(fileName)" defines the path as \\m\USERS\z\My Documents\Visual Studio 2010\Projects\W\bin\Debug\20160824.ard.out'.

------------------
const string TARGET_DIR_IN = @"\\v\na\ctrls\dev\W\data\Test\";

string[] files = Directory.GetFiles(TARGET_DIR_IN, "*.out", SearchOption.TopDirectoryOnly);
            foreach (string file in files)
            {
                string fileName = Path.GetFileName(file);
                byte[] imgBits = File.ReadAllBytes(fileName);
0
 
LVL 43

Expert Comment

by:zephyr_hex (Megan)
ID: 41778234
I'm not sure I understand your question.  Are you saying that GetFiles() is not getting the files in TARGET_DIR_IN?
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question