Solved

Find files and rename them!

Posted on 2000-05-03
5
473 Views
Last Modified: 2012-06-21
Can anybody help me whith a small problem?

I need to create a program that finds .trc files on the disk. I have tried the FindFirstFile() method but I dont seem to get it right. There are som problems whith the headerfiles as well. Plz tell me witch HFiles to include.

Lets say that I get the Find function working. How do I change the name on the file?

Here is my code.

#include <winbase.h>
#include "Control.h"
#include <stdio.h>
#include "stdafx.h"
#include <fstream.h>

void main( void )
{
    WIN32_FIND_DATA trc_File;
    HANDLE hFile;

      if((hFile = FindFirstFile("*.trc", &trc_File)) == NULL )
      {
         printf( "No *.trc files in current directory!\n" );
      }
      else
      {
            /* Find the rest of the .trc files */

      while( FindNextFile ( hFile, &trc_File ) == 0 )
        {
      
        }

       FindClose( hFile );
   }
}
0
Comment
Question by:Micke_Cap
  • 2
  • 2
5 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 2773541
You should #include <windows.h> instead of winbase.h ...
0
 
LVL 9

Accepted Solution

by:
jasonclarke earned 100 total points
ID: 2773599
Here is a version of your code that will rename all the "*.trc" files to "*.xxx".

#include <windows.h>
#include <stdio.h>
#include <fstream.h>

#include <string>

void main( void )
{
    WIN32_FIND_DATA trc_File;
    HANDLE hFile;

    hFile = FindFirstFile("*.trc",&trc_File);

    if(hFile == INVALID_HANDLE_VALUE )
    {
       printf( "No *.trc files in current directory!\n" );
    }
    else
    {
    /* Find the rest of the .trc files */

        do
        {
            std::string oldFileName = trc_File.cFileName;
            std::string newFileName = oldFileName;
            int pos = newFileName.find_last_of(".");
            newFileName.replace(pos,4,".xxx");
            ::rename(oldFileName.c_str(), newFileName.c_str());
        } while( FindNextFile ( hFile, &trc_File ));

       FindClose( hFile );
   }
}


0
 

Author Comment

by:Micke_Cap
ID: 2776314
Before i give you the points.

Does this code go though the entire drive or just the current directory??

Mike
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 2779884
Just the current directory,  I just adapted the structure you gave to make it work.  To get it to search the entire drive you need to make the search filter out directories and then work with them.

Let me know if you want help with this.
0
 

Author Comment

by:Micke_Cap
ID: 2779974
Ok thanks.
I'll let you know when I need it.

//Mike
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

679 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