• C

function to Rename file using wild card character like ren *.dat to *.txt

I want to rename the files like *.dat to *.txt. Pls. suggest the function for the same that works on both windows as well as unix.
rename function only rename single file.

thanks in advance!
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hi deepakg76,

there is no such function in ANSI C ... you can perhaps try system ("rename ... ");

Kent OlsenDBACommented:

Linux tools will let you make this change with a single command:

rename .dat .txt *.dat

Windows has no such equivalent, you'll have to write a program or script to do this.

for i in `ls directory_name`
     mv $i.dat $.txt

will convert all dat files in a directory to txt files (extensions)
10 Holiday Gifts Perfect for Your Favorite Geeks

Still have some holiday shopping to do for the geeks in your life? While toys, clothing, games, and gift cards are still viable options for your friends and family, there’s more reason than ever to consider gadgets and software.

SOLUTION FOR UNIX .. for windows use findfirst and findnext functions instead of  dir functions

#include <sys/stat.h>
#include <dirent.h>
void process_dir(char *directory_path)
     DIR* dirp =  NULL;
     struct dirent* dp;
     struct stat buf;
     char  file_name[64];
     char  old_file_name[64];
     char  new_file_name[64];
     char * temp;
     dirp = opendir(directory_path);
     if(dirp == NULL)     return ;
      while (dp = readdir(dirp))
        printf("FILE NAME %s\n",dp->d_name);
        printf("FILE TYPE  %s\n",(buf.st_mode & S_IFREG)? "REGULAR FILE ":"DIRECTORY");
        if(buf.st_mode & S_IFREG)
               temp = strchr(file_name,'.');
               if (temp  &&   !strcmp(temp,".dat"))
                     // we have found a file name ending with '.dat'
                     //terminate  Filename with '\0'
                     *temp = '\0';
                     //create old and new new filenames for rename

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kent OlsenDBACommented:

Hi Ajar,

Nice code, written quickly.  :)

It does miss files with two periods.  "2003.11.12_TestScores.dat"  will be missed do to the test finding the first period.

  temp = strchr (file_name, '.');
  temp1 = temp;
  while (temp1)
    temp1 = strchr (temp1);
    if (temp1)
      temp = temp1;

should solve that.
not sure about windows atm, but I used scandir() under Unix to get the expressions for what I needed.

#include <stdio.h>
#include <dirent.h>
#include <string.h>

int my_sort( const void *_a, const void *_b)
    struct dirent **a = (struct dirent **)_a;
    struct dirent **b = (struct dirent **)_b;


int my_select( const struct dirent *a )
    char *cs = NULL;

        //printf("%s\n", a->d_name);
    cs = strrchr(a->d_name, '.');
    if (cs == NULL)
        return 0;

    if (strcasecmp(cs,".dat") == 0)
        return 1;

    return 0;

int main( void )
     struct dirent **namelist;
     int n = 0;

     n = scandir(".", &namelist, my_select, my_sort);
     if (n < 0)
          fprintf(stderr, "Error: Scandir()\n");

     while ( n--)
     // you rename code for namelist[n]->d_name
     printf("%s\n", namelist[n]->d_name);

     return 0;
this will handle all files like....



the my_sort() is just there for a complete example, you can just use alphasort() if you wish
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.