Help on renaming files using a C program

This is a discussion on Help on renaming files using a C program within the C Programming forums, part of the General Programming Boards category; it's like this see: 1.) there exists a folder c:\folder\ which contains files with names 000file.bnf, 001file.bnf, 002file.bnf and so ...

  1. #1
    and Nothing Else Matters
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    117

    Help on renaming files using a C program

    it's like this see:

    1.) there exists a folder c:\folder\ which contains files with names 000file.bnf, 001file.bnf, 002file.bnf and so on and so forth...

    2.) I want to create a program which only uses C standard libraries in order to "switch and rename" the files such that numbers would be transferred to the end of the file name: file000.bnf, file001.bnf, file002.bnf...

    im still a rookie in C programming, currently studying in a university, we're still on Data Structures 1 (lists, stacks, queues, etc..)

    This is not a homework or bring home exam or anything.. i just want to make a program that i can use in my daily life lol (coz i have this folder filled with digicam photos but i cant arrange them in alphabetical order becoz of the numbers in front of the filename...)

    thanks guys!!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,674
    > 000file.bnf to file000.bnf
    Just use strncpy() to copy string fragments from one place to another, eg
    Code:
    char newfilename[50];
    strncpy( &newfilename[0], &oldfilename[4], 4 );  // file
    strncpy( &newfilename[4], &oldfilename[0], 3 );  // file000
    // etc
    Then use the rename() function to rename the old file to the new file.

    When that's done, we can then tackle the "all the files" problem.
    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.

  3. #3
    and Nothing Else Matters
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    117
    Quote Originally Posted by Salem
    > 000file.bnf to file000.bnf
    Just use strncpy() to copy string fragments from one place to another, eg
    Code:
    char newfilename[50];
    strncpy( &newfilename[0], &oldfilename[4], 4 );  // file
    strncpy( &newfilename[4], &oldfilename[0], 3 );  // file000
    // etc
    Then use the rename() function to rename the old file to the new file.

    When that's done, we can then tackle the "all the files" problem.
    yeah, i've thought of that but the problem is, what if there exists files which have more than 3 numbers and 4 alpha chars? i need an algorithm that would:

    1.) transfer all of the numbers before a alpha character is encountered.
    2.) check and modifies files in the entire directory, and terminates the program after the last file has been modified.

    i wuz thinkin of maype sscanf and then the macros isalpha isdigit to separate them, then use sprintf to combine again... the only problem is, i dont know how to fopen files using wildcards (*.jpg's for instance).

  4. #4
    and Nothing Else Matters
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    117

    Angry

    Quote Originally Posted by MDofRockyView
    If you know about ADT's already than this shoud be no problem. Just a bit of messing around, a hastle and inconvenience more than anything.
    i dont know ADT's yet. as ive said, we are still discussing it this semester, and we're still on singly and doubly linked lists (as implementation of LIST ADT).. and im having problems too, like say, SORTING an entire linked list, how to reverse the order of a singly linked list..

    programming is really hard man!!

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,169
    Code:
    char numstr[100];
    int i;
    
    for(i = 0;isdigit(oldfilename[i]);++i)
      numstr[i] = oldfilename[i];
    numstr[i] = '\0';
    
    *strchr(oldfilename + i, '.') = '\0';
    sprintf(newfilename, "%s%s.bnf", oldfilename + i, numstr);
    If you understand what you're doing, you're not learning anything.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Another option:
    Code:
    #include <stdio.h>
    
    int main (void)
    {
       const char *oldname[] =
       {
          "000file.bnf", "0001file.bnf", "02filename.bnf",
       };
       char newname[256];
       size_t i;
       for ( i = 0; i < sizeof oldname / sizeof *oldname; ++i )
       {
          char num[256], name[256];
          if ( sscanf(oldname[i], "%255[0123456789]%255[^.].bnf", num, name) == 2 )
          {
             sprintf(newname, "%s%s.bnf", name, num);
             printf("oldname = \"%s\", newname = \"%s\"\n", oldname[i], newname);
          }
       }
       return 0;
    }
    
    /* my output
    oldname = "000file.bnf", newname = "file000.bnf"
    oldname = "0001file.bnf", newname = "file0001.bnf"
    oldname = "02filename.bnf", newname = "filename02.bnf"
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,169
    sscanf() junkie
    If you understand what you're doing, you're not learning anything.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >sscanf() junkie
    Format string mastery is a skill cultivated by a true C aficionado.
    My best code is written with the delete key.

  9. #9
    and Nothing Else Matters
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    117
    hmmm, i think i get it...

    only 1 problem remains (and this is the hard one).. how do i rewrite the code so that it will open and rename ALL files in a specified directory (regardless of the length of the filenames). the algorithms above will work only if i know the filenames in advance. but what i want is to let the program open files by itself. i think i've read somewhere that there is a function in <io.h> called getnext() or sumthin and it opens a file with some properties... is there any function in C that can open a file without necessarily knowing the filename first???

    thanks guys!!

  10. #10
    and Nothing Else Matters
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    117
    hmmm, or how would you guys store all the filenames of an entire directory in an array of strings??

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  12. #12
    and Nothing Else Matters
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    117
    oh brother.. i was hoping a more "simpler" program.. but after reading the FAQ, i think i'll have no choice but to forget this problem, and move on to much simpler problems which i can handle...

    but thanks anyways guys!!

  13. #13
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,674
    How about invoking your program with
    myrenamer.exe 0*.bnf

    Code:
    int main ( int argv, char *argv[] ) {
      for ( i = 1 ; i < argc ; i++ ) {
        // put the code Dave posted, using argv[i] as the input string
      }
    }
    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.

  14. #14
    and Nothing Else Matters
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    117
    how come i dont see a single rename() function in the code??? or is it understood that i should be the one responsible to place it...

    anyways, if i have to manually input the filename one by one every time i run the program, i might as well manually rename it in the windows explorer.. hehehe!! i wish i could just run the program, input the path where i would like the files renamed, and then the program will do everythin for me.. now that's programming at work

  15. #15
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,674
    > how come i dont see a single rename() function in the code?
    Because we're not here to spoon feed you the complete answer.

    You're expected to take what has been given to you and adapt it to your own circumstances, and do some reading to fill in the blanks.

    > Then use the rename() function to rename the old file to the new file.
    I told you about the rename function, what more do you want?
    Basically, all you had to do is look up the function to work out whether it was
    rename("old","new") or rename("new","old").
    If that's too much, try something else in life.
    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.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Get program to copy itself into program files, then start on startup.
    By guitarist809 in forum Windows Programming
    Replies: 6
    Last Post: 03-03-2008, 08:42 AM
  2. Replies: 5
    Last Post: 02-11-2008, 12:36 AM
  3. Multiple source files for one program
    By gflores in forum C++ Programming
    Replies: 3
    Last Post: 08-15-2004, 02:32 AM
  4. my program wont read my text files in??
    By Neildadon in forum C++ Programming
    Replies: 2
    Last Post: 12-15-2002, 09:08 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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