About "rename"

S

shuaishuaiyes

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"?
 
R

Richard Heathfield

(e-mail address removed) 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.
 
T

Tom St Denis

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
 
S

santosh

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;
 
S

Simon Biber

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.
 
R

Richard Bos

Tom St Denis said:
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
 
R

Roberto Waltman

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top