Go Premium for a chance to win a PS4. Enter to Win

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 187
  • Last Modified:

Searching for and processing files

I want to search for several files in a directory, perform some tasks on them and then copy each of those files to a new file name but still maintaining the dos 8.3 format for example (setup.cnt to setup_m.cnt). Then I want create a  log file with the new file names in it. I am using a find file method that comes with the C Run time library in Visual C++ 5.0, but I am having trouble passing the file names to other functions that will perform the required tasks. My knowledge of manipulating files in C is not great, so can someone PLEASE HELP!!!!,

here is a copy of the code I already wrote:
#include <io.h>
#include <stdio.h>
#include <string.h>

 void main( void )
   struct _finddata_t cnt_file;
   long hFile;
   char *new_file[13];
      FILE *out_file;

    /* Find first .cnt file in current directory */
    if( (hFile = _findfirst( "*.cnt", &cnt_file )) == -1L )
       printf( "No cnt files in current directory!\n" );
            printf( "Listing of .cnt files\n\n" );
              printf( " %-12s\n", cnt_file.name );

            /* Find the rest of the .cnt files */
            while( _findnext( hFile, &cnt_file ) == 0 )
                  printf( " %-12s %\n", cnt_file.name );

       _findclose( hFile );
         return (FILE *cnt_file);
   copy_files (&cnt_file, &new_file[]);
   create_log (&new_file[]);


1 Solution
changgAuthor Commented:
Edited text of question
changgAuthor Commented:
Edited text of question
changgAuthor Commented:
Edited text of question
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Firstly, you have ignored the file returned by the call to _findfirst, and just gone straight onto _findnext - so you'll miss the first file found.

Try using a do{...}while(...) instead of a while(...){...}

Also, I'd suggest you use the Win32 API functions (which you must have as you are using VC5) like this...

WIN32_FIND_DATA filefinddata;
HANDLE h = ::FindFirstFile("*.cnt",&filefinddata);
  do {
    /* do something with filefinddata.cFileName; */
  } while (::FindNextFile(h,&filefinddata));

changgAuthor Commented:
When storing the file names in an array what method could I use to create new files names (file_m.cnt),  or if the file name is already 8.3 (filename.cnt to filena_m.cnt).
First I assume you would place your functions within the loop after the printf's right ??
Second, you declare new-file as an array of char pointers instead of an array of char's
Third you use &new_file[] to pass the adress of the string to the functions. I don't think thats correct, it should be &new_file[0] or simply new_file. Thus:
//declaration of function:
void editFiles(struct _finddata_t &fd, char *string);
// usage:
editFiles(cnt_file, new_file);

A very simple methode to create a new filename is to add a underscore to it. So afile.txt becomes _afile.txt
And if the file is already 8 char long scroll the name to the right. Some code for you:

void makeFileName(char *newName, char *oldName)
  int i;
  char buffer[14];
  strcpy(buffer+1, oldName);
  buffer[0] = '_';
  /* find the length of the name without extension */
  for (i = 0; oldName[i] != '\0' && oldName[i] != '.'; ++i);
  if (i == 8) /* . in 8th pos, scroll the extension left */
    for (; oldName[i] != '\0'; ++i)
      buffer[i-1] = oldName[i];
    buffer[i] = '\0';
  strcpy(newName, buffer);

OK, if you fix your code as per Ronslow (or, you already know that you are skipping the first file and you are doing it on purpose).
How about creating a new file name with sprintf.
Since you know that the file is a .CNT, you can strip that off with a strchr( oldName, '.') = '\0';
After duplicating the string somewhere, of course.
Then something like
char NewFileName[13];
sprintf( NewFileName, "%6s_M.CNT", oldName)

will take the first six character of the root of the file name and append the "_M" that you used in your example. You can then do all of the copying and logging that you already do in your code.


Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now