We help IT Professionals succeed at work.

Searching for and processing files

changg
changg asked
on
Medium Priority
201 Views
Last Modified: 2010-04-15
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" );
   else
   {
            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[]);
   edit_new_files(&new_file[]);
   create_log (&new_file[]);
   
}

Thanks,

Garett
Comment
Watch Question

Author

Commented:
Edited text of question

Author

Commented:
Edited text of question

Author

Commented:
Edited text of question

Commented:
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);
if (h != INVALID_HANDLE_VALUE) {
  do {
    /* do something with filefinddata.cFileName; */
  } while (::FindNextFile(h,&filefinddata));
  ::FindClose(h);
}


Author

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);
}

Commented:
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.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.