473,385 Members | 1,487 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,385 software developers and data experts.

About "rename"

Hello everyone...
I'm a Chinese student and my English is very poor...So excuse me if I
make grammar mistake.

I want to ask some questions about "rename". I'm a beginner, so my C
..... :)
I want to rename a batch of files..Such as I want to
rename"1.avi,2.avi" to "Prison BreakS1E1.avi,Prison BreakS1E2.avi" in
the same folder..How can I do?

And is there a function to read a list of files in a folder in the "C
language"?

Nov 22 '06 #1
6 2494
sh***********@sina.com said:
Hello everyone...
I'm a Chinese student and my English is very poor...So excuse me if I
make grammar mistake.

I want to ask some questions about "rename". I'm a beginner, so my C
.... :)
I want to rename a batch of files..Such as I want to
rename"1.avi,2.avi" to "Prison BreakS1E1.avi,Prison BreakS1E2.avi" in
the same folder..How can I do?
Use sprintf to build the filename.
And is there a function to read a list of files in a folder in the "C
language"?
Not in the standard library, no, since the concept of "folder" simply
doesn't exist in C, but your implementation will provide a way to read file
meta-information such as you require. Consult your implementation's
documentation, or a newsgroup dealing with your implementation or platform.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Nov 22 '06 #2

sh***********@sina.com wrote:
Hello everyone...
I'm a Chinese student and my English is very poor...So excuse me if I
make grammar mistake.

I want to ask some questions about "rename". I'm a beginner, so my C
.... :)
I want to rename a batch of files..Such as I want to
rename"1.avi,2.avi" to "Prison BreakS1E1.avi,Prison BreakS1E2.avi" in
the same folder..How can I do?
for f in *.avi; do mv -f $f "Prison BreakS1E$f"; done

No C required.

NEXT!

[btw piracy is bad m'kay!]

Tom

Nov 22 '06 #3
sh***********@sina.com wrote:
Hello everyone...
I'm a Chinese student and my English is very poor...So excuse me if I
make grammar mistake.
That's okay.
I want to ask some questions about "rename". I'm a beginner, so my C
.... :)
I want to rename a batch of files..Such as I want to
rename"1.avi,2.avi" to "Prison BreakS1E1.avi,Prison BreakS1E2.avi" in
the same folder..How can I do?
The easiest way is to write a shell script or a perl program. But I
suppose you want to do it in C as a learning exercise...
And is there a function to read a list of files in a folder in the "C
language"?
Not in standard C. But practically all major operating systems provide
filesystem primitives to accomplish such tasks. Often, the platform's C
library will include easier-to-use, more portable wrappers around these
primitives. You'll have to consult your C library's documentation or a
group devoted to your platform for more details.

Doing this in a generic fashion is significantly involved, but for the
specific case you've mentioned above is easy. Try translating the
following pseudo-code to C.

CONST STRING prefix := "Prison BreakS1E";
CONST STRING suffix := ".avi";
CONST STRINGS original_names := "1.avi", "2.avi", ...;

filename[ARRAY_SIZE];
filenum[MAX_SIZE];

FOR( num := 1; num <= LIMIT; num++)
filename := prefix;
filenum := CONVERT_TO_STRING(num);
filename := CONCACTENATE(filename, filenum);
filename := CONCACTENATE(filename, suffix);
IF RENAME(original_names[num] WITH filename[]) FAILED
HANDLE_ERROR();
ELSE
CONTINUE LOOP;

Nov 22 '06 #4
sh***********@sina.com wrote:
Hello everyone...
I'm a Chinese student and my English is very poor...So excuse me if I
make grammar mistake.
Your sentence is missing an article or a plural. It should end with
"make a grammar mistake" or "make grammar mistakes". :-)
I want to ask some questions about "rename". I'm a beginner, so my C
..... :)
I want to rename a batch of files..Such as I want to
rename"1.avi,2.avi" to "Prison BreakS1E1.avi,Prison BreakS1E2.avi" in
the same folder..How can I do?
"How can I do it?" -- The "it" is important, to make the verb "do"
transitive. Wo zenme neng zuo _ta_? :-)

#include <stdio.h>

