Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Diff Copy Script

A Python utility to copy only modified files from a source directory to a target directory based on modification date, with powerful filtering capabilities.

Features

  • Date-based filtering: Copy only files modified after a specific date
  • Directory structure preservation: Maintains original folder hierarchy
  • Flexible filtering:
    • Exclude specific directories (e.g., node_modules, .git)
    • Exclude file types by extension (e.g., .log, .tmp)
    • Include only specific file types
    • Exclude specific files by name
  • Automatic directory creation: Creates target directories as needed
  • Overwrite support: Replaces existing files in target
  • Progress tracking: Shows what's being copied
  • Summary report: Displays statistics after completion

Requirements

  • Python 3.6 or higher
  • No external dependencies (uses only standard library)

Installation

  1. Download or clone the script files to a directory
  2. Ensure you have Python 3 installed:
    python --version

Configuration

Edit config.json to set up your copy operation:

{
  "source_dir": "C:/path/to/your/source/folder",
  "output_dir": "C:/path/to/your/output/folder",
  "day": 1,
  "month": 1,
  "year": 2026,
  "exclude_dirs": [...],
  "exclude_extensions": [...],
  "include_extensions": [],
  "exclude_files": [...]
}

Configuration Options

Required Fields

  • source_dir: Full path to the source directory to scan
  • output_dir: Full path to the target directory where files will be copied
  • day: Day of the month (1-31)
  • month: Month of the year (1-12)
  • year: Full year (e.g., 2026)
    • Files modified after this date (year-month-day) will be copied
    • Example: "day": 15, "month": 12, "year": 2025 = files after December 15, 2025

Optional Fields

  • exclude_dirs: Array of directory names to skip

    • Example: ["node_modules", ".git", "dist", "build"]
    • Matches on directory name only, not full path
  • exclude_extensions: Array of file extensions to skip

    • Include the dot (e.g., ".log", ".tmp")
    • Case-insensitive matching
    • Example: [".log", ".tmp", ".cache", ".pyc"]
  • include_extensions: Array of file extensions to include

    • If specified, ONLY these extensions will be copied
    • Overrides exclude_extensions
    • Leave empty [] to allow all extensions (except excluded ones)
    • Example: [".py", ".js", ".ts", ".jsx"]
  • exclude_files: Array of specific filenames to skip

    • Exact filename match (case-sensitive)
    • Example: ["package-lock.json", "yarn.lock", ".env.local"]

Usage

Basic Usage

  1. Edit config.json with your settings
  2. Run the script:
    python diff_copy.py

Using a Different Config File

python diff_copy.py my-custom-config.json

Example Scenarios

Scenario 1: Copy Modified Python Files

Copy only Python files modified after January 1, 2024:

{
  "source_dir": "C:/projects/myapp",
  "output_dir": "C:/backups/myapp-changes",
  "modified_after_date": "2024-01-01",
  "exclude_dirs": ["__pycache__", ".git", "venv"],
  "exclude_extensions": [".pyc", ".pyo"],
  "include_extensions": [".py"],
  "exclude_files": []
}

Scenario 2: Copy All Modified Files Except Logs

Copy all modified files but exclude logs and temporary files:

{
  "source_dir": "C:/projects/webapp",
  "output_dir": "C:/deployment/webapp",
  "modified_after_date": "2024-12-01",
  "exclude_dirs": ["node_modules", "dist", ".git"],
  "exclude_extensions": [".log", ".tmp", ".cache"],
  "include_extensions": [],
  "exclude_files": ["package-lock.json", ".env.local"]
}

Scenario 3: Copy Recent Source Code

Copy source code files modified in the last week:

{
  "source_dir": "C:/dev/myproject",
  "output_dir": "C:/reviews/myproject-recent",
  "modified_after_date": "2024-12-25",
  "exclude_dirs": ["bin", "obj", "packages", ".vs"],
  "exclude_extensions": [".exe", ".dll", ".pdb", ".suo"],
  "include_extensions": [".cs", ".csproj", ".sln", ".config"],
  "exclude_files": []
}

Output Example

======================================================================
DIFF COPY SCRIPT
======================================================================
Source Directory: C:/projects/myapp
Output Directory: C:/backups/myapp-changes
Modified After: 2024-01-01
======================================================================

Scanning files...

✓ Copied: src/main.py
✓ Copied: src/utils/helper.py
✓ Copied: tests/test_main.py
✓ Copied: config/settings.json

======================================================================
SUMMARY
======================================================================
Files Processed: 156
Files Copied: 4
Files Skipped: 152
Errors: 0
======================================================================

Output directory: C:/backups/myapp-changes

Done!

How It Works

  1. Load Configuration: Reads and validates config.json
  2. Parse Date: Converts the date string to a datetime object
  3. Scan Directory: Walks through all files in the source directory
  4. Apply Filters: For each file:
    • Checks if in excluded directory
    • Checks modification date
    • Checks file extension filters
    • Checks specific file exclusions
  5. Copy Files: Copies matching files while preserving directory structure
  6. Report: Shows summary of operation

Troubleshooting

Error: Configuration file not found

  • Make sure config.json exists in the same directory as the script
  • Or specify the full path: python diff_copy.py C:/path/to/config.json

Error: Source directory does not exist

  • Check that the source_dir path in config.json is correct
  • Use forward slashes / or escaped backslashes \\ in paths

Error: Invalid date format

  • Use format: YYYY-MM-DD or YYYY-MM-DD HH:MM:SS
  • Example: "2024-01-01" or "2024-01-01 14:30:00"

No files copied

  • Check that files were actually modified after the specified date
  • Verify your filter settings aren't too restrictive
  • Make sure include_extensions is empty [] if you want all file types

Permission errors

  • Run the script with appropriate permissions
  • Check that you have read access to source and write access to output directory

Tips

  1. Test First: Start with a small source directory to verify your filters work correctly

  2. Include vs Exclude: If you specify include_extensions, only those file types will be copied (ignore exclude_extensions)

  3. Date Formats: The script accepts both date-only and date-time formats

  4. Path Separators: Use forward slashes / in paths for cross-platform compatibility

  5. Backup: Consider backing up your target directory before running if it contains important files

License

This script is provided as-is for free use and modification.

About

A Python utility to copy only modified files from a source directory to a target directory based on modification date, with powerful filtering capabilities.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages