Accessing Every File in a Directory

So I wrote a program to turn a binary file's data into an unsigned character array for inclusion in an executable. It works just super.

I'm wondering how I can write a program that will perform this operation on every file in a directory and all it's sub-directories so that I can I can include everything I need all at ounce.
closed account (Dy7SLyTq)
what os are you running?
Boost.Filesystem's directory_interator can be used for a cross-platform solution, it you are familar with the Boost library. But the other possible solutions are platform specific solutions, so we need to know what o/s you're using.

Also, you haven't made it clear whether you app which will performs the operation on every file in a folder is the same one as the app which is doing the actual performing.

If you were thinking about a separate app, then you could write (or obtain) a tool like walk.exe, which used to provided as part of the Microsoft's LAN Manager Resource Kit.

WALK.EXE Command Line Parameters and Usage
http://support.microsoft.com/kb/113605

This page includes source for a version of walk that uses the POSIX opendir, readdir, and closedir calls.

Recursively Walk Directories and Files in C
http://www.shayanderson.com/c/recursively-walk-directories-and-files-in-c.htm

(There is probably a better way to walk directory trees when using Linux, but as I'm a Windows programmer that's not something I know about.)

A similar app for Windows would need to use either the CRT calls _findfirst _findnext, and _findclose or the Windows API calls FindFirstFile, FindNextFile, FindClose.

Or you could use the Boost directory_interator instead.

Andy

PS If I understand you correctly, your original app is a bit like the Linux xxd tool? (I'm using the MinGW version under MSYS here...)

$ xxd -i smiley.png smiley.h

creates smiley.h containing:

1
2
3
4
5
6
7
8
9
10
11
12
13
unsigned char smiley_png[] = {
  0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d,
  0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x1e,
  0x08, 0x06, 0x00, 0x00, 0x00, 0x3b, 0x30, 0xae, 0xa2, 0x00, 0x00, 0x00,

// snip

  0xfa, 0x3c, 0x4d, 0x69, 0x2f, 0x8f, 0x1b, 0x58, 0x21, 0x3f, 0x32, 0x07,
  0x02, 0xc2, 0x8a, 0xcd, 0x65, 0xfc, 0x96, 0x5e, 0xf3, 0x0f, 0x89, 0xf1,
  0x15, 0x4d, 0xa1, 0xa4, 0x6a, 0xcd, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45,
  0x4e, 0x44, 0xae, 0x42, 0x60, 0x82
};
unsigned int smiley_png_len = 2454;


Last edited on
perform this operation on every file in a directory and all it's sub-directories

boost's recursive_directory_iterator sounds like a better match then
http://www.boost.org/doc/libs/release/libs/filesystem/doc/reference.html#Class-recursive_directory_iterator
My OS is windows 7.

andywestken: Your smiley_png example is pretty much exactly what my current program produces from a source file.

I wasn't aware that I'd need to look at separate libraries to perform this kind of operation... I'm still a little intimidated by that sort of thing.
closed account (Dy7SLyTq)
on windows boost is the best option unless you download mingw or cygwin with gcc
I wasn't aware that I'd need to look at separate libraries to perform this kind of operation... I'm still a little intimidated by that sort of thing.


Something very similar to the current boost implementation is likely to be adopted in the next version of the standard, so one might as well start getting used to it. If you are using MSVC++, there is an implementation included from tr2 which you can experiment with without downloading or installing boost if you so desire (although installing and getting familiar with boost is definitely worth the effort.) The boost filesystem has gone through a major revision since then and there have been changes since tr2 which will not be reflected in the MSVC++ implementation, so it would be best to work with boost if you can (and, a major plus, the documentation for boost is superior.)

http://msdn.microsoft.com/en-us/library/hh874694
on windows boost is the best option unless you download mingw or cygwin with gcc

Out of interest, why? I don't see opendir, etc as better than FindFirstFile on, etc. And I see the cygwin alternative as worse as it means the resultant app would need to cart cygwin1.dll about with it, when a tiny utility should be self-contained.

And I see that the TR2 components are only available in Visual C++ 2012 and later, so people still working with Visual C++ 2010 (or earlier) will definitely need to use Boost.

Andy
closed account (Dy7SLyTq)
didnt say it was better. just gave him the option
By chance, I've sort of just bumped into the source for the WALK program I mentioned earlier in this post. It can be found here:

http://yahozna.dyndns.org/scratch/vc15/MSVCNT/SAMPLES/WALK/

Andy

PS It's a sample from an old version of the Microsoft Windows SDK. I bumped into a CD of version 3.1 of the SDK at the back of a draw... And googling for a fragment of the code led me to the site I mentioned above.
Topic archived. No new replies allowed.