473,573 Members | 2,388 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Rename a file in a directory

saranjegan
51 New Member
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 4974
saranjegan
51 New Member
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 Recognized Expert Moderator Specialist
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
saranjegan
51 New Member
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
17860
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 want to rename it 2004_08_003.jpg. I can understand how to do it to this extent: Ch Dir () ' whatever directory these files are located in Dim...
7
2676
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 " & (format(Date(),"yymmdd")) & ",pdf" So that the file name becomes: "Directory 050126.pdf" I have tried the NAME {oldfilename} AS {newfilename} as an event...
5
2725
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 called with an in-use file). However, what happens is that a copy of the file is made, and the old file still exists. For example: ...
3
6715
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 would like to create a record in mysql database. I've tried to find some tutorials but I've had trouble finding any good ones. Wondering if anybody...
3
3608
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 the directories is named 'mydir' (without the quotes). Note that the all the letters in the directory name are in lowercase. To rename any...
1
2147
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. Attached is the code : # directory structure for input and archive files $inputDir = "\\interac\\Data\\imntran\\".$ARGV; $archDir =...
5
2279
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 names that matches $ARGV.I need help on how to rename the files .For ex if argumnets are a bbb if a file name is rename.txt then O/P must be...
2
1518
by: shapper | last post by:
Hello, I am using Visual Studio 2008 and Web Deployment Projects: http://blogs.msdn.com/webdevtools/archive/2007/12/01/web-deployment-projects-wdp-for-visual-studio-2008-december-2007-ctp-released.aspx Is it possible to rename a directory and a folder when the web site building finishes? I was trying to do something inside the <Target...
1
5907
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 which simply moves a zip file (located in the same directory as the source code) to another directory by: <?php $sourcecode =...
4
5258
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 example .././latest/../.. 3) rename the file, no matter what name it has - to play.html So far I got the upload working and the file is uploaded...
0
7747
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7670
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8036
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5557
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5263
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3706
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3703
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2167
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1005
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.