amloessb

folderRar.vbs (v1.1.1)

Dec 4th, 2011
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'Mass-RAR Script v1.1.1
  2. 'Written by Aaron Loessberg-Zahl
  3. 'Last Modified 03 Dec 2011
  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.1  12-04-2011  amloessb  Fixed syntax error
  22. 'v1.1    12-03-2011  amloessb  Added silly spinner thing
  23. 'v1.0    12-01-2011  amloessb  First working version
  24.  
  25. Option Explicit
  26.  
  27. On Error Goto 0
  28.  
  29. Dim objFSO, objDir, args, objSubdirs, subfolder, WshShell, objArchive
  30. Dim strDir, strCmd, intCounter, strSize, exitCode, intSpinner, strPath, strName
  31.  
  32. 'fixFolderStr (folder)
  33. 'Purpose: Put a trailing \ on the folder path if it's missing
  34. 'Returns: The correctrd path string
  35. Function fixFolderStr (folder)
  36.     If Not Mid(folder,Len(folder),1) = "\" Then
  37.         fixFolderStr = folder & "\"
  38.     Else
  39.         fixFolderStr = folder
  40.     End If
  41. End Function
  42.  
  43. Function isProcessRunning(strComputer, strProcessName)
  44.     Dim objWMIService, strWMIQuery
  45.  
  46.     strWMIQuery = "Select * from Win32_Process where name like '" & strProcessName & "'"
  47.    
  48.     Set objWMIService = GetObject("winmgmts:" _
  49.         & "{impersonationLevel=impersonate}!\\" _
  50.             & strComputer & "\root\cimv2")
  51.  
  52.     If objWMIService.ExecQuery(strWMIQuery).Count > 0 Then
  53.         isProcessRunning = True
  54.     Else
  55.         isProcessRunning = False
  56.     End If
  57. End Function
  58.  
  59. Set args = WScript.Arguments
  60.  
  61. If Not args.Count = 1 Then
  62.     WScript.Echo ""
  63.     WScript.Echo "Usage: cscript folderRar.vbs <directory>"
  64.     WScript.Echo ""
  65.     WScript.Echo "Adds each folder in the given directory into its own archive."
  66.     WScript.Quit 1
  67. End If
  68.  
  69. intSpinner = 0
  70. intCounter = 0
  71. strDir = args(0)
  72. Set objFSO = CreateObject("Scripting.FileSystemObject")
  73. Set WshShell = WScript.CreateObject("WScript.Shell")
  74.  
  75. If objFSO.FolderExists(strDir) Then
  76.     Set objDir = objFSO.GetFolder(strDir)
  77.     Set objSubdirs = objDir.SubFolders
  78.     WScript.Echo objSubdirs.Count & " folders found.  Now archiving..."
  79.     WScript.Echo ""
  80.     For Each subfolder In objSubdirs
  81.         strPath = subfolder.Path
  82.         strName = subfolder.Name
  83.         strCmd = "rar a -t -df " & Chr(34) & subfolder.Path & ".rar" & Chr(34)_
  84.                  & " " & Chr(34) & subfolder.Path & Chr(34)
  85.         WshShell.Run strCmd, 7, False
  86.         WScript.StdOut.Write " "
  87.         While isProcessRunning(".", "rar.exe")
  88.             Select Case intSpinner
  89.                 Case 3
  90.                     WScript.StdOut.Write Chr(8) & "/"
  91.                     intSpinner = 0
  92.                 Case 2
  93.                     WScript.StdOut.Write Chr(8) & "|"
  94.                     intSpinner = 3
  95.                 Case 1
  96.                     WScript.StdOut.Write Chr(8) & "\"
  97.                     intSpinner = 2
  98.                 Case 0
  99.                     WScript.StdOut.Write Chr(8) & "-"
  100.                     intSpinner = 1
  101.             End Select
  102.             WScript.Sleep(10)
  103.         WEnd
  104.         WScript.StdOut.Write Chr(8)
  105.         On Error Resume Next
  106.         Set objArchive = objFSO.GetFile(strPath & ".rar")
  107.         If Err.Number <> 0 Then
  108.             WScript.Echo "ERROR: Archive " & strPath & ".rar" & _
  109.                          " could not be created."
  110.         Else
  111.             strSize = FormatNumber((objArchive.Size / 1048576.0), 2)
  112.             WScript.Echo strName & ".rar -- " & strSize & "M"
  113.             intCounter = intCounter + 1
  114.         End If
  115.         On Error Goto 0
  116.     Next
  117.     WScript.Echo ""
  118.     WScript.Echo intCounter & " folders archived successfully."
  119. Else
  120.     WScript.Echo strDir & " does not exist!"
  121. End If
Advertisement
Add Comment
Please, Sign In to add comment