473,321 Members | 1,667 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

Rename a file in a directory

saranjegan
hi,
this is the task....
rename every file in a directory by adding ascii value of 23 to each of them
this is my work on this : this code is running without an error but its not getting renamed...i want to know whethet this logic works or some violation errors..

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<dirent.h>
  4. int main(void)
  5. {
  6.     DIR *dfd=opendir(".");
  7.     struct dirent *dp;
  8.     char *oldname,*newname;
  9.     clrscr();
  10.     if(dfd!=NULL)
  11.     {
  12.             while((dp=readdir(dfd))!=NULL)
  13.             {
  14.              oldname=dp->d_name;
  15.              //printf("%s\n",oldname);
  16.              while(*oldname!='\0')
  17.               {
  18.               *newname=(*oldname)+23;
  19.               oldname++;
  20.               newname++;
  21.               }
  22.               i='\0';
  23.  
  24.               if((rename(dp->d_name,newname))==0)
  25.               {
  26.               printf("\n %s is renamed as %s ",dp->d_name,newname);
  27.               }
  28.                     else
  29.                     perror("rename");
  30.             }
  31.              closedir(dfd);
  32.     }
  33.  
  34. getch();
  35. return 0;
  36. }
Feb 15 '07 #1
3 4962
this is the modified code sorry for the previous

#include <s
Expand|Select|Wrap|Line Numbers
  1. dio.h>
  2. #include <conio.h>
  3. #include<string.h>
  4. #include<dirent.h>
  5. int main()
  6. {
  7.     struct dirent *dp;
  8.     int len=0;
  9.     int i=0;
  10.     char *oldname,*newname;
  11.     DIR *dfd=opendir(".");
  12.     if(dfd!=NULL)
  13.     {
  14.         while((dp=readdir(dfd))!=NULL)
  15.         {
  16.           printf("%s",dp->d_name);
  17.           oldname=dp->d_name;
  18.           len=strlen(oldname);
  19.           for(i=0;i<=len;i++)
  20.           {
  21.             newname=(oldname+23);
  22.           }
  23.           i='\0';
  24.             if(rename(dp->d_name,newname)==0)
  25.             {
  26.                 printf("\n %s is renamed as %s ",dp->d_name,newname);
  27.             }
  28.           else
  29.           perror("rename");
  30.           }
  31.  
  32.  
  33.     getch();
  34.     closedir(dfd);
  35.  
  36.     }
  37.  
  38. return 0;
  39. }
Feb 15 '07 #2
sicarie
4,677 Expert Mod 4TB
this is the modified code sorry for the previous

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include<string.h>
  4. #include<dirent.h>
  5. int main()
  6. {
  7.     struct dirent *dp;
  8.     int len=0;
  9.     int i=0;
  10.     char *oldname,*newname;
  11.     DIR *dfd=opendir(".");
  12.     if(dfd!=NULL)
  13.     {
  14.         while((dp=readdir(dfd))!=NULL)
  15.         {
  16.           printf("%s",dp->d_name);
  17.           oldname=dp->d_name;
  18.           len=strlen(oldname);
  19.           for(i=0;i<=len;i++)
  20.           {
  21.             newname=(oldname+23);
  22.           }
  23.           i='\0';
  24.             if(rename(dp->d_name,newname)==0)
  25.             {
  26.                 printf("\n %s is renamed as %s ",dp->d_name,newname);
  27.             }
  28.           else
  29.           perror("rename");
  30.           }
  31.  
  32.  
  33.     getch();
  34.     closedir(dfd);
  35.  
  36.     }
  37.  
  38. return 0;
  39. }
You declare a struct, but I don't see the implementation anywhere, can you post that?
Feb 15 '07 #3
You declare a struct, but I don't see the implementation anywhere, can you post that?
Dirent is an structure defined in header file dirent.h which contains null terminated file names in the corresponding directory......
Feb 16 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Larry R Harrison Jr | last post by:
I have Access XP. I know Visual Basic quite well. One thing I know Access can do--and I can't quite figure out how--is to rename the files in a directory. If the file is named "aug01_003.jpg" I...
7
by: Chuck | last post by:
Problem: to rename a file sent to a PDF writer A macro opens a report in print mode the printer is a pdf writer The report name is: "Directory.pdf" The desired name is: "Directory " &...
5
by: Tony Meyer | last post by:
On Windows, if I do os.rename(old, new) where old is a file that is in-use (e.g. python itself, or a dll that is loaded), I would expect that an error would be raised (e.g. as when os.remove is...
3
by: gsoguerrilla | last post by:
Hi, I have limited knowledge in php and I am having trouble with uploading an image to a remote directory and resizing it if it's larger and renaming it to a unique id, while at the same time I...
3
by: rn5a | last post by:
An inquisitive question...... A ListBox lists all the directories & files residing in a directory on the server. Assume that the ListBox lists 2 directories & 4 files. Also assume that one of...
1
by: codemaster | last post by:
Hi, I am a total novice to perl. I am moving files from one directory to another and trying to rename a file appending the timestamp. But for some reason, my code is not able to rename. ...
5
by: mythili123 | last post by:
The script will rename all files in the current directory whose names contain the first argument by replacing that part of the filename with the second argument. so far i could just get the file...
2
by: shapper | last post by:
Hello, I am using Visual Studio 2008 and Web Deployment Projects:...
1
by: Alien | last post by:
Hi, I am new to PHP and trying to move a zip file from one directory to another. I researched on the web on this and all fingers pointed to this rename() function. I tried writing a PHP script...
4
by: PHPstarter | last post by:
Hi guys. I'm having a problem with this case. I want a php script activated by a standard file submit to: 1) upload the file to ../../upload 2) copy the file to a directory further back, for...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.