How to read all files and all subfolders (including their files)

My task is going to read all the .wav files and all text files without extension (like those in Linux), read them and process them.

For each pair of wave file and text file with the same name, I create a new text file with the name similar to names of that pair. For example:

Currently I'm in my working folder .\, now I have these files:
.\1\s1.wav
.\1\s1
.\1\s2.wav
.\1\s2
.\2\s3.wav
.\2\s3
.\2\s4.wav
.\2\s4
.\3\s5.wav
.\3\s5

which means there are 3 subfolders in my working folder, .\1\, .\2\, .\3\. In each subfolder, there is some pair(s) of sample files s*.wav and s*. Like I search for the s1.wav and s1, then read them, and process and put my output into a new file I should create, s1.txt.

How to do this task? It's urgent, thank you!

I tried to find some boost library available, but I don't know how to include them into my project. Is these helpful:
http://www.drdobbs.com/cpp/184404797#l4
Regular Expressions in C++
http://www.boost.org/
Boost C++ Libraries


My working environment:
Microsoft Visual Studio 2008
Windows 7 English Professional
Why are you bothering with boost? This is as simple as, in your case (Win32), FindFirstFile(...) FindNextFile(...). They can be found here:

FindFirstFile(...): http://msdn.microsoft.com/en-us/library/aa364418(VS.85).aspx

FindNextFile(...): http://msdn.microsoft.com/en-us/library/aa364428(VS.85).aspx

NOTE: To get Windows to run a file without an extention you call the program that will be executing it then pass the file name to it as an argument. This is done with CreateProcess(...) which can be found here:
http://msdn.microsoft.com/en-us/library/ms682425(VS.85).aspx
Last edited on
I'd like to point out that being as clear in your intentions as you can is a very good idea on this site, especially in cases like yours:
My task is going to read all the .wav files and all text files without extension (like those in Linux), read them and process them.

The bold parts pretty much describe making modifications to the host file on a winodws machine. This is an old trick used to redirect users to particular websites with out the need for a Proxy Filter between the target and the world; needless to say this is a method usually used for malicious or disruptive purposes and we tend to look down on such ventures here.
Last edited on
But I still don't know how to search files in those subfolders.

Now I know how to search files under the given folder, like in above example, it's just returning three folder names:
The first file found is .
The next file found is ..
The next file found is 1
The next file found is 2
The next file found is 3


Then how can I manipulate with these output, and continue search these 3 subfolders?

Thank you !! ToT

I try to solve this problem by recursively call a function, search_this_folder(), which I didn't finish in the following coding. I just want to get reassured from you about whether this is the right way to solve this problem. Thank you! Suppose this coding is in
C:\exercise\test.cpp

Then, to run it, first open command line prompt (press [Ctrl + R] and type cmd), switch to:
cd C:\exercise\

Then, type:
test.exe "C:\exercise\data\*"


Suppose those folders and subfolders and there files are in the C:\exercise\data\ directory:
C:\exercise\data\1\s1.wav
C:\exercise\data\1\s1
C:\exercise\data\1\s2.wav
C:\exercise\data\1\s2
C:\exercise\data\2\s3.wav
C:\exercise\data\2\s3
C:\exercise\data\2\s4.wav
C:\exercise\data\2\s4
C:\exercise\data\3\s5.wav
C:\exercise\data\3\s5

And there goes the output (something like this but not exactly the same layout):
The first file found is .
The next file found is ..
The next file found is 1
The next file found is 2
The next file found is 3


Here is my coding. Microsoft Visual Studio 2008. Windows 7 Pro English.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iomanip>
#include <iostream>
#include <windows.h>
#include <tchar.h>
#include <stdio.h>

using namespace std;

int
search_this_folder()
{
	return 0; // succeed
}


//void main(int argc, char *argv[]) // ??
void _tmain(int argc, TCHAR *argv[]) // ??
// argc = argument count
// argv = argument vector
{
   WIN32_FIND_DATA FindFileData;
   HANDLE hFind;
   DWORD dwError;

   cout << "The name used to start the program (ie argv[0]) is: "; // << argv[ 0 ] << "\n";
   _tprintf (TEXT("%s \n"), argv[ 0 ]);
   // Should use _tprinf() instead of cout. _tprinf() is another name for wprintf.
   // Because the type of argv[] is TCHAR*, not char*
   cout << "Arguments are:" << "\n";

   for (int i = 1; i < argc; i++)
   {
     cout << setw( 2 ) << i << ": ";
	 _tprintf (TEXT("%s \n"), argv[ i ]);
   }

   cout << "argc (argument count) = " << argc << "\n";
	
   // If usage syntax is wrong
   if ( argc != 2 )
   {
      _tprintf(TEXT("Usage: %s [target_file]\n"), argv[ 0 ]);
	  cout << "Usage should be: ";
      _tprintf (TEXT("%s"), argv[ 0 ]);
	  cout << " [target_file]" << "\n";
      return;
   }
	
   // If usage syntax is correct
   _tprintf (TEXT("Target file is %s\n"), argv[1]);
   
   hFind = FindFirstFile(argv[1], &FindFileData);
   if ( hFind == INVALID_HANDLE_VALUE ) // that is the first file does not exist
   {
      printf ("FindFirstFile failed (%d)\n", GetLastError());
      return;
   } 
   else // that is the first file is found
   {
      _tprintf (TEXT("The first file found is %s\n"), 
                FindFileData.cFileName);
	  search_log = search_this_folder(); //
	  while (FindNextFile(hFind, &FindFileData) != 0)
	  {
	     _tprintf (TEXT("The next file found is %s\n"), 
		           FindFileData.cFileName);
		 search_log = search_this_folder();
	  }
	  
	  dwError = GetLastError();
      FindClose(hFind);
	  if (dwError != ERROR_NO_MORE_FILES) 
      {
         printf ("FindNextFile error. Error is %u\n", dwError);
      }
   }
}

Topic archived. No new replies allowed.