Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.22 KB | None | 0 0
  1. Option Explicit
  2.  
  3. '
  4. ' Syncronize folders, in both ways, recursivelly
  5. '
  6. ' Usage: cscript SyncFolders.vbs {source} {dest} [{logfile}] [/nolog] [/noecho] [/oneway] [/backup]
  7. '
  8. ' {source} - Source path
  9. ' {dest} - Target path
  10. ' {logfile} - Log file. If not passed, "SyncFolders_YYYYMMDDHHMMSS.log" will be created.
  11. ' /nolog - Do not create log file
  12. ' /noecho - Do not echo
  13. ' /oneway - Do not check the reverse way
  14. ' /backup - Update files on target path as they are in source path.
  15. ' CAUTION: THIS OPTION WILL DELETE FILES ON DEST IF IT WAS REMOVED FROM SOURCE!!!
  16. '
  17. Dim objFSO
  18. Set objFSO = CreateObject("Scripting.FileSystemObject")
  19.  
  20. Dim bHasLogFile
  21. Dim objLogFile
  22. bHasLogFile = False
  23.  
  24. Dim dtTimestamp
  25. Dim strTimestamp
  26.  
  27. Dim arrValidOptions
  28. arrValidOptions = Array("/nolog", "/noecho", "/oneway", "/backup")
  29.  
  30. If (WScript.Arguments.Count < 2) Then
  31. WScript.Echo("Usage: cscript SyncFolders.vbs {source} {dest} [/options] [{log-file}]")
  32. WScript.Quit(1)
  33. End If
  34.  
  35. Dim strArg
  36. Dim strSrcFolder: strSrcFolder = ""
  37. Dim strDstFolder: strDstFolder = ""
  38. Dim strLogFile: strLogFile = ""
  39. Dim arrOptions()
  40. Dim intOptions
  41. intOptions = 0
  42.  
  43. For Each strArg in WScript.Arguments
  44. If (Left(strArg, 1) = "/") Then
  45. If UBound(Filter(arrValidOptions, strArg)) = -1 Then
  46. WScript.Echo("Parameter not expected: " & strArg)
  47. WScript.Quit(1)
  48. End If
  49.  
  50. intOptions = intOptions + 1
  51. ReDim Preserve arrOptions(intOptions)
  52. arrOptions(intOptions - 1) = strArg
  53. Else
  54. If strSrcFolder = "" Then
  55. strSrcFolder = strArg
  56. Else
  57. If strDstFolder = "" Then
  58. strDstFolder = strArg
  59. Else
  60. If strLogFile = "" Then
  61. strLogFile = strArg
  62. Else
  63. WScript.Echo("Unexpected parameter found: " & strArg)
  64. WScript.Quit(1)
  65. End If
  66. End If
  67. End If
  68. End If
  69. Next
  70. If strSrcFolder = "" Or strDstFolder = "" Then
  71. WScript.Echo("Usage: cscript SyncFolders.vbs {source} {dest} [/options] [{log-file}]")
  72. WScript.Quit(1)
  73. End If
  74.  
  75. If strLogFile = "" Then
  76. strLogFile = "SyncFolders_" & GetTime() & ".log"
  77. Else
  78. If Mid(strLogFile, Len(strLogFile) - 3, 1) = "." Then
  79. strLogFile = Left(strLogFile, Len(strLogFile) - 4) & "_" & GetTime() & Right(strLogFile, 4)
  80. Else
  81. strLogFile = strLogFile & "_" & GetTime() & ".log"
  82. End If
  83. End If
  84.  
  85. If UBound(Filter(arrOptions, "/noecho")) = -1 Then
  86. WScript.Echo("Start of Script")
  87. End If
  88.  
  89. SyncFolders strSrcFolder, strDstFolder
  90.  
  91. If UBound(Filter(arrOptions, "/noecho")) = -1 Then
  92. WScript.Echo("End of Script")
  93. End If
  94.  
  95. ' Close Log file, if is opened
  96. If bHasLogFile Then
  97. objLogFile.Close
  98. End If
  99.  
  100. Set objFSO = Nothing
  101.  
  102. '
  103. ' Pad characters of strSource on the left with strPad until intSize
  104. '
  105. Function LPad(strAlpha, strPad, intSize)
  106. Dim strPadded
  107.  
  108. If Len(strAlpha) < intSize Then
  109. strPadded = String(intSize - Len(strAlpha), strPad) & strAlpha
  110. Else
  111. strPadded = strAlpha
  112. End If
  113.  
  114. LPad = strPadded
  115. End Function
  116.  
  117. '
  118. ' Get Timestamp on format YYYYMMDDHHmmss
  119. '
  120. Function GetTime()
  121. dtTimestamp = now
  122. strTimestamp = LPad(Year(dtTimestamp), "0", 4) & LPad(Month(dtTimestamp), "0", 2) & LPad(Day(dtTimestamp), "0", 2) & LPad(Hour(dtTimestamp), "0", 2) & LPad(Minute(dtTimestamp), "0", 2) & LPad(Second(dtTimestamp), "0", 2)
  123. GetTime = strTimestamp
  124. End Function
  125.  
  126. '
  127. ' WriteLog
  128. '
  129. Sub WriteLog(strProcedure, arrParams)
  130. ' Echo output
  131. If UBound(Filter(arrOptions, "/noecho")) = -1 Or arrParams(0) = "Error" Then
  132. WScript.Echo(strProcedure & ", " & Join(arrParams, ", "))
  133. End If
  134.  
  135. If UBound(Filter(arrOptions, "/nolog")) = -1 Then
  136. ' Check to open Log File
  137. If Not bHasLogFile Then
  138. Set objLogFile = objFSO.CreateTextFile(strLogFile, True)
  139. bHasLogFile = True
  140. End If
  141.  
  142. ' Write Log file
  143. objLogFile.Write strProcedure & ";" & Join(arrParams, ";") & vbCrLf
  144. End If
  145. End Sub
  146.  
  147. '
  148. ' Syncronize Both ways
  149. '
  150. Sub SyncFolders(strFolderSync1, strFolderSync2)
  151. WriteLog "SyncFolders", Array("Call", strFolderSync1, strFolderSync2)
  152.  
  153. Dim i
  154. For i = 0 To 1
  155. If i = 0 Then
  156. SyncToFolder strFolderSync1, strFolderSync2, i
  157. Else
  158. If UBound(Filter(arrOptions, "/oneway")) = -1 Then
  159. If UBound(Filter(arrOptions, "/backup")) = -1 Then
  160. SyncToFolder strFolderSync2, strFolderSync1, i
  161. Else
  162. UpdateBackupFolder strFolderSync1, strFolderSync2
  163. End If
  164. End If
  165. End If
  166. Next
  167. End Sub
  168.  
  169. '
  170. ' Syncronize One way, recursivelly
  171. '
  172. Sub SyncToFolder(strFolderOrig, strFolderTarget, intWay)
  173. WriteLog "SyncToFolder", Array("Call", strFolderOrig, strFolderTarget, intWay)
  174.  
  175. Dim strSource
  176. Dim strDest
  177.  
  178. ' Check if source folder exists
  179. If objFSO.FolderExists(strFolderOrig) = False Then
  180. WriteLog "SyncToFolder", Array("Error", "Source folder does not exists", strFolderOrig)
  181. WScript.Quit(1)
  182. End If
  183.  
  184. Dim bError
  185. bError = False
  186.  
  187. ' Check if destiny folder exists
  188. If objFSO.FolderExists(strFolderTarget) = False Then
  189. WriteLog "SyncToFolder", Array("Create folder", strFolderTarget)
  190.  
  191. On Error Resume Next
  192. objFSO.CreateFolder(strFolderTarget)
  193. If Err.Number > 0 Then
  194. WriteLog "SyncToFolder", Array("Error", "Can't create folder", strFolderTarget, Err.Number, Err.Description, Err.Source)
  195. Err.Clear
  196. bError = True
  197. End If
  198. On Error Goto 0
  199. End If
  200.  
  201. If bError Then
  202. Exit Sub
  203. End If
  204.  
  205. Dim objFolderOrig
  206. Set objFolderOrig = objFSO.GetFolder(strFolderOrig)
  207. Dim objFolderTarget
  208. Set objFolderTarget = objFSO.GetFolder(strFolderTarget)
  209.  
  210. ' Check Files
  211. Dim objFileSrc
  212. Dim objFileDst
  213.  
  214. On Error Resume Next
  215. For Each objFileSrc in objFolderOrig.Files
  216. strSource = objFolderOrig.Path & "\" & objFileSrc.Name
  217. strDest = objFolderTarget.Path & "\" & objFileSrc.Name
  218.  
  219. If Not objFSO.FileExists(strDest) Then
  220. ' File does not exists
  221. WriteLog "SyncToFolder", Array("Copy file", strSource, strDest)
  222. objFSO.CopyFile strSource, strDest
  223. Else
  224. Set objFileDst = objFSO.GetFile(strDest)
  225.  
  226. If (objFileSrc.DateLastModified > objFileDst.DateLastModified) Then
  227. ' Copy newer file
  228. WriteLog "SyncToFolder", Array("Overwrite older file", strSource, strDest)
  229. objFSO.CopyFile strSource, strDest
  230. End If
  231. End If
  232. Next
  233. If Err.Number > 0 Then
  234. WriteLog "SyncToFolder", Array("Error", "Can't read source files", objFolderOrig, Err.Number, Err.Description, Err.Source)
  235. Err.Clear
  236. bError = True
  237. End If
  238. On Error Goto 0
  239.  
  240. If Not bError Then
  241. ' Check Subfolders
  242. Dim objSubFolder
  243.  
  244. On Error Resume Next
  245. For Each objSubFolder in objFolderOrig.SubFolders
  246. strSource = objFolderOrig.Path & "\" & objSubFolder.Name
  247. strDest = objFolderTarget.Path & "\" & objSubFolder.Name
  248.  
  249. If intWay = 0 Then
  250. ' If on way forward, check both ways
  251. SyncFolders strSource, strDest
  252. Else
  253. ' If on way back, check one way. Only if not exists
  254. If objFSO.FolderExists(strDest) = False Then
  255. SyncToFolder strSource, strDest, 1
  256. End If
  257. End If
  258. Next
  259. If Err.Number > 0 Then
  260. WriteLog "SyncToFolder", Array("Error", "Can't read subfolders", objFolderOrig, Err.Number, Err.Description, Err.Source)
  261. Err.Clear
  262. bError = True
  263. End If
  264. On Error Goto 0
  265. End If
  266. End Sub
  267.  
  268. '
  269. ' Update Backup folder, recursivelly
  270. '
  271. Sub UpdateBackupFolder(strFolderOrig, strFolderTarget)
  272. WriteLog "UpdateBackupFolder", Array("Call", strFolderOrig, strFolderTarget)
  273.  
  274. Dim strSource
  275. Dim strDest
  276.  
  277. ' Check if source folder exists
  278. If objFSO.FolderExists(strFolderOrig) = False Then
  279. WriteLog "UpdateBackupFolder", Array("Error", "Source folder does not exists", strFolderOrig)
  280. WScript.Quit(1)
  281. End If
  282.  
  283. Dim bError
  284. bError = False
  285.  
  286. ' Check if backup folder exists
  287. If objFSO.FolderExists(strFolderTarget) = False Then
  288. WriteLog "UpdateBackupFolder", Array("Create folder", strFolderTarget)
  289.  
  290. On Error Resume Next
  291. objFSO.CreateFolder(strFolderTarget)
  292. If Err.Number > 0 Then
  293. WriteLog "UpdateBackupFolder", Array("Error", "Can't create folder", strFolderTarget, Err.Number, Err.Description, Err.Source)
  294. Err.Clear
  295. bError = True
  296. End If
  297. On Error Goto 0
  298. End If
  299.  
  300. If bError Then
  301. Exit Sub
  302. End If
  303.  
  304. Dim objFolderOrig
  305. Set objFolderOrig = objFSO.GetFolder(strFolderOrig)
  306. Dim objFolderTarget
  307. Set objFolderTarget = objFSO.GetFolder(strFolderTarget)
  308.  
  309. ' Check Files
  310. Dim objFileSrc
  311. Dim objFileDst
  312.  
  313. On Error Resume Next
  314. For Each objFileDst in objFolderTarget.Files
  315. strDest = objFolderTarget.Path & "\" & objFileDst.Name
  316. strSource = objFolderOrig.Path & "\" & objFileDst.Name
  317.  
  318. If Not objFSO.FileExists(strSource) Then
  319. ' File does not exists on SOURCE. Delete it.
  320. WriteLog "UpdateBackupFolder", Array("Delete file", strDest)
  321. objFSO.DeleteFile strDest
  322. Else
  323. Set objFileSrc = objFSO.GetFile(strSource)
  324. If Err.Number > 0 Then
  325. WriteLog "UpdateBackupFolder", Array("Error", "Can't read source file", strSource, Err.Number, Err.Description, Err.Source)
  326. Err.Clear
  327. bError = True
  328. End If
  329.  
  330. If (objFileDst.DateLastModified > objFileSrc.DateLastModified) Then
  331. ' Replace back
  332. WriteLog "UpdateBackupFolder", Array("Replace back older file", strSource, strDest)
  333. objFSO.CopyFile strSource, strDest
  334. End If
  335. If Err.Number > 0 Then
  336. WriteLog "UpdateBackupFolder", Array("Error", "Can't restore this item", strSource, strDest, Err.Number, Err.Description)
  337. Err.Clear
  338. bError = True
  339. End If
  340. End If
  341. Next
  342. If Err.Number > 0 Then
  343. WriteLog "UpdateBackupFolder", Array("Error", "Can't read backup files", objFolderTarget, Err.Number, Err.Description, Err.Source)
  344. Err.Clear
  345. bError = True
  346. End If
  347. On Error Goto 0
  348.  
  349. If Not bError Then
  350. ' Check Subfolders
  351. Dim objSubFolder
  352.  
  353. On Error Resume Next
  354. For Each objSubFolder in objFolderTarget.SubFolders
  355. strDest = objFolderTarget.Path & "\" & objSubFolder.Name
  356. strSource = objFolderOrig.Path & "\" & objSubFolder.Name
  357.  
  358. ' If subfolder does not exists on SOURCE, delete all folder recursivelly
  359. If objFSO.FolderExists(strSource) = False Then
  360. DeleteAllFolder strDest
  361. End If
  362. Next
  363. If Err.Number > 0 Then
  364. WriteLog "UpdateBackupFolder", Array("Error", "Can't read subfolders", objFolderTarget, Err.Number, Err.Description, Err.Source)
  365. Err.Clear
  366. bError = True
  367. End If
  368. On Error Goto 0
  369. End If
  370. End Sub
  371.  
  372. '
  373. ' Delete all folder recursivelly
  374. '
  375. Sub DeleteAllFolder(strFolderToDelete)
  376. WriteLog "DeleteAllFolder", Array("Call", strFolderToDelete)
  377.  
  378. Dim strDest
  379.  
  380. Dim bError
  381. bError = False
  382.  
  383. ' Delete all files
  384. Dim objDeletedFolder
  385. Set objDeletedFolder = objFSO.GetFolder(strFolderToDelete)
  386.  
  387. ' Check Files
  388. Dim objDeleteFile
  389.  
  390. On Error Resume Next
  391. For Each objDeleteFile in objDeletedFolder.Files
  392. strDest = objDeletedFolder.Path & "\" & objDeleteFile.Name
  393.  
  394. WriteLog "DeleteAllFolder", Array("Delete file", strDest)
  395. objFSO.DeleteFile strDest
  396. Next
  397. If Err.Number > 0 Then
  398. WriteLog "DeleteAllFolder", Array("Error", "Can't read delete folder files", strDest, Err.Number, Err.Description, Err.Source)
  399. Err.Clear
  400. bError = True
  401. End If
  402. On Error Goto 0
  403.  
  404. If Not bError Then
  405. ' Delete all subfolders
  406. Dim objSubFolder
  407.  
  408. On Error Resume Next
  409. For Each objSubFolder in objDeletedFolder.SubFolders
  410. strDest = objDeletedFolder.Path & "\" & objSubFolder.Name
  411.  
  412. DeleteAllFolder strDest
  413. Next
  414. If Err.Number > 0 Then
  415. WriteLog "DeleteAllFolder", Array("Error", "Can't read delete subfolders", objDeletedFolder, Err.Number, Err.Description, Err.Source)
  416. Err.Clear
  417. bError = True
  418. End If
  419. On Error Goto 0
  420. End If
  421.  
  422. ' Delete itself
  423. WriteLog "DeleteAllFolder", Array("Delete folder", strFolderToDelete)
  424. objFSO.DeleteFolder(strFolderToDelete)
  425. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement