The words Under construction in black text on a yellow background with diagonal black stipes surrounding it
I'm in the process of moving my site. It's still a work in progress. Please excuse the mess and broken links.

Keep The Creation aka Birth Timestamp When Copying A File On A Mac In Python

TODO: Pull subtitle into page object

NOTE: I'm not sure this is accurate. It looked like it when I first tested it, but I'm not sure now.

Leaving here until I can investigate further.

This is how you can make a copy of a file and maintain its "birth" time.

Note: I'm pretty sure you need XCode to get the `SetFile` command.

NOTE:

code_start_default_section code_end_default_section

TODO: Formatting

```python3 import subprocess import time

def copy_file_keeping_creation_date(source_path, dest_path): # TODO: Add error handling in all this creation_time = subprocess.run(['stat', '-f', '%B', src_path], stdout=subprocess. PIPE).stdout.decode('utf-8') date_string = time.strftime('%m/%d/%Y %H:%M:%S', time.localtime(int(creation_time))) copy2(src_path, dest_path) response = subprocess.run(['SetFile', '-d', date_string, dest_path]) print(response) # TODO: Make a better response

Notes:

- copy2 copies the most metadata of the possible python options, but still doesn't do the birth time. - `rsync -a` doesn't copy it either. - TODO: show examples from `stat` command - TODO: put in details about the four times available for files.

links:

- https://apple.stackexchange.com/a/99599/7828 - https://stackoverflow.com/a/56009590/102401

TODO: Look at adding an answer to this one:

https://stackoverflow.com/questions/56008797/how-to-change-the-creation-date-of-file-using-python-on-a-mac

TODO: Look at this answer and see if there's something in that pathlib that can be used for finding the data instead of using `stat` - https://stackoverflow.com/a/52858040/102401

TODO: Look at `cp -p` to see what it perserves. According to the comment on this answer (https://stackoverflow.com/a/17685271/102401) that's what copy2 is supposed to do. Check it with `stat` and see if it keeps the birth timestamp.

Debugging Stuff

I'm moving stuff around right now. All this below is helping me figure out where to put stuff

        -- title

Keep The Creation aka Birth Timestamp When Copying A File On A Mac In Python

-- p

NOTE: I'm not sure this is accurate. It looked like it when I first tested it, but I'm not sure now.

-- p

Leaving here until I can investigate further.

-- p

This is how you can make a copy of a file and maintain its "birth" time.

-- p

Note: I'm pretty sure you need XCode to get the `SetFile` command.

-- p

NOTE:

-- code/

# this is the command to get the birth time of the inode
stat -f "%B" file_name
# and this is for the file modified time:
stat -f "%m" file_name

-- /code

-- p

TODO: Formatting

-- p

```python3
import subprocess
import time

-- p

def copy_file_keeping_creation_date(source_path, dest_path):
    # TODO: Add error handling in all this
    creation_time = subprocess.run(['stat', '-f', '%B', src_path], stdout=subprocess.
PIPE).stdout.decode('utf-8')
    date_string = time.strftime('%m/%d/%Y %H:%M:%S', time.localtime(int(creation_time)))
    copy2(src_path, dest_path)
    response = subprocess.run(['SetFile', '-d', date_string, dest_path])
    print(response)
    # TODO: Make a better response

-- p

Notes:

-- p

- copy2 copies the most metadata of the possible python options, but still doesn't do the birth time. 
- `rsync -a` doesn't copy it either. 
- TODO: show examples from `stat` command 
- TODO: put in details about the four times available for files.

-- p

links:

-- p

- https://apple.stackexchange.com/a/99599/7828
- https://stackoverflow.com/a/56009590/102401

-- p

TODO: Look at adding an answer to this one:

-- p

https://stackoverflow.com/questions/56008797/how-to-change-the-creation-date-of-file-using-python-on-a-mac

-- p

TODO: Look at this answer and see if there's something in that pathlib that can be used for finding the data instead of using `stat` - https://stackoverflow.com/a/52858040/102401

-- p

TODO: Look at `cp -p` to see what it perserves. According to the comment on this answer (https://stackoverflow.com/a/17685271/102401) that's what copy2 is supposed to do. Check it with `stat` and see if it keeps the birth timestamp.


-- categories
-- Miscellaneous

-- metadata
-- date: 2021-11-03 22:12:19
-- id: 20eof4ea
-- status: draft
-- type: post
-- SCRUBBED_NEO: false
-- site: aws