Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

command line app

Posted on 1999-07-15
10
Medium Priority
?
238 Views
Last Modified: 2010-04-02
URGENT

i need command line app
three variables
-dir name
-dir size
-file name

it reads the dir possibly with * option
sorts the dirs with size bigger than 'dir size'
writes a file with the dir names

thanks

it's URGENT
0
Comment
Question by:acorp
[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
10 Comments
 
LVL 2

Expert Comment

by:Slarti
ID: 1200151
OS, compiler, etc.?
0
 
LVL 9

Accepted Solution

by:
jasonclarke earned 700 total points
ID: 1200152
I reckon if you were using Unix you wouldn't need this (ls and awk would do an admirable job!), so here is a PC version, using the windows API.  I have compiled it on VC++ 5, but it should work elsewhere:

#include <vector>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>

#include "windows.h"

using namespace std;

typedef pair<string,unsigned long> dirElement;

#pragma warning(disable: 4786)

vector<dirElement> FindFileList(const string& path)
{
    vector<dirElement> results;
    WIN32_FIND_DATA findData;
    HANDLE searchHandle = FindFirstFile(path.c_str(),
                                        &findData);
    if (searchHandle != INVALID_HANDLE_VALUE)
    {
        BOOL searching = 1;
        while (searching)
        {
            if (searching && !(findData.dwFileAttributes &             
                               FILE_ATTRIBUTE_DIRECTORY))
            {
                unsigned long size = (findData.nFileSizeHigh * MAXDWORD) + findData.nFileSizeLow;
                results.push_back(dirElement(findData.cFileName, size));
            };
            searching = FindNextFile(searchHandle, &findData);
        }
        FindClose(searchHandle);
    }
    return results;
}

bool dirElementCompare(const dirElement& lhs, const dirElement& rhs)
{
    return (lhs.second < rhs.second);
}

void main(int argc, char** argv)
{
    if (argc < 4) {
        cerr << "Usage : findFiles searchPath size outputFile" << endl;
        exit(0);
    }

    string path = argv[1];
    unsigned long size = atol(argv[2]);

    ofstream ofile(argv[3],ios::out);
    if (ofile.fail()) {
        cerr << "Can't open file for output" << endl;
        exit(0);
    }

    vector<dirElement> files = FindFileList(path);
    std::sort(files.begin(), files.end(), ::dirElementCompare);

    vector<dirElement>::iterator i = files.begin();
    while (i != files.end())
    {
        if (i->second > size) {
            ofile << i->first << endl;
        }
        ++i;
    }
}
0
 

Author Comment

by:acorp
ID: 1200153
f:\temp\aaa\aaa.cpp(72) : fatal error C1010: unexpected end of file while looking for precompiled header directive
Error executing cl.exe.

aaa.exe - 1 error(s), 0 warning(s)


what's this
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 9

Expert Comment

by:jasonclarke
ID: 1200154
maybe you have compiled it with Pre-compiled header support turned on.  try turning it off,

select the project, then settings, then the C++ tab, then the Pre-Compiled Headers option, then select 'Not using Pre-Compiled Headers'
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 1200155
Wonder if pre-compiled headers had anyhing to do with that (but I don't use VC, so anything would be possible ;)
'unexpected end of file' trouble usually points to a missing brace or #endif
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 1200156
VC++ does give this warning if you have pre-compiled headers switched on, but don't have the appropriate include files.
0
 

Author Comment

by:acorp
ID: 1200157
this creates an empty file
give an example of working with the progg

it compiles without errors now

i'll increase to 400
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 1200158
just:

appname c:\*.* 25000 test.dat

will produce a list of all the files in the root directory whose size is > 25000. Is this what you want?  If not tell me.
0
 

Author Comment

by:acorp
ID: 1200159
no no no
i don't need to compare the size of the file but the directories

you see
we have a server here with thousands of directories and we want to tracks the sizes of the bigger ones

we need to calculate the size of the whole directory not just a single file within
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 1200160
Hopefully, the following will work.  It does a recursive file search on the directories, so the size returned for a directory is the size of all the files in that directory + the size of all the files in its sub-directories.  Let me know if this is not what you want.

Note you must specify the wildcard characters with this for it to work, i.e. do C:\*.* rather that just C:\.

We could proceed much quicker with more than 1 update per day!


#include <vector>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>

#include "windows.h"

using namespace std;

typedef pair<string,unsigned long> dirElement;

#pragma warning(disable: 4786)

string
find_directory_root(const string& path)
{
    string result = path;
    string seperator = "\\";
    string::iterator i = find_end(result.begin(), result.end(),
                                  seperator.begin(), seperator.end());
    if (i != result.end())
    {
        result = string(result.begin(), i);
    }

    return result;
}

string
find_path_end(const string& path)
{
    string result = path;
    string seperator = "\\";
    string::iterator i = find_end(result.begin(), result.end(),
                                  seperator.begin(), seperator.end());
    if (i != result.end())
    {
        result = string(i,result.end());
    }

    return result;
}

unsigned long FindDirSize(const string& path)
{
    string dirRoot = find_directory_root(path);
    string dirPath = dirRoot + "\\*.*";
    unsigned long dirSize = 0;

    WIN32_FIND_DATA findData;
    HANDLE searchHandle = FindFirstFile(path.c_str(),
                                        &findData);

    if (searchHandle != INVALID_HANDLE_VALUE)
    {
        BOOL searching = 1;
        while (searching)
        {
            if (searching && !(findData.dwFileAttributes &             
                               FILE_ATTRIBUTE_DIRECTORY))
            {
                unsigned long size = (findData.nFileSizeHigh * MAXDWORD) + findData.nFileSizeLow;
                dirSize += size;
            };
            searching = FindNextFile(searchHandle, &findData);
        }
        FindClose(searchHandle);
    }
    return dirSize;
}

vector<dirElement> FindDirectorySizes(const string& path)
{
    vector<dirElement> results;
    string dirRoot = find_directory_root(path);
    string dirPath = dirRoot + "\\*.*";

    unsigned long size = FindDirSize(dirPath);

    WIN32_FIND_DATA findData;
    HANDLE searchHandle = FindFirstFile(dirPath.c_str(),
                                        &findData);

    if (searchHandle != INVALID_HANDLE_VALUE)
    {
        BOOL searching = 1;
        while (searching)
        {
            if (searching && (findData.dwFileAttributes &             
                              FILE_ATTRIBUTE_DIRECTORY))
            {
                string name = findData.cFileName;
                if (!(name == "." || name == ".."))
                {
                    string dirName = dirRoot+"\\"+name;
                    vector<dirElement> subDirs = FindDirectorySizes(dirName+"\\*.*");
                    vector<dirElement>::iterator i =  subDirs.begin();
                    while (i != subDirs.end())
                    {
                        results.push_back(*i);
                        size += i->second;
                        ++i;
                    }
                }
            }
            searching = FindNextFile(searchHandle, &findData);
        }
        FindClose(searchHandle);
    }
    results.push_back(dirElement(dirRoot + "\\", size));

    return results;
}

bool dirElementCompare(const dirElement& lhs, const dirElement& rhs)
{
    return (lhs.second > rhs.second);
}

void main(int argc, char** argv)
{
    if (argc < 4) {
        cerr << "Usage : findFiles searchPath size outputFile" << endl;
        exit(0);
    }

    string path = argv[1];
    unsigned long size = atol(argv[2]);

    ofstream ofile(argv[3],ios::out);
    if (ofile.fail()) {
        cerr << "Can't open file for output" << endl;
        exit(0);
    }

    vector<dirElement> files = FindDirectorySizes(path);
    std::sort(files.begin(), files.end(), ::dirElementCompare);

    vector<dirElement>::iterator i = files.begin();
    while (i != files.end())
    {
        if (i->second > size) {
            ofile << i->first << "\t" << i->second << endl;
        }
        ++i;
    }
}
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

670 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