amloessb

folderRar.vbs (v1.1)

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