---
title: "How do you copy file metadata (creation date, attributes) along with the file?"  
description: "How do you copy file metadata (creation date, attributes) along with the file?"  
author: "ICSM Computer"  
published: 2025-05-14  
updated: 2025-05-25  
canonical: https://www.mindstick.com/forum/161620/how-do-you-copy-file-metadata-creation-date-attributes-along-with-the-file  
category: "c#"  
tags: ["file handling"]  
reading_time: 2 minutes  

---

# How do you copy file metadata (creation date, attributes) along with the file?

How do you [copy](https://www.mindstick.com/forum/161993/explain-the-numpy-array-copy-vs-view-with-example) [file](https://www.mindstick.com/articles/59/encrypting-and-decrypting-files-using-c-sharp) [metadata](https://www.mindstick.com/articles/334241/the-power-of-metadata-how-it-shapes-google-search-results) ([creation](https://yourviews.mindstick.com/view/85506/what-was-the-causes-of-creation-of-pakistan) [date](https://yourviews.mindstick.com/story/3869/propose-day-2024-exciting-date-ideas-for-you-and-your-partner), [attributes](https://www.mindstick.com/articles/13105/2-attributes-that-make-your-odoo-ecommerce-theme-productive-and-engaging)) along with the file?

## Replies

### Reply by Anubhav Sharma

To **copy a file along with its metadata** such as creation date, modification date, and file attributes, you need more than just a standard file copy. Most basic `File.Copy` or `shutil.copy` calls do **not** preserve everything by default.

Below are solutions for copying **file content + metadata** across different platforms.

## C# (.NET)

### Full Copy (including timestamps and attributes)

```cs
using System;
using System.IO;

public static void CopyFileWithMetadata(string sourcePath, string destinationPath)
{
    // Copy the file content and metadata
    File.Copy(sourcePath, destinationPath, overwrite: true);

    // Copy timestamps
    File.SetCreationTime(destinationPath, File.GetCreationTime(sourcePath));
    File.SetLastWriteTime(destinationPath, File.GetLastWriteTime(sourcePath));
    File.SetLastAccessTime(destinationPath, File.GetLastAccessTime(sourcePath));

    // Copy file attributes (e.g., ReadOnly, Hidden)
    File.SetAttributes(destinationPath, File.GetAttributes(sourcePath));
}
```

- `File.Copy()` copies the file content.
- `File.Set*Time()` replicates creation and modification timestamps.
- `File.SetAttributes()` copies attributes like `ReadOnly`, `Hidden`, etc.

## Python

### Use `shutil.copy2()` to copy file + metadata

```python
import shutil

def copy_file_with_metadata(src, dst):
    shutil.copy2(src, dst)
```

1. `copy2()` copies the content and metadata (timestamps, mode).
2. On most platforms, `copy2` will preserve modification and access times. Creation time is preserved on Windows.
3. Note: On **Unix**, creation time (`ctime`) often cannot be set or preserved directly, as it's managed by the file system.

## Linux/macOS (Command Line)

### Use `cp -p` or `rsync`

```plaintext
cp -p source.txt destination.txt
```

`-p` preserves mode, ownership, and timestamps.

Or use `rsync`:

```plaintext
rsync -a source.txt destination.txt
```

`-a` (archive mode) copies content with all metadata preserved.

## Windows (Command Line)

### Use `robocopy` to preserve metadata

```plaintext
robocopy sourceDir targetDir filename.txt /COPYALL
```

`/COPYALL` includes timestamps, ACLs, owner info, attributes.

## Summary

| Language | Tool | Metadata Preserved |
| --- | --- | --- |
| C# | Manual Set*Time + SetAttributes | Creation, last modified, attributes |
| Python | `shutil.copy2` | Most metadata (not all on Unix) |
| Bash | `cp -p`, `rsync -a` | Timestamps, mode |
| Windows | `robocopy /COPYALL` | All metadata (NTFS only) |


---

Original Source: https://www.mindstick.com/forum/161620/how-do-you-copy-file-metadata-creation-date-attributes-along-with-the-file

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
