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.