int main(void)
{
char from[128], to[128];
int i = 1;
while(1)
{
sprintf(from, "%d.avi", i);
sprintf(to, "Prison BreakS1E%d.avi", i);
i++;
if(rename(from, to) == 0)
{
printf("Renamed %s to %s\n", from, to);
}
else
{
printf("Failed to rename %s to %s\n", from, to);
break;
}
}
return 0;
}

First create some files:

C:\docs\prog\c>bash -c "for I in `seq 10`; do touch $I.avi; done"

Then compile the program:

C:\docs\prog\c>gcc -ansi -pedantic -Wall -W -O2 renavi.c -o renavi

Then run the program:

C:\docs\prog\c>renavi
Renamed 1.avi to Prison BreakS1E1.avi
Renamed 2.avi to Prison BreakS1E2.avi
Renamed 3.avi to Prison BreakS1E3.avi
Renamed 4.avi to Prison BreakS1E4.avi
Renamed 5.avi to Prison BreakS1E5.avi
Renamed 6.avi to Prison BreakS1E6.avi
Renamed 7.avi to Prison BreakS1E7.avi
Renamed 8.avi to Prison BreakS1E8.avi
Renamed 9.avi to Prison BreakS1E9.avi
Renamed 10.avi to Prison BreakS1E10.avi
Failed to rename 11.avi to Prison BreakS1E11.avi

Since 11.avi doesn't exist, it fails at that point and breaks from the loop.
And is there a function to read a list of files in a folder in the "C
language"?
No.

--
Simon.
Nov 22 '06 #5
"Tom St Denis" <to********@gmail.comwrote:
sh***********@sina.com wrote:
I want to ask some questions about "rename". I'm a beginner, so my C
.... :)
I want to rename a batch of files..Such as I want to
rename"1.avi,2.avi" to "Prison BreakS1E1.avi,Prison BreakS1E2.avi" in
the same folder..How can I do?

for f in *.avi; do mv -f $f "Prison BreakS1E$f"; done
Not likely to work on the systems where AVIs are most commonly found.
There is a similar solution on those systems, but it's just as off-topic
here as yours.

Richard
Nov 22 '06 #6
>sh***********@sina.com wrote:
>I want to rename a batch of files..Such as I want to
rename"1.avi,2.avi" to "Prison BreakS1E1.avi,Prison BreakS1E2.avi" in
the same folder..How can I do?
Assuming you are using some version of a Microsoft Windows operating
system, (based on the embedded spaces in the file names and the use of
the word "folder",) take a look here:

http://www.nonags.com/nonags/fileren.html
Pick a program from there and then use system("your program of choice
....");

R.W.
Nov 22 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Hugz | last post by:
Hello, And Thank you for taking time to help me.Myself Hugz.I am new to perl and want to write my own Scripts.But i can't understand how perl "rename" function and "file locking" works. ...
1
by: Dentharg | last post by:
Hi! How can I rename this tab in Crystal Report Viewer from "Main Report" to something else? Thanks!
4
by: Russell Warren | last post by:
I've been having a hard time tracking down a very intermittent problem where I get a "permission denied" error when trying to rename a file to something that has just been deleted (on win32). ...
5
by: Matt S. | last post by:
I have been using C# 2005 Express to make class libraries for use in another software program. Each version of this software has a different .NET API, so maintaining multiple solutions of the same...
3
by: Luciano | last post by:
I have been programing in PHP recently. For this reason, I have lots of basic doubts. Let's see one of then. I'm developing in the localhost, so safety issues are not important. I have a script...
2
by: Ole Mercano | last post by:
When I create in Designer a new button on my Form it is automatically named e.g. "button1". When I doubleclick on it I am directed to the Form.cs where the following procedure is automatcially...
92
by: ureuffyrtu955 | last post by:
Python is a good programming language, but "Python" is not a good name. First, python also means snake, Monty Python. If we search "python" in google, emule, many results are not programming...
2
by: dstork | last post by:
Anyone know how to rename the "Exit Access" command at the bottom of the Office menu. I'd like to rename it to "Exit" as I had done in previous versions of Access. I know I can disable it with ...
9
by: Kelii | last post by:
Currently I have a button that allows the user to "Close Company" - at the moment it doesn't do anything :D I would like the button to "disconnect" the back end then show my Open Company form. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.