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
38 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 42

Accepted Solution

by:
zephyr_hex earned 500 total points
Comment Utility
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
Comment Utility
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 42

Expert Comment

by:zephyr_hex
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now