Thread: Rename file to creation time

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    3

    Rename file to creation time

    I have a problem. my program works but i can't rename mi files to creation time
    program must rename files in a folder.
    Example:

    old file names


    bla1.jpg ->2007(year)1010(Date)1100(time)_bla1.jpg
    bla2.jpg ->200710101100_bla2.jpg


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <dirent.h>
    #include <string.h>
    #include <windows.h> 
    #include <time.h> 
    #include <iostream.h> 
    #define MAX 20
    
    
    char *my_itoa(int wert, int laenge) {
       char *ret =(char *) malloc(laenge+1 * sizeof(char));
       int i;
    
       for(i  =0; i < laenge; i++) {
          ret[laenge-i-1] = (wert % 10) + 48;
          wert = wert / 10;
       }
       ret[laenge]='\0';
       return ret;
    }
    
    int main(int argc, char *argv[])
    {
        DIR         *dip;
        struct dirent  *dit;
    
        int i = 0, pos = 0, len,x=1;
        char path[MAX]/* = "/alanya"*/, c, prefix[MAX], end[MAX], neu[MAX];
        printf("\n      ----- rename 0.2 -----");
        printf("\n\n / -> root der aktuellen Partition\nauf der die Datei ausgefuehrt wird.\n\n");
        printf("Verzeichnis:\n> ");
        fflush(stdin);
        gets(path);
        
        // Bösse 
        // SYSTEMTIME systime;
        // FILETIME   ftime; 
        
        // GetSystemTime(&systime);
        // SystemTimeToFileTime(&systime,&ftime);
    
      //  *(ULONGLONG*)&ftime+=24*60*40*1000000*10;
    
      //  FileTimeToSystemTime(&ftime,&systime);
       // SetSystemTime(&systime);
        
        printf("Prefix:\n> ");
        fflush(stdin);
        gets(prefix);
        printf("Dateiendung:\n> ");
        fflush(stdin);
        gets(end);
     
       
        if((dip = opendir(path)) == NULL)
        {
            printf("fehler beim oeffnen des verzeichnisses\n\n");
            return 0;
        }
       
        else
        {
            while((dit = readdir(dip)) != NULL)
            {
                if(strcmp(dit->d_name, ".") != 0 && strcmp(dit->d_name, "..") != 0)
                {
                                       
                         strcpy(neu,prefix);
                         strcat(neu, my_itoa(x,5));
                         strcat(neu, end);
                         printf("%s\n",neu);
                         x++;
                                     
                    printf("%s\n", dit->d_name);
                    rename(dit->d_name,neu);
                }
            }
        }
       
       
        if(closedir(dip) == -1)
        {
            printf("fehler beim schliessen des verzeichnisses\n\n");
            return 0;
        }
       
        printf("\n\n");
        system("PAUSE");
        return 0;
    }

    thx for help!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
        fflush(stdin);
        gets(path);
    Both of these are BAD! The first because it:
    1. Doesn't do what you think it does.
    2. Is undefined - so it may potentially cause problems like crashing on some systems.

    The second line is bad because it's one of the most famous ways to cause a "buffer overflow", which is a common way to crash and create security holes in machines.

    As to your rename - if your current directory isn't the same as where the file is, then you may will not be able to rename the file without first prepending the path.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    3
    thx for post.

    the file is in same directory as the prgramm.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > #include <stdlib.h>
    A standard C header

    > #include <dirent.h>
    A POSIX header

    > #include <windows.h>
    A Win32 header

    > #include <iostream.h>
    A C++ header

    Perhaps begin with some focus on which APIs and languages you're intending to use, rather than just beginning with
    #include <kitchen_sink.h>

    > char *ret =(char *) malloc(laenge+1 * sizeof(char));
    See the FAQ on casting malloc in C, though you might want to stop using C++ to compile this code beforehand.
    On another note, the caller doesn't free this memory, so expect leakage and ballooning resource usage.

    I haven't checked (hint: you do it), but are you sure it's safe to rename a file in the same directory that you're processing with readdir()?

    > 200710101100_bla2.jpg
    This is already longer than the MAX you've allowed for.

    The stat() function (which I'm guessing you may have since you have readdir) will return the creation time as a time_t, which you can then format using strftime()
    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.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    3
    thx.

    can you give a expample to this?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Example of what?
    There's directory traversal stuff in the FAQ for a variety of systems.
    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. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. Simple File Creation Algorithm
    By muffin in forum C Programming
    Replies: 13
    Last Post: 08-24-2001, 03:28 PM