I've been trying all night to get this to work, but everything i do give me a compile or runtime error.

currently I'm using file_size() (no idea what to include inside the brackets).

Also if you have can help with any of the following it would help me greatly:

-list the files in reverse order
-show all files (including hidden files)(dont know if its doing that now, if so then i need to switch it so its off by default)
-how to display the filename (example.txt)

#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
using namespace boost::filesystem;

#include <boost/regex.hpp>

void show_files_withR( const path & directory, bool recurse_into_subdirs = true)
{
  if( exists( directory ) )
  {
    directory_iterator end ;
    for( directory_iterator iter(directory) ; iter != end ; ++iter )
      if ( is_directory( *iter ) )
      {
		cout << "\nd: " << setw(5) << right << iter->path() ;
        if( recurse_into_subdirs ) show_files_withR(*iter) ;
      }
      else 
		cout << "\n-: " << setw(5) << right << iter->path() ;
  }
}

int main(int argc, char *argv[])
{
	if (argc != 4)
	{
		cout << "Usage: program.exe [switches] [path] [filespec]" << endl;
	}
	else
	{
		string s_switches(argv[1]);
		string s_path(argv[2]);
		string s_filespec(argv[3]);

		bool aSwitch = false;
		bool rSwitch = false;
		bool RSwitch = false;
		bool lSwitch = false;

		//set switches
		if (s_switches == "-a")
		{
			bool aSwitch = true;
			cout << "switch -a is: " << aSwitch << endl;
		}
		if (s_switches == "-r")
		{
			bool rSwitch = true;
			cout << "switch -r is: " << rSwitch << endl;
		}
		if (s_switches == "-R")
		{
			bool RSwitch = true;
			cout << "switch -R is: " << RSwitch << endl;
		}
		if (s_switches == "-l")
		{
			bool lSwitch = true;
			cout << "switch -l is: " << lSwitch << endl;
		}

		//boost::regex expr(s_filespec);
		//fs::path currentPath(s_path);
		//cout << currentPath << endl;
		//fs::basic_directory_iterator(s_path);


		//uintmax_t file_size(const path&)

		const path & directory = s_path;
		if( exists( directory ) )
		{
			directory_iterator end;
			for( directory_iterator iter(directory) ; iter != end ; ++iter )
				if ( is_directory( *iter ) )
				{
					cout << "\nd: " << setw(5) << right << file_size(s_path) << iter->path();
					if(RSwitch) show_files_withR(*iter);
				}
				else 
					cout << "\n-: " << setw(5) << right << iter->path();
		}

	}	

	cin.ignore();
	return 0;
}

Recommended Answers

All 14 Replies

What are the errors?

adam, do u have msn or something? it would be a hell of a lot easier to talk over that, my email is <snipped>

as for the error its a debug error saying it crashed on runtime in an unusual way.

Getting some food right now.

Use some print statements in your code to figure out where you are getting errors.

I'll try to help when I get back but I have some work to do so I'll only be able to stop in here when I feel like procrastinating.

i know for sure its the file_size() thats causing the error, i just took it out and it runs fine

You're passing file_size the root directory path. I don't know that it will accept a directory. I don't think so. You should probably only use it if it's is a file and use this: iter->path()

ya funny, i just got it to work as you posted that... anyways i'm having a little problem printing out the sub directories. and ideas why?

EDIT: actually is there another way to do the "process all folders recursively" part without using an outside method, i can't assign switch variables to the method due to it not being inside the main class. so can you show me how to add it on to the one inside the class

I don't know what you are asking. Verbosity is a good thing when asking a technical question. You should probably have a function for each of the switches but you need to list what each is for. Can there be multiple switches?

There is a function i have at the top it is being used to go into sub directory's. For some reason it isn't working now, I wanted to have the whole program work within the "main" class. So i need that function at the top to be worked into the one inside the class. Also it is possible to have more then one switch at a time, so if that happens, then i would want to turn on the features inside that one loop. This cannot be done if its outside of the main (as far as i know).

Clear things up?

I think you are going in the wrong direction. You should be creating functions, like your show_files_withR function, instead of integrating it into your main function (classes are a very different thing).

What does each of the switches do? You can pass switches as parameters to your function, if you need access to them in there.

but i can have say -lRa together so, your saying to have every function for each combo possible?

As for what each switch does:

• R: process all folders recursively.
• r: list the files in reverse order.
• a: show all files (including hidden files)
• l: show the following details:
o Display the letter ‘d’ if the file is a directory, dash (‘-‘) otherwise.
o Display the size of the file in bytes
o Display the folder name after the file name if the –R switch was chosen.

I guess it would be easier to have many different functions but then i still have the problem with regular expressions, those would still have to be passed up, its just impossible to have a function for every outcome for that!

Now that I see what the switches do, no you wouldn't want functions for each operation.

Start with the simplest task and forget how you will integrate the switches into everything. Take it one step at a time.

Can there be no switches? Is the program to just display the files in one directory in that case? If so, that's a good place to start. If not, start with displaying all files or displaying files in reverse order. Then build from there.

Well there always has to be one swith, right now i'm going to work with the -l switch, i have it already displaying the d: or -: and file size, just need to know how to display the folder name its in, not the whole path. And what would i use to go in reverse order?

EDIT: also should i use a function or not?

EDIT2: ok i was stupid, i figured out how to pass up data to the function. anyways, i still need to know how to show only the folder its in and just remembered i need to know how to just show the file. so it should look likethis:

d:           subdir1                    ../dir1
-:  381,490 test.psd                    ../dir1/subdir1

ok i figured out that you can get the file only by using iter->leaf(); but still need to know how to get the folder the file/directory is in.

Also i can't seem to get it to recurse into sub directories.
This is what i go so far:

void show_files (const path & directory, bool recurse_into_subdirs, bool lSwitch, bool rSwitch, bool aSwitch)
{
  if( exists( directory ) )
  {
    directory_iterator end ;
    for( directory_iterator iter(directory) ; iter != end ; ++iter )
      if ( is_directory( *iter ) )
      {
		cout << "\nd: " << setw(5) << right << iter->path() ;
        if( recurse_into_subdirs ) show_files(*iter, recurse_into_subdirs, lSwitch, rSwitch, aSwitch) ;
      }
      else 
		cout << "\n-: " << setw(5) << right << file_size(iter->path()) << iter->leaf() ;
  }
}

ok i found out whats causing the problem with recursing into the sub directories,

This works

show_files(s_path, true, lSwitch, rSwitch, aSwitch);

This doesn't work

show_files(s_path, RSwitch, lSwitch, rSwitch, aSwitch);

Ok i got the above to work.

Just to get back on track and clear up any confusion in the thread this is what i have left to do and could use help on:

1. SWITCHES:
r: list the files in reverse order.
a: show all files (including hidden files)
l: show the following details:
DONE -Display the letter ‘d’ if the file is a directory, dash (‘-‘) otherwise.
DONE -Display the size of the file in bytes
-Display the folder name after the file name if the –R switch was chosen.

2. add in regular expressions
3. make path and regular expression in 1 cmd line arg, this means finding a way to split the 2
4. multiple switches, same as 3(each switch needs to be split up)

current code:

#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
#include <locale>
using namespace std;

#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
#include <boost/filesystem/operations.hpp>
using namespace boost::filesystem;

#include <boost/regex.hpp>

void show_files (const path & directory, bool recurse_into_subdirs, bool lSwitch, bool rSwitch, bool aSwitch)
{
	if(exists(directory))
    {
      directory_iterator end ;
      for(directory_iterator iter(directory) ; iter != end ; ++iter)
        if (!is_directory(*iter))
        {
			cout << setw(6) << left << (lSwitch == true?"\n-: ":"\n") << setw(15) << right << file_size(iter->path()) << setw(100) << left << iter->leaf() ;
        }
        else
		{
			cout << setw(6) << left << (lSwitch == true?"\nd: ":"\n") << setw(15) << right  << setw(100) << left << iter->leaf() << "/" ;
			if(recurse_into_subdirs) show_files(*iter, recurse_into_subdirs, lSwitch, rSwitch, aSwitch) ;
		}
    }
}

int main(int argc, char *argv[])
{

	locale local("");
	cout.imbue(local);

	if (argc != 4)
	{
		cout << "Usage: program.exe [switches] [path] [filespec]" << endl;
	}
	else
	{
		string s_switches(argv[1]);
		string s_path(argv[2]);
		string s_filespec(argv[3]);

		bool aSwitch = false;
		bool rSwitch = false;
		bool RSwitch = false;
		bool lSwitch = false;

		//set switches
		if (s_switches == "-a")
		{
			aSwitch = true;
		}
		if (s_switches == "-r")
		{
			rSwitch = true;
		}
		if (s_switches == "-R")
		{
			RSwitch = true;
		}
		if (s_switches == "-l")
		{
			lSwitch = true;
		}

		show_files(s_path, RSwitch, lSwitch, rSwitch, aSwitch);

	}	

	cin.ignore();
	return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.