Like Tree1Likes
  • 1 Post By Salem

Help with renaming multiple files

This is a discussion on Help with renaming multiple files within the C Programming forums, part of the General Programming Boards category; Hi, i have a problem. I want to rename multiple files, and they are named like scan1.pdf scan2.pdf, scan3.pdf and ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    1

    Help with renaming multiple files

    Hi,
    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
    Code:
    #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" );
      else
        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.
    cheers

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

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    This is really a job best done in the shell itself, or perl.
    rags_to_riches likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 06:21 PM
  2. Help renaming files!
    By guriwashere in forum C Programming
    Replies: 10
    Last Post: 06-10-2009, 10: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, 06:30 PM
  5. Replies: 1
    Last Post: 05-01-2003, 03:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21