Advertisement
DePhoegon

Copying Drive/Directory w/ minimal overhead

Aug 26th, 2019
436
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 2.59 KB | None | 0 0
  1. @echo off
  2. rem Main use to move entire directory/drive without looking at the data before hand or otherwise reading the data more then needed.
  3. rem useful for larger drives/folder sizes with many files.
  4. rem respects access rights (if you can not get there in command-line, or access/read they file it will skip it)
  5. rem meant to be as light as possible for drives that may not tolerate a lot of data manipulation.
  6. rem Powershell version in the works, for network access.
  7. rem must be local drive w/ letter or mapped network drive that is accessible via mapped drive letter.
  8. rem use of the variable adjustment to avoid multiple inputs that can be referred from a path&Drive letter.
  9.  
  10. rem setting variables, and tests for arguments being passed in or not, & checks \? for help  
  11. set FILENAME=DC.bat
  12. if %1 == \? GOTO HELP
  13. set SRC=.
  14. set DEST=.
  15. set SRC=%1
  16. set DEST=%2
  17. set INP=.
  18. set IFL=.
  19. set IFLP=.
  20. if %SRC% == . GOTO NSRC
  21. if %DEST% == . GOTO NDEST
  22. GOTO FORM
  23.  
  24. rem Starting loop, moving to the correct drive, then moves to the right folder (with assuming it may not always be the root of the drive.
  25. :START
  26. %SRC:~0,2%
  27. cd %SRC%
  28. for /r %%a in (*.*) do (
  29. set INP=%DEST%%%~pa
  30. set IFL=%%a
  31. set IFLP=%%~nxa
  32. CALL :ACT
  33. )
  34. GOTO FIN
  35.  
  36. rem Checks for the matching folder structure, and does it one level at a time, due to the nature of the recursive For loop that calls [ACT], then creates the folder if needed which is followed by a copy.
  37. :ACT
  38. IF NOT EXIST "%INP%" MD "%INP%"
  39. copy /v /y "%IFL%" "%INP%"
  40. IF EXIST "%INP%%IFLP%" echo "%INP%%IFLP%"
  41. GOTO FIN
  42.  
  43. rem some input cleaning and ensuring that the variables can mesh the paths as needed without error.
  44. :FORM
  45. IF %SRC:~-1% == / set SRC=%SRC:~0,-1%\
  46. IF NOT %SRC:~-1% == \ set SRC=%SRC%\
  47. IF %DEST:~-1% == / set DEST=%DEST:~0,-1%
  48. IF %DEST:~-1% == \ set DEST=%DEST:~0,-1%
  49. GOTO PRIME
  50.  
  51. rem Argument checks
  52. :PRIME
  53. IF NOT EXIST "%SRC%" GOTO NOSRC
  54. IF NOT EXIST "%DEST%" GOTO NODEST
  55. GOTO START
  56.  
  57. rem error outputs, and pauses in the following sections, and the help section bellow.
  58. :NSRC
  59. echo Source Input required at end.
  60. pause
  61. GOTO FIN
  62. :NDEST
  63. echo Destination Input Required.
  64. pause
  65. GOTO FIN
  66. :NOSRC
  67. echo Source Directory does not exist, or incorectly entered
  68. pause
  69. GOTO FIN
  70. :NODEST
  71. echo Destination Directory does not Exist, or incorrectly entered
  72. pause
  73. GOTO FIN
  74. :HELP
  75. echo For copying entire Directories with folder structure intact, ignoring 'empty folders'
  76. echo %FILENAME% [Source] [Destination]
  77. echo Example %FILENAME% X:\ "F:\Named Folder"
  78. echo If folders have spaces in the name. use Double Quotes around the whole argument Example "D:\Named Folder"
  79. :FIN
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement