duplicatefile.md 4.3 KB

duplicatefile.cmd

A Windows batch script for efficiently duplicating a source file to multiple destination files with various operational modes.

Overview

duplicatefile.cmd is a command-line utility that allows you to copy a single source file to multiple destination files in one operation. It supports both direct file specification and list-based operations, with an optional dry-run mode for testing.

Syntax

duplicatefile <source_file> [-dry] [-list] <out_file1> .. <out_filen>

Parameters

  • <source_file> - The file to be duplicated (required)
  • [-dry] - Optional flag for dry-run mode (shows operations without executing)
  • [-list] - Optional flag to use list file mode
  • <out_file1> .. <out_filen> - Target destination files

Usage Modes

1. Direct File Mode

Copy source file to multiple specified destinations:

duplicatefile source.txt file1.txt file2.txt file3.txt

This will copy source.txt to file1.txt, file2.txt, and file3.txt.

2. List File Mode

Use a text file containing the list of target destinations:

duplicatefile source.txt -list targets.txt

The targets.txt file should contain one destination file per line:

backup1.txt
subfolder/backup2.txt
../backup3.txt
logs/archive.txt

3. Dry Run Mode

Preview operations without actually copying files:

duplicatefile source.txt -dry file1.txt file2.txt file3.txt
duplicatefile source.txt -dry -list targets.txt

Features

Automatic Directory Creation

The script automatically creates target directories if they don't exist:

duplicatefile config.ini backup/2025/config.ini logs/config.ini

Error Handling

  • Validates source file existence
  • Checks for list file availability when using -list mode
  • Reports copy operation success/failure for each file
  • Provides clear error messages

Flexible Path Support

Supports various path formats:

  • Relative paths: backup/file.txt
  • Parent directories: ../archive/file.txt
  • Absolute paths: C:\backups\file.txt

Examples

Database Backup Scenario

duplicatefile database.db -list backup_locations.txt

With backup_locations.txt:

daily/database_2025-06-12.db
weekly/database_week24.db
monthly/database_june.db
archive/database_backup.db

Configuration File Distribution

duplicatefile app.config -dry ^
  server1/app.config ^
  server2/app.config ^
  test/app.config

Log File Archiving

duplicatefile current.log ^
  archive/2025-06-12.log ^
  backup/current_backup.log ^
  ../shared/current.log

Output Format

Normal Mode

Source file: source.txt

Target files:  file1.txt file2.txt file3.txt

Copying: "source.txt" -> "file1.txt"
  Success
Copying: "source.txt" -> "file2.txt"
  Success
Copying: "source.txt" -> "file3.txt"
  Success

Operation completed.

Dry Run Mode

[DRY RUN MODE] - No files will be copied

Source file: source.txt

Target files:  file1.txt file2.txt file3.txt

[DRY RUN] Would copy: "source.txt" -> "file1.txt"
[DRY RUN] Would copy: "source.txt" -> "file2.txt"
[DRY RUN] Would copy: "source.txt" -> "file3.txt"

Operation completed.

Error Handling

The script handles various error conditions:

  • Missing source file: Error: Source file "missing.txt" does not exist
  • No arguments: Displays usage help
  • Missing list file: Error: List file "targets.txt" does not exist
  • Copy failures: Reports individual file copy errors

Tips and Best Practices

For Database Backups

Use list files to maintain consistent backup locations:

duplicatefile production.db -list db_backup_targets.txt

For Development Workflows

Use dry-run mode to verify operations before execution:

duplicatefile config.ini -dry -list deployment_targets.txt

Path Considerations

  • Use quotes around file paths containing spaces
  • Forward slashes (/) and backslashes () both work on Windows
  • Relative paths are resolved from the current working directory

Requirements

  • Windows operating system
  • Command Prompt or PowerShell
  • Appropriate file system permissions for source and target locations

Installation

  1. Save the script as duplicatefile.cmd
  2. Place it in a directory included in your system PATH, or
  3. Run it from its directory using the full path

The script is self-contained and requires no additional dependencies.