Changing file CREATE time on Win10

Discussion in 'Programming & Software Development' started by spotted, Jan 3, 2021.

  1. spotted

    spotted Member

    Joined:
    Nov 10, 2010
    Messages:
    420
    Location:
    Sydney
    Using linux subsystem on Win10. I have ubuntu.
    Touch doesn't change CREATE time, only access/modified.
    The only way to change CREATE time to current time is to use copy.

    In theory I can do
    `
    mv "${FILENAME}" tmp$$
    cp --no-preserve=all tmp$$ "${FILENAME}"
    `

    If you run these commands and pause and refresh explorer between them, it would work.
    But there's some memory in effect. If you run the commands 1 after the next, the new copy would actually keep the create time of the old file.

    I'm not sure what's going on here. Even if I delete "${FILENAME}" and do cp again, it still has the old create time.

    Here's the killer.
    I copy tmp$$ to x. X has today's create as it never existed.
    I copy x to "${FILENAME}" , it still has the old create time!

    How can I do this?

    I have a stupid program that uses create time to sort files.
     
  2. grs1961

    grs1961 Member

    Joined:
    Jan 21, 2005
    Messages:
    528
    Location:
    Melbourne
    The various utilities you are using all maintain and propagate the creation time of the file, for various sensible and performance related reasons.

    If you want to create a "really" new copy, you'll have to do it the old fashioned-way, write (or find) a program that deletes the target file, re-creates the target file, then opens the source file, reads that and writes it out.

    A bit like this (note written for UNIX without enough error checking):

    Code:
    #include <errno.h>
    #include <string.h>
    #include <unistd.h>
    
    int
    main(int argc, char *argv[])
    {
       int c;
       FILE *ifp, *ofp;
    
       if (argc != 2)
       {  
          fprintf(stderr, "usage: %s infile outfile\nNOTE: outfile is unlink()ed before infile is copied to it.\n",
                argv[0]);
          return 1;
       }
       if ((ifp = fopen(argv[1], "rb")) == NULL)
       {  
          fprintf(stderr, "%s: failed to open \"%s\" for input, errno: %d, %s\n",
                argv[1], errno, strerror(errno));
          return 2;
       }
       if (unlink(argv[2]) != 0)
       {  
          if (errno != ENOENT)
          {  
             fprintf(stderr, "%s: failed to unlink \"%s\", errno: %d, %s\n",
                   argv[2], errno, strerror(errno));
             return 3;
          }
       }
       if ((ofp = fopen(argv[2], "wb")) == NULL)
       {  
          fprintf(stderr, "%s: failed to open \"%s\" for output, errno: %d, %s\n",
                argv[2], errno, strerror(errno));
          return 4;
       }
       errno = 0;
       while ((c = getc(ifp)) != EOF)
       {  
          putc(c, ofp);
       }
       fclose(ifp);
       fclose(ofp);
       return 0;
    }
    
     
    Last edited: Jan 3, 2021

Share This Page

Advertisement: