Advertisement
Guest User

Synchronization Script (not working)

a guest
Jul 7th, 2015
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.48 KB | None | 0 0
  1. ::@echo off
  2. color f0
  3. setlocal enabledelayedexpansion
  4. goto begin
  5.  
  6.  
  7. :stringLength ::string
  8. set tempstring=%1
  9. set /a stringLen=0
  10. call:stringLengthLoop %tempstring%
  11. goto:eof
  12.  
  13. :stringLengthLoop
  14. if defined tempstring (
  15.   set tempstring=%tempstring:~1%
  16.   set /a stringLen += 1
  17.   goto stringLengthLoop
  18. )
  19. goto:eof
  20.  
  21. :analyzeTime ::time
  22. set lastMod=%1
  23. set year=%lastMod:~0,4%
  24. set month=%lastMod:~5,2%
  25. set day=%lastMod:~8,2%
  26. set hour=%lastMod:~11,2%
  27. set minute=%lastMod:~14,2%
  28. goto:eof
  29.  
  30. :compareModifiedDate ::file1 file2
  31. echo Comparing files
  32. for %%i in (%1) do (set time1=%%~ti)
  33. for %%i in (%2) do (set time2=%%~ti)
  34. call:analyzeTime %time1%
  35. set /a year1=%year%
  36. set /a month1=%month%
  37. set /a day1=%day%
  38. set /a hour1=%hour%
  39. set /a minute1=%minute%
  40. call:analyzeTime %time2%
  41. set /a year2=%year%
  42. set /a month2=%month%
  43. set /a day2=%day%
  44. set /a hour2=%hour%
  45. set /a minute2=%minute%
  46. if %year1% EQU %year2% (
  47.   if %month1% EQU %month2% (
  48.     if %day1% EQU %day2% (
  49.       if %hour1% EQU %hour2% (
  50.         if %minute1% EQU %minute2% (
  51.           set recent=none
  52.         ) else if %minute1% GTR %minute2% (
  53.           set recent=file1
  54.         ) else (
  55.           set recent=file2
  56.         )
  57.       ) else if %hour1% GTR %hour2% (
  58.         set recent=file1
  59.       ) else (
  60.         set recent=file2
  61.       )
  62.     ) else if %day1% GTR %day2% (
  63.       set recent=file1
  64.     ) else (
  65.       set recent=file2
  66.     )
  67.   ) else if %month1% GTR %month2% (
  68.     set recent=file1
  69.   ) else (
  70.     set recent=file2
  71.   )
  72. ) else if %year1% GTR %year2% (
  73.   set recent=file1
  74. ) else (
  75.   set recent=file2
  76. )
  77. goto:eof
  78.  
  79. :begin
  80. if exist "%homedrive%%homepath%\Drive" (
  81. set drivePath=%homedrive%%homepath%\Drive
  82. goto deviceSetup) else (
  83. goto customDrivePath)
  84.  
  85. :customDrivePath
  86. set /p drivePath=Google Drive folder was not found. Specify its path:
  87. if exist %drivePath% (goto sync) else (goto customPath)
  88.  
  89. :deviceSetup
  90. set /p devicePath=Which folder path would you like to sync?
  91. if exist %devicePath% (goto syncDrivetoDevice) else (goto deviceSetup)
  92.  
  93. :syncDrivetoDevice
  94. call:stringLength %drivePath%
  95. set /a driveLength=%stringLen%
  96. call:stringLength %devicePath%
  97. set /a deviceLength=%stringLen%
  98.  
  99. for /d /r %drivePath% %%d in (*) do (
  100.   set currentDriveFolderPath=%%d
  101.   if not exist "%devicePath%!currentDriveFolderPath:~%driveLength%!" (
  102.     mkdir "%devicePath%!currentDriveFolderPath:~%driveLength%!"
  103.   )
  104.   for %%f in (*.*) do (
  105.     set currentFile=%%f
  106.     if exist "%devicePath%!currentDriveFolderPath:~%driveLength%!!currentFile!" (
  107.       set currentDriveFile="!currentDriveFolderPath!\!currentFile!"
  108.       set currentDeviceFile="%devicePath%!currentDriveFolderPath:~%driveLength%!!currentFile!"
  109.       call:compareModifiedDate !currentDriveFile! !currentDeviceFile!
  110.       if !recent! == file1 (
  111.         copy /y !currentDriveFile! !currentDeviceFile!
  112.       ) else if !recent! == file2 (
  113.         copy /y !currentDeviceFile! !currentDriveFile!
  114.       )
  115.     ) else (
  116.       set source
  117.       copy /y !currentDriveFile! !currentDeviceFile!
  118.     )
  119.   )
  120. )
  121.  
  122. :syncDevicetoDrive
  123. for /d /r %devicePath% %%d in (*) do (
  124.   set currentDeviceFolderPath=%%d
  125.   if not exist %drivePath%!currentDeviceFolderPath:~%deviceLength%! (
  126.     mkdir "%drivePath%!currentDeviceFolderPath:~%deviceLength%!"
  127.     for %%f in (*.*) do (
  128.       set currentFile=%%f
  129.       if not exist "%drivePath%!currentDeviceFolderPath:~%DeviceLength%!!currentFile!" (
  130.         copy /y !currentDriveFile! !currentDeviceFile!
  131.       )
  132.     )
  133.   )
  134. )
  135.  
  136. pause
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement