batch file renaming - rename function

I wrote some c++ code to rename a bunch of files incorrectly named, using the "rename" function. The original names of the files are in the form of "Emma Watson Ultimate Sexy Pics [Entertainclub.blogspot.com] (5).jpg" where the number in bold varies between 5 and 54, with some numbers missing (this is not porn, they're normal pics, I'm a female Harry Potter fan; for some unknown reason, the uploader named them like that).

Upon running this program, the file names don't change and the black screen displays only values of "-1". I mention that I work in Borland C++ version 3.1 and that I've changed the directory to the folder where the files are located, so the source code file and the EXE are there. Any ideas on what's wrong with it?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<iostream.h>

void main()
{clrscr();
int l1,l2;
char oldname[300]=
"Emma Watson Ultimate Sexy Pics [Entertainclub.blogspot.com] (";
char newname[200]="Emma Watson ";
l1=strlen(oldname);
l2=strlen(newname);

for(int i=5;i<55;i++)
	{if(i<10)
		{oldname[l1]=i;
		 l1++;
		 newname[l2]=i;
		 l2++;}
	 
         else
		{oldname[l1]=i/10;
		 oldname[l1+1]=i%10;
		 l1=l1+2;
		 newname[l2]=i/10;
		 newname[l2+1]=i%10;
		 l2=l2+2;}

	 oldname[l1]=')';
	 oldname[l1+1]='.';
	 oldname[l1+2]='j';
	 oldname[l1+3]='p';
	 oldname[l1+4]='g';
	 oldname[l1+5]=0;
	 
         newname[l2]='.';
	 newname[l2+1]='j';
	 newname[l2+2]='p';
	 newname[l2+3]='g';
	 newname[l2+4]=0;

	 cout<<rename(oldname,newname)<<" ";
	 }
getch();
}



I know there are many programs out there to do this, but I wanted to do it myself (so I don't really need it, I'm just curious to know what I've done wrong).
Last edited on
It is because you are goofing up on the way you are handling the string data. You are manually converting (not successfully, in the case of the oldname) the integer to a number, and placing it at the end of the string, but then overwriting that starting at line 30.

Remember to always keep your strings having the last character as a null '\0'. (If you do, you can simply append stuff using strcat().)

However, I recommend you actually just use sprintf() to format your data. Personally, I also like my numbers to list sequentially in directories, so I'll have leading zeroes. You can take that out if you like by changing the format specifier string on line 21 to the same thing on line 20 (I underlined them for you).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <cstdio>
#include <cstdlib>
using namespace std;

bool fileexists( const char* filename )
  {
  FILE* fp = fopen( filename, "r+" );
  bool exists = (fp != NULL);
  if (exists) fclose( fp );
  return exists;
  }

int main()
  {
  char oldname[ 100 ];
  char newname[ 100 ];

  for (unsigned n = 5; n < 55; n++)
    {
    sprintf( oldname, "%s%u%s",   "Emma Watson Ultimate Sexy Pics [Entertainclub.blogspot.com] (", n, ").jpg" );
    sprintf( newname, "%s%0*u%s", "Emma Watson ",                                               2, n,  ".jpg" );
    if (fileexists( oldname ))
      {
      if (rename( oldname, newname ) == 0)
        {
        puts( newname );
        }
      else
        {
        printf( "FAILED to rename \"%s\"\n", oldname );
        }
      }
    }

  puts( "done." );
  return 0;
  }

Oh yeah, before I forget, this is basically C code, not C++. Since you are using an old compiler, you will probably have to fix those first three lines to something like:

1
2
3
#include <stdio.h>
#include <stdlib.h>

Hope this helps.
Thanks a lot for noticing the mistakes! It's been a while since I've last tried programming...
I corrected them in my program but it still displays only values of "-1" and the names don't change. I bypassed the check for the existence of the file (I'm not familiar with the necessary instructions for this), I hope it wasn't essential.
Also, thanks for the "sprintf" tip.

I have to admit I don't really understand C, but I've tried your code too with the specified modifications; line 5 gave me errors on compiling (I don't think my C++ supports "bool"), so I modified it to "int" and no more errors appeared, but it still doesn't do the trick, the file names stay the same. It doesn't display any error message (like the one you put on line 30).
I've also tried the tutorials on this site for the stuff I don't understand in your code, but I didn't really get them.

Although I changed the directory to the folder with the files (my version of C++ automatically puts the .CPP and the .EXE in the installation folder), could there still be a problem from this side? I've double-checked that the "oldname" is correct.

Here is the corrected code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#include<fstream.h>

void main()
{clrscr();
char oldname[200], newname[200];
for(int i=5;i<55;i++)
	{sprintf(oldname,"%s%u%s",
	 "Emma Watson Ultimate Sexy Pics [Entertainclub.blogspot.com] (",
	 i,").jpg");

	 sprintf(newname,"%s%u%s","Emma Watson ",i,".jpg");
	 cout<<oldname<<endl<<newname<<" ";
                   //I've also put this "cout" to make sure the names are ok this time
	 cout<<rename(oldname,newname)<<" "<<endl;
	 }
getch();
}




Also, if it's any help, here is your code which I modified:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
#include <stdlib.h>


int fileexists ( const char* filename )  //turned "bool" to "int"
  {int ok; 
  FILE* fp = fopen( filename, "r+" );
  ok = (fp != NULL);
  if (ok) fclose( fp );
  return ok;
  }

int main()
  {
  char oldname[ 100 ];
  char newname[ 100 ];

  for (unsigned n = 5; n < 55; n++)
    {
    sprintf( oldname, "%s%u%s",   "Emma Watson Ultimate Sexy Pics [Entertainclub.blogspot.com] (", n, ").jpg" );
    sprintf( newname, "%s%0*u%s", "Emma Watson ",                                               2, n,  ".jpg" );
    if (fileexists( oldname ))
      {
      if (rename( oldname, newname ) == 0)
        {
        puts( newname );
        }
      else
        {
        printf( "FAILED to rename \"%s\"\n", oldname );
        }
      }
    }

  puts( "done." );
  return 0;
  }
Last edited on
Is your exe in the same directory as the files you are renaming? Are you starting it from the command prompt? Are the files and directory writable?
Yes, no and yes. I've tried it now with the command line and it just displays the same things it does when it's running in C++ and the names still don't change.

The files and folder are just regular ones on my hard disk, without being read-only or archived or anything, I can change their names manually, so I guess this means they're writable.

I've also tried writing the entire path in the "oldname" and "newname", without better results.
Well, I guess that's that, I can't figure out anything and you've helped me enough. Thanks a lot for taking the time to help out a noob! :D
¿does fileexists return true? I think that you are not seeing the real filename.
As the only thing that you care about the file name is the number (if you do actually care), pass it as an argument to the program.
$ for K in *.jpg; do
  ./program.bin "$K" prefix_; #you need to quote the variable, because of the spaces
done

Then in your program you just need to parse argv[1] to obtain the number.

Make simulation runs or backup your data.
Spaces are evil.
Last edited on
Topic archived. No new replies allowed.