| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- @echo off
- setlocal enabledelayedexpansion
- :: Initialize variables
- set "source_file="
- set "dry_run=false"
- set "use_list=false"
- set "list_file="
- set "output_files="
- :: Check if no arguments provided
- if "%~1"=="" (
- echo Usage: duplicatefile ^<source_file^> [-dry] [-list] ^<out_file1^> .. ^<out_filen^>
- echo.
- echo Options:
- echo -dry : Dry run - show what would be copied without actually copying
- echo -list : Use first output file as a list file containing target filenames
- echo.
- echo Examples:
- echo duplicatefile source.txt file1.txt file2.txt file3.txt
- echo duplicatefile source.txt -dry file1.txt file2.txt
- echo duplicatefile source.txt -list filelist.txt
- exit /b 1
- )
- :: Parse command line arguments
- set "arg_index=0"
- for %%a in (%*) do (
- set /a arg_index+=1
-
- if !arg_index! equ 1 (
- set "source_file=%%~a"
- ) else if "%%~a"=="-dry" (
- set "dry_run=true"
- ) else if "%%~a"=="-list" (
- set "use_list=true"
- ) else (
- if "!use_list!"=="true" and "!list_file!"=="" (
- set "list_file=%%~a"
- ) else (
- set "output_files=!output_files! %%~a"
- )
- )
- )
- :: Validate source file
- if "!source_file!"=="" (
- echo Error: Source file not specified
- exit /b 1
- )
- if not exist "!source_file!" (
- echo Error: Source file "!source_file!" does not exist
- exit /b 1
- )
- :: Display operation mode
- if "!dry_run!"=="true" (
- echo [DRY RUN MODE] - No files will be copied
- echo.
- )
- echo Source file: !source_file!
- echo.
- :: Handle list mode
- if "!use_list!"=="true" (
- if "!list_file!"=="" (
- echo Error: List file not specified when using -list option
- exit /b 1
- )
-
- if not exist "!list_file!" (
- echo Error: List file "!list_file!" does not exist
- exit /b 1
- )
-
- echo Reading target files from list: !list_file!
- echo.
-
- for /f "usebackq delims=" %%f in ("!list_file!") do (
- set "target_file=%%f"
-
- :: Skip empty lines
- if not "!target_file!"=="" (
- call :copy_file "!source_file!" "!target_file!" "!dry_run!"
- )
- )
- ) else (
- :: Handle direct file list mode
- if "!output_files!"=="" (
- echo Error: No output files specified
- exit /b 1
- )
-
- echo Target files: !output_files!
- echo.
-
- for %%f in (!output_files!) do (
- call :copy_file "!source_file!" "%%f" "!dry_run!"
- )
- )
- echo.
- echo Operation completed.
- exit /b 0
- :: Subroutine to copy a file
- :copy_file
- set "src=%~1"
- set "dst=%~2"
- set "is_dry=%~3"
- if "%is_dry%"=="true" (
- echo [DRY RUN] Would copy: "%src%" -^> "%dst%"
- ) else (
- echo Copying: "%src%" -^> "%dst%"
-
- :: Create directory if it doesn't exist
- for %%d in ("%dst%") do (
- if not exist "%%~dpd" (
- mkdir "%%~dpd" 2>nul
- )
- )
-
- :: Copy the file
- copy "%src%" "%dst%" >nul 2>&1
- if !errorlevel! equ 0 (
- echo Success
- ) else (
- echo Error: Failed to copy file
- )
- )
- goto :eof
|