Thread: Help with renaming multiple files

  1. #1
    Registered User
    Join Date
    Jun 2011

    Help with renaming multiple files

    i have a problem. I want to rename multiple files, and they are named like scan1.pdf scan2.pdf, scan3.pdf and so on. I want to change only the numbers at the end of the file.
    I used this code for renaming one file
    #include <stdio.h>
    int main ()
      int result;
      char oldname[] ="oldname.txt";
      char newname[] ="newname.txt";
      result= rename( oldname , newname );
      if ( result == 0 )
        puts ( "File successfully renamed" );
        perror( "Error renaming file" );
      return 0;
    i would like to if there is any way, that i could use
    for (i=1,i<=n,i++) and put the parameter i in the oldname.txt?
    I hope you understood.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Look into what sprintf can do for you.

  3. #3
    Join Date
    Aug 2010
    Ontario Canada
    Ok... Unless you already have a list of the files to be renamed, you need to read the directory, get a file name, remove the extension, remove the number, add your own number, put the extension back, then rename the file, in a loop until all your files are done.

    If you have a list like ...oldname newname... it's much easier, just use the rename command in a loop until your list is exhausted.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    This is really a job best done in the shell itself, or perl.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. XP/Vista - renaming, moving & deleting files
    By Tigers! in forum Windows Programming
    Replies: 4
    Last Post: 06-28-2009, 05:21 PM
  2. Help renaming files!
    By guriwashere in forum C Programming
    Replies: 10
    Last Post: 06-10-2009, 09:37 AM
  3. Multiple Source Files, make files, scope, include
    By thetinman in forum C++ Programming
    Replies: 13
    Last Post: 11-05-2008, 11:37 PM
  4. Help on renaming files using a C program
    By sangken in forum C Programming
    Replies: 21
    Last Post: 08-03-2006, 05:30 PM
  5. Replies: 1
    Last Post: 05-01-2003, 02:52 PM