Solved

Find files and rename them!

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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 clear a vector as well as how to detect empty vectors in C++.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now