a file count

D

Darren

Hi Group,

Any ideas how to check for files in a directory, to see if any are of the
same prefix, and make a count of them.

I have a round 100 files, the filename is made up of an engineer number
followed by a contract number.

I want to see how many files each engineer has:

while (! end of list)
{
if (file_name_prefix == engineer_id)
{
engineer_id++;
}
}

any ideas VERY appreciated.

Many Thanks,

Darren
 
F

Fred L. Kleinschmidt

Darren said:
Hi Group,

Any ideas how to check for files in a directory, to see if any are of the
same prefix, and make a count of them.

I have a round 100 files, the filename is made up of an engineer number
followed by a contract number.

I want to see how many files each engineer has:

while (! end of list)
{
if (file_name_prefix == engineer_id)
{
engineer_id++;
}
}

any ideas VERY appreciated.

Many Thanks,

Darren

There are probably as many ways of doing this as there are programmers.
Note that for n files, there may be n different engineers.

One way:
-read the entire list
-allocate arrays for n enfineer id's and n counters (preset to zero).
-sort the list (may not be necessary, if the directory read is already
sorted)
-for first item, store engineer id and set counter to 1.
-for each other item in turn, if it has the same engineerid, increment
her counter.
if new id, increment array positions and store id and set new counter
to 1.
 
P

Peter Pichler

Darren said:
Any ideas how to check for files in a directory, to see if any are of the
same prefix, and make a count of them.

Directories are off-topic here, C as described in the Standard knows nothing
about them. See your compiler's documentation on how to walk a directory on
your operating system.

Checking for a filename prefix, however, is easily done in standard C.
I have a round 100 files, the filename is made up of an engineer number
followed by a contract number.

I want to see how many files each engineer has:

Provided you have built the list using some platform specific extensions,
_and_ provided the definition:

const char engineer_id[] = "ABCD";
while (! end of list)
{
if (file_name_prefix == engineer_id)

if (strncmp(file_name, engineer_id, sizeof engineer_id - 1) == 0)
{
engineer_id++;
}
}

any ideas VERY appreciated.

Use strlen instead of sizeof if engineer_id is not constant. (There are two
differences between the two: one syntactical and one semantical. Details
are left as an exercise for you.)

You may also consider changing the logic slightly if the engineer's id is
not at the fixed position in the file name.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,093
Messages
2,570,607
Members
47,227
Latest member
bluerose1

Latest Threads

Top