Solved

Searching for and processing files

Posted on 1997-05-05
7
180 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
0
Comment
Question by:changg
7 Comments
 

Author Comment

by:changg
ID: 1250254
Edited text of question
0
 

Author Comment

by:changg
ID: 1250255
Edited text of question
0
 

Author Comment

by:changg
ID: 1250256
Edited text of question
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 10

Expert Comment

by:RONSLOW
ID: 1250257
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
 

Author Comment

by:changg
ID: 1250258
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
 
LVL 3

Expert Comment

by:LucHoltkamp
ID: 1250259
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
 
LVL 4

Accepted Solution

by:
emmons earned 100 total points
ID: 1250260
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

785 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