[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 189
  • 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" );
   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
0
changg
Asked:
changg
1 Solution
 
changgAuthor Commented:
Edited text of question
0
 
changgAuthor Commented:
Edited text of question
0
 
changgAuthor Commented:
Edited text of question
0
Easily manage email signatures in Office 365

Managing email signatures in Office 365 can be a challenging task if you don't have the right tool. CodeTwo Email Signatures for Office 365 will help you implement a unified email signature look, no matter what email client is used by users. Test it for free!

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


0
 
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).
0
 
LucHoltkampCommented:
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);
}

0
 
emmonsCommented:
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.

0

Featured Post

The IT Degree for Career Advancement

Earn your B.S. in Network Operations and Security and become a network and IT security expert. This WGU degree program curriculum was designed with tech-savvy, self-motivated students in mind – allowing you to use your technical expertise, to address real-world business problems.

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