amloessb

folderRar.vbs (v1.1.2)

Apr 3rd, 2012
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'Mass-RAR Script v1.1.2
  2. 'Written by Aaron Loessberg-Zahl
  3. 'Last Modified 03 April 2012   
  4. '
  5. 'RARs each folder in the specified directory into a separate archive.
  6. 'Tests archives and deletes original files.
  7. '
  8. 'This script must be run from the command line, and your WinRAR installation
  9. 'folder must be in your PATH variable.
  10. '
  11. 'For comments/questions/bugs, please contact
  12. '
  13. ' ----------------------------------------------------------------------------
  14. ' "THE BEER-WARE LICENSE" (Revision 2659):
  15. ' <[email protected]> wrote this file. As long as you retain this
  16. ' notice, you can do whatever you want with this stuff. If we meet some day,
  17. ' and you think this stuff is worth it, you can buy me a beer in return.
  18. ' ----------------------------------------------------------------------------
  19. '
  20. 'Changelog:
  21. 'v1.1.2  04-03-2012  amloessb  Corrected typos, added comments
  22. 'v1.1.1  12-04-2011  amloessb  Fixed syntax error
  23. 'v1.1    12-03-2011  amloessb  Added silly spinner thing
  24. 'v1.0    12-01-2011  amloessb  First working version
  25.  
  26. Option Explicit
  27.  
  28. On Error Goto 0
  29.  
  30. Dim objFSO, objDir, args, objSubdirs, subfolder, WshShell, objArchive
  31. Dim strDir, strCmd, intCounter, strSize, exitCode, intSpinner, strPath, strName
  32.  
  33. 'fixFolderStr (folder)
  34. 'Purpose: Put a trailing \ on the folder path if it's missing
  35. 'Returns: The corrected path string
  36. Function fixFolderStr (folder)
  37.     If Not Mid(folder,Len(folder),1) = "\" Then
  38.         fixFolderStr = folder & "\"
  39.     Else
  40.         fixFolderStr = folder
  41.     End If
  42. End Function
  43.  
  44. 'isProcessRunning (strComputer, strProcessName)
  45. 'Purpose: Determine if the specified process is running on the specified computer.
  46. '         strComputer should be "." for the local machine
  47. 'Returns: True if the process is running, false otherwise.
  48. Function isProcessRunning(strComputer, strProcessName)
  49.     Dim objWMIService, strWMIQuery
  50.  
  51.     strWMIQuery = "Select * from Win32_Process where name like '" & strProcessName & "'"
  52.    
  53.     Set objWMIService = GetObject("winmgmts:" _
  54.         & "{impersonationLevel=impersonate}!\\" _
  55.             & strComputer & "\root\cimv2")
  56.  
  57.     If objWMIService.ExecQuery(strWMIQuery).Count > 0 Then
  58.         isProcessRunning = True
  59.     Else
  60.         isProcessRunning = False
  61.     End If
  62. End Function
  63.  
  64. Set args = WScript.Arguments
  65.  
  66. If Not args.Count = 1 Then
  67.     WScript.Echo ""
  68.     WScript.Echo "Usage: cscript folderRar.vbs <directory>"
  69.     WScript.Echo ""
  70.     WScript.Echo "Adds each folder in the given directory into its own archive."
  71.     WScript.Quit 1
  72. End If
  73.  
  74. intSpinner = 0
  75. intCounter = 0
  76. strDir = args(0)
  77. Set objFSO = CreateObject("Scripting.FileSystemObject")
  78. Set WshShell = WScript.CreateObject("WScript.Shell")
  79.  
  80. If objFSO.FolderExists(strDir) Then
  81.     Set objDir = objFSO.GetFolder(strDir)
  82.     Set objSubdirs = objDir.SubFolders
  83.     WScript.Echo objSubdirs.Count & " folders found.  Now archiving..."
  84.     WScript.Echo ""
  85.     For Each subfolder In objSubdirs
  86.         strPath = subfolder.Path
  87.         strName = subfolder.Name
  88.         strCmd = "rar a -t -df " & Chr(34) & subfolder.Path & ".rar" & Chr(34)_
  89.                  & " " & Chr(34) & subfolder.Path & Chr(34)
  90.         WshShell.Run strCmd, 7, False
  91.         WScript.StdOut.Write " "
  92.         'Print a silly spinner on the screen to show that progress is being made
  93.        'Interesting consequence of how Sleep works is that the spinner moves
  94.        'slower the harder WinRAR is working.
  95.        While isProcessRunning(".", "rar.exe")
  96.             Select Case intSpinner
  97.                 Case 3
  98.                     WScript.StdOut.Write Chr(8) & "/"
  99.                     intSpinner = 0
  100.                 Case 2
  101.                     WScript.StdOut.Write Chr(8) & "|"
  102.                     intSpinner = 3
  103.                 Case 1
  104.                     WScript.StdOut.Write Chr(8) & "\"
  105.                     intSpinner = 2
  106.                 Case 0
  107.                     WScript.StdOut.Write Chr(8) & "-"
  108.                     intSpinner = 1
  109.             End Select
  110.             WScript.Sleep(10)
  111.         WEnd
  112.         WScript.StdOut.Write Chr(8)
  113.         On Error Resume Next
  114.         Set objArchive = objFSO.GetFile(strPath & ".rar")
  115.         If Err.Number <> 0 Then
  116.             WScript.Echo "ERROR: Archive " & strPath & ".rar" & _
  117.                          " could not be created."
  118.         Else
  119.             strSize = FormatNumber((objArchive.Size / 1048576.0), 2)
  120.             WScript.Echo strName & ".rar -- " & strSize & "M"
  121.             intCounter = intCounter + 1
  122.         End If
  123.         On Error Goto 0
  124.     Next
  125.     WScript.Echo ""
  126.     WScript.Echo intCounter & " folders archived successfully."
  127. Else
  128.     WScript.Echo strDir & " does not exist!"
  129. End If
Advertisement
Add Comment
Please, Sign In to add comment