Solved

Find files and rename them!

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

914 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

16 Experts available now in Live!

Get 1:1 Help Now