Advertisement
mosaid

batch script to copy files to usb

Nov 19th, 2018
744
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 6.75 KB | None | 0 0
  1. @echo off
  2. %homedrive%
  3. setlocal enableextensions enabledelayedexpansion
  4. set VK="HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
  5. set "recoverDir=%~dp0recovered"
  6. set "volume=%~d0"
  7. SET "REQUIRED_SPACE=                                       1073741824"
  8. set "REQUIRED_SPACE=%REQUIRED_SPACE:~-25%"
  9. set "cmdln=%1"
  10. if "%cmdln%" EQU "" ( goto chelp )
  11. :cparse
  12. set tmp1=%cmdln%
  13. set tmp2=!cmdln:x=!
  14. ECHO.%cmdln% | findstr /C:"s" >Nul && (
  15.                                    goto cspecific
  16.                                  )
  17. ECHO.%cmdln% | findstr /C:"0" >Nul && (
  18.                                    goto cpall
  19.                                  )
  20. ECHO.%cmdln% | findstr /C:"1" >Nul && (
  21.                                    set cmdln=!cmdln:1=!
  22.                                    goto cimage
  23.                                  )
  24. ECHO.%cmdln% | findstr /C:"2" >Nul && (
  25.                                    set cmdln=!cmdln:2=!
  26.                                    goto cmusic
  27.                                  )
  28. ECHO.%cmdln% | findstr /C:"3" >Nul && (
  29.                                    set cmdln=!cmdln:3=!
  30.                                    goto cvidio
  31.                                  )
  32. ECHO.%cmdln% | findstr /C:"4" >Nul && (
  33.                                    set cmdln=!cmdln:4=!
  34.                                    goto cdocs
  35.                                  )
  36. goto cend
  37. :cimage
  38. set VNM="My Pictures"
  39. set ftypes=*.jpg,*.bmp,*.gif,*.jpeg,*.png
  40. set fldrname=image
  41. goto ccopy
  42. :cmusic
  43. set VNM="My Music"
  44. set ftypes=*.mp3,*.wma
  45. set fldrname=music
  46. goto ccopy
  47. :cvidio
  48. set VNM="My Video"
  49. set ftypes=*.flv,*.mp4,*.mkv,*.rmvb,*.avi
  50. set fldrname=video
  51. goto ccopy
  52. :cdocs
  53. set VNM="Personal"
  54. set ftypes=*.doc,*.docx,*.pdf,*.xls,*.xlsx,*.txt
  55. set fldrname=documents
  56. goto ccopy
  57. :cspecific
  58. echo.
  59. if not "%tmp1%" EQU "%tmp2%" ( Echo move specif types of files :
  60.       ) else Echo copy specif types of files :
  61. echo.
  62. echo enter one extension : *.jpg  
  63. echo.  
  64. echo enter multiple extensions : *.exe,*.txt  
  65. echo.
  66. echo.
  67. set /p ftypes=extensions :
  68. goto cpcpesific
  69. :ccopy
  70. FOR /F "tokens=3*" %%a IN ( 'REG QUERY %VK% /v %VNM%') DO (
  71.  if not "%%a" EQU "REG_SZ" (
  72.     set docsdir=%%a %%b
  73.  ) else  set docsdir=%%b
  74. )
  75. pushd "%docsdir%"
  76. mkdir %recoverDir%\%fldrname% >nul 2>nul
  77. attrib /d /s +s +h %recoverDir% >nul 2>nul
  78. for /r %%# in (%ftypes%) do (
  79.        FOR /f "tokens=1* delims=:" %%i IN ('fsutil volume diskfree %volume%') DO (
  80.               SET "FREE_SPACE=%%j"
  81.        )
  82.        SET "FREE_SPACE=                                   !FREE_SPACE!"
  83.        SET "FREE_SPACE=!FREE_SPACE:~-25!"
  84.        FOR %%M IN (%%#) DO SET "FILE_SIZE=%%~zM"
  85.        SET "FILE_SIZE=                                   !FILE_SIZE!"
  86.        SET "FILE_SIZE=!FILE_SIZE:~-25!"
  87.        IF !REQUIRED_SPACE! LSS !FILE_SIZE! ( SET "REQUIRED_SPACE=!FILE_SIZE!" )
  88.        ECHO FILE SIZE  :!FILE_SIZE!
  89.        ::echo REQUIRED   :!REQUIRED_SPACE!
  90.        ::echo free space :!FREE_SPACE!
  91.        IF NOT !FREE_SPACE! LSS !REQUIRED_SPACE! (      
  92.          if not "%tmp1%" EQU "%tmp2%" (  
  93.            xcopy "%%#" "%recoverDir%\%fldrname%\%%~nx#" >nul 2>nul
  94.          ) else echo copy "%%#" "%recoverDir%\%fldrname%\%%~nx#" >nul 2>nul
  95.        ) ELSE (
  96.                 ECHO NOT ENOUGH FREE SPACE
  97.                 GOTO cend
  98.                )
  99. )
  100. goto cparse
  101. :chelp
  102. cls
  103. echo.
  104. echo          =====================================================
  105. echo            WARRNING : BE CAREFUL WHILE MOVING SYSTEM FILES
  106. echo          =====================================================
  107. echo.
  108. echo /1: image files     /2: audio files
  109. echo /3: video files     /4: text documents
  110. echo /0: copy all types of files
  111. echo /x: move Files not copy Files (to sort files in folders )
  112. echo /s: copy specific file types  
  113. echo.
  114. echo Files are moved/copied to current directory of  %~nx0
  115. echo.
  116. echo examples
  117. echo.
  118. echo  /1      : copy image files
  119. echo  /12     : copy image and audio files
  120. echo  /x12    : move image and audio files  
  121. echo  /1234   : copy image,audio,video and text files
  122. echo  /xs     : move specific files
  123. echo.
  124. echo  %~nx0 /12 is the syntax to use in the command prompt (cmd)    
  125. echo.
  126. echo.
  127. set /p cmdln=Enter command :
  128. goto cparse
  129. :cpall
  130. mkdir "%recoverDir%\image" >nul 2>nul
  131. mkdir "%recoverDir%\music" >nul 2>nul
  132. mkdir "%recoverDir%\video" >nul 2>nul
  133. mkdir "%recoverDir%\document" >nul 2>nul
  134. mkdir "%recoverDir%\applications" >nul 2>nul
  135. mkdir "%recoverDir%\icons" >nul 2>nul
  136. mkdir "%recoverDir%\other" >nul 2>nul
  137. attrib /d /s +s +h %recoverDir% >nul 2>nul
  138. pushd %homepath%
  139. for /f "usebackq delims=" %%# in (`dir /b /ad`) do (
  140.       if not "%%#" EQU "AppData" (
  141.       if not "%%#" EQU "Application Data" (
  142.       if not "%%#" EQU "Local Settings" (  
  143.       pushd "%homepath%\%%#"
  144.       for /r %%a in (*.*) do (
  145.          set b=*.jpg,*.bmp,*.gif,*.jpeg
  146.          set b2=!b:%%~xa=!
  147.          set b3=image
  148.          if "!b!" EQU "!b2!" (
  149.          set b=*.mp3,*.wma
  150.          set b2=!b:%%~xa=!
  151.          set b3=music
  152.          )
  153.          if "!b!" EQU "!b2!" (
  154.          set b=*.flv,*.mp4,*.mkv,*.rmvb,*.avi
  155.          set b2=!b:%%~xa=!
  156.          set b3=video
  157.          )
  158.          if "!b!" EQU "!b2!" (
  159.          set b=*.doc,*.docx,*.pdf,*.xls,*.xlsx,*.txt
  160.          set b2=!b:%%~xa=!
  161.          set b3=document
  162.          )
  163.          if "!b!" EQU "!b2!" (
  164.          set b=*.exe,*.msi,*.inf,*.ini,*.lnk,*.bat,*.cmd
  165.          set b2=!b:%%~xa=!
  166.          set b3=applications
  167.          )
  168.          if "!b!" EQU "!b2!" (
  169.          set b=*.png,*.ico
  170.          set b2=!b:%%~xa=!
  171.          set b3=icons
  172.          )
  173.          if "!b!" EQU "!b2!" (
  174.          set b3=other
  175.          )
  176.          FOR /f "tokens=1* delims=:" %%i IN ('fsutil volume diskfree %volume%') DO (
  177.               SET "FREE_SPACE=%%j"
  178.          )
  179.          SET "FREE_SPACE=                                     %FREE_SPACE%"
  180.          set "FREE_SPACE=%FREE_SPACE:~-25%"          
  181.          IF NOT !FREE_SPACE! LSS !REQUIRED_SPACE! (
  182.              if not "!tmp1!" EQU "!tmp2!" (
  183.                  xcopy "%%a" "%recoverDir%\!b3!\%%~nxa" >nul 2>nul
  184.              ) else copy "%%a" "%recoverDir%\!b3!\%%~nxa" >nul 2>nul
  185.          ) ELSE (
  186.                   ECHO NOT ENOUGH FREE SPACE
  187.                   GOTO cend
  188.                 )
  189. )))))
  190. goto cend
  191. :cpcpesific
  192. mkdir "%recoverDir%\specific" >nul 2>nul
  193. attrib /d /s +s +h %recoverDir% >nul 2>nul
  194. pushd %homepath%
  195. for /f "usebackq delims=" %%# in (`dir /b /ad`) do (
  196.       if not "%%#" EQU "AppData" (
  197.       if not "%%#" EQU "Application Data" (
  198.       if not "%%#" EQU "Local Settings" (  
  199.       pushd "%homepath%\%%#"
  200.       for /r %%a in (%ftypes%) do (
  201.           FOR /f "tokens=1* delims=:" %%i IN ('fsutil volume diskfree %volume%') DO (
  202.               SET "FREE_SPACE=%%j"
  203.           )
  204.           SET "FREE_SPACE=                                     %FREE_SPACE%"
  205.           set "FREE_SPACE=%FREE_SPACE:~-25%"
  206.           IF NOT !FREE_SPACE! LSS !REQUIRED_SPACE! (
  207.              if not "!tmp1!" EQU "!tmp2!" (
  208.                   xcopy "%%a" "%recoverDir%\specific\%%~nxa" >nul 2>nul
  209.              ) else copy "%%a" "%recoverDir%\specific\%%~nxa"  >nul 2>nul
  210.           ) ELSE (
  211.                     ECHO NOT ENOUGH FREE SPACE
  212.                     GOTO cend
  213.                  )
  214. )))))
  215. goto cend
  216. :cend
  217. attrib /d /s +s +h %recoverDir% >nul 2>nul
  218. exit /b
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement