Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Find files and rename them!

Posted on 2000-05-03
5
Medium Priority
?
491 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 400 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

610 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