duplicatefile.cmd 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. :: Initialize variables
  4. set "source_file="
  5. set "dry_run=false"
  6. set "use_list=false"
  7. set "list_file="
  8. set "output_files="
  9. :: Check if no arguments provided
  10. if "%~1"=="" (
  11. echo Usage: duplicatefile ^<source_file^> [-dry] [-list] ^<out_file1^> .. ^<out_filen^>
  12. echo.
  13. echo Options:
  14. echo -dry : Dry run - show what would be copied without actually copying
  15. echo -list : Use first output file as a list file containing target filenames
  16. echo.
  17. echo Examples:
  18. echo duplicatefile source.txt file1.txt file2.txt file3.txt
  19. echo duplicatefile source.txt -dry file1.txt file2.txt
  20. echo duplicatefile source.txt -list filelist.txt
  21. exit /b 1
  22. )
  23. :: Parse command line arguments
  24. set "arg_index=0"
  25. for %%a in (%*) do (
  26. set /a arg_index+=1
  27. if !arg_index! equ 1 (
  28. set "source_file=%%~a"
  29. ) else if "%%~a"=="-dry" (
  30. set "dry_run=true"
  31. ) else if "%%~a"=="-list" (
  32. set "use_list=true"
  33. ) else (
  34. if "!use_list!"=="true" and "!list_file!"=="" (
  35. set "list_file=%%~a"
  36. ) else (
  37. set "output_files=!output_files! %%~a"
  38. )
  39. )
  40. )
  41. :: Validate source file
  42. if "!source_file!"=="" (
  43. echo Error: Source file not specified
  44. exit /b 1
  45. )
  46. if not exist "!source_file!" (
  47. echo Error: Source file "!source_file!" does not exist
  48. exit /b 1
  49. )
  50. :: Display operation mode
  51. if "!dry_run!"=="true" (
  52. echo [DRY RUN MODE] - No files will be copied
  53. echo.
  54. )
  55. echo Source file: !source_file!
  56. echo.
  57. :: Handle list mode
  58. if "!use_list!"=="true" (
  59. if "!list_file!"=="" (
  60. echo Error: List file not specified when using -list option
  61. exit /b 1
  62. )
  63. if not exist "!list_file!" (
  64. echo Error: List file "!list_file!" does not exist
  65. exit /b 1
  66. )
  67. echo Reading target files from list: !list_file!
  68. echo.
  69. for /f "usebackq delims=" %%f in ("!list_file!") do (
  70. set "target_file=%%f"
  71. :: Skip empty lines
  72. if not "!target_file!"=="" (
  73. call :copy_file "!source_file!" "!target_file!" "!dry_run!"
  74. )
  75. )
  76. ) else (
  77. :: Handle direct file list mode
  78. if "!output_files!"=="" (
  79. echo Error: No output files specified
  80. exit /b 1
  81. )
  82. echo Target files: !output_files!
  83. echo.
  84. for %%f in (!output_files!) do (
  85. call :copy_file "!source_file!" "%%f" "!dry_run!"
  86. )
  87. )
  88. echo.
  89. echo Operation completed.
  90. exit /b 0
  91. :: Subroutine to copy a file
  92. :copy_file
  93. set "src=%~1"
  94. set "dst=%~2"
  95. set "is_dry=%~3"
  96. if "%is_dry%"=="true" (
  97. echo [DRY RUN] Would copy: "%src%" -^> "%dst%"
  98. ) else (
  99. echo Copying: "%src%" -^> "%dst%"
  100. :: Create directory if it doesn't exist
  101. for %%d in ("%dst%") do (
  102. if not exist "%%~dpd" (
  103. mkdir "%%~dpd" 2>nul
  104. )
  105. )
  106. :: Copy the file
  107. copy "%src%" "%dst%" >nul 2>&1
  108. if !errorlevel! equ 0 (
  109. echo Success
  110. ) else (
  111. echo Error: Failed to copy file
  112. )
  113. )
  114. goto :eof