Hi, I'd like to make a program which does this renaming work:
example.txt -> example(copy).txt
Also, I'd like it to support any number of '.' into the file name, like:
example.2011.txt -> example.2011(copy).txt
I've tried using string.h functions... and I didn't think it could be that hard.
I don't need help about the file managing (fopen, rename, etc). I just need help finding the correct position where to add the "(copy)".
I've been working on it for some hours... then I decided to ask here because probably the code became too complex for the simple work.
Here it is:
Code:
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
char **ppc; // to visit **argv
char *est; // where to attach strings' extension
char *result; // resultant filename
int dim; // string length
int dim2; // 2nd string length
int i; // this serves like an index
int i2; // used to find filename's first part, then to copy the extension part
int i3;
if(argc > 1)
{
ppc = argv + 1;
for(i = 1; i < argc; ++i)
{
printf("%s renamed to ", *ppc);
dim = strlen(*ppc);
est = strrchr(*ppc, '.');
i2 = est - *ppc;
printf("\ni2 = %d (number of letters before the last '.')\n", i2);
result = strncpy(result, *ppc, i2);
printf("\nresult: %s\n", result);
i3 = i2 = dim - i2;
printf("\ni2: %d\n", i2);
while(i2-- > 0)
{
printf("%c", *est++);
//est++;
//*est = *est;
//*(++est) = *(est+1);
}
//est = est - i2
*(est) = '\0';
est = est - i3;
printf("\nextension: %s\n", est);
dim2 = strlen(est);
//result = strncpy(result, *ppc, dim - dim2);
result = strcat(result, "(copy)");
result = strcat(result, est);
printf("%s\n", result);
}
}
return 0;
}
If you are on *nix, you can use the terminal and try "./binaryname example.txt" and it will work. But when I tried "./binaryname example.2011.txt" it printed some weird characterhs through the name.
Where do I make errors?
Thank you