Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2003-11-13
9
Medium Priority
?
616 Views
Last Modified: 2010-04-15
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!
0
Comment
Question by:deepakg76
[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
  • 2
  • +1
9 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9738507
Hi deepakg76,

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

Cheers!
Sunny:o)
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 9739110

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.

Kent
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9739148
for i in `ls directory_name`
do
     mv $i.dat $.txt
done

will convert all dat files in a directory to txt files (extensions)
0
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.

 
LVL 6

Accepted Solution

by:
Ajar earned 80 total points
ID: 9739257
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);
        stat(dp->d_name,&buf);
        printf("FILE TYPE  %s\n",(buf.st_mode & S_IFREG)? "REGULAR FILE ":"DIRECTORY");
        if(buf.st_mode & S_IFREG)
        {
               strcpy(file_name,dp->d_name);
               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
                     sprintf(old_File_name,"%s/%s.dat",directory_path,file_name);
                     sprintf(new_File_name,"%s/%s.txt",directory_path,file_name);
                     rename(old_file_name,new_file_name);
               }
        }
       
     }
        closedir(dirp);
}
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 9739330

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.
Kent
0
 
LVL 5

Expert Comment

by:g0rath
ID: 9739787
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;

    return(strcmp((*a)->d_name,(*b)->d_name));
}

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");
          exit(1);
     }

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

     return 0;
}
0
 
LVL 5

Expert Comment

by:g0rath
ID: 9739809
this will handle all files like....

1.dat
1.2.3.dat


etc...

the my_sort() is just there for a complete example, you can just use alphasort() if you wish
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

721 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