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
63 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
[X]
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
  • 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

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

710 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