Solved

Find files and rename them!

Posted on 2000-05-03
5
457 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

809 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