Advertisement
Linda-chan

CompressPhotos.VBS v3

Feb 5th, 2015
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Option Explicit
  2.  
  3. Const AppGUID = "{D96D136C-F6C5-4F99-8344-759DD645CFDB}"
  4.  
  5. Dim FSO
  6. Dim WShell
  7.  
  8. DoIt
  9.  
  10. '====================================================================
  11. Private Sub DoIt()
  12.   If DoIt2() Then
  13.     WScript.Quit 0
  14.   Else
  15.     WScript.Quit 1
  16.   End If
  17. End Sub
  18.  
  19. '====================================================================
  20. Private Function DoIt2()
  21.   Dim Folder
  22.   Dim SourcePath
  23.   Dim Recursive
  24.  
  25.   DoIt2 = False
  26.  
  27.   Set FSO = CreateObject("Scripting.FileSystemObject")
  28.   Set WShell = CreateObject("WScript.Shell")
  29.  
  30.   If Not GetParams(SourcePath, Recursive) Then
  31.     ShowUsage
  32.     Exit Function
  33.   End If
  34.  
  35.   On Error Resume Next
  36.  
  37.   Set Folder = FSO.GetFolder(SourcePath)
  38.   If Err.Number <> 0 Then
  39.     WScript.Echo "ERROR! Can't get source folder."
  40.     Exit Function
  41.   End If
  42.  
  43.   If Not CompressPhotos(Folder, Recursive) Then Exit Function
  44.  
  45.   WScript.Echo "=============="
  46.   WScript.Echo "All done! ^_^v"
  47.  
  48.   DoIt2 = True
  49. End Function
  50.  
  51. '====================================================================
  52. Private Function GetParams(ByRef lpSourcePath, _
  53.                            ByRef lpRecursive)
  54.   Select Case WScript.Arguments.Count
  55.     Case 1
  56.       lpSourcePath = WScript.Arguments(0)
  57.       lpRecursive = False
  58.       GetParams = True
  59.     Case 2
  60.       If UCase(WScript.Arguments(0)) = "/R" Then
  61.         lpSourcePath = WScript.Arguments(1)
  62.         lpRecursive = True
  63.         GetParams = True
  64.       Else
  65.         GetParams = False
  66.       End If
  67.     Case Else
  68.       GetParams = False
  69.   End Select
  70. End Function
  71.  
  72. Private Sub ShowUsage()
  73.   WScript.Echo "Usage: CompressPhotos.VBS [/R] SourcePath"
  74. End Sub
  75.  
  76. '====================================================================
  77. Private Function CompressPhotos(ByRef Folder, _
  78.                                 ByVal Recursive)
  79.   Dim File
  80.   Dim SubFolder
  81.   Dim Ext
  82.  
  83.   On Error Resume Next
  84.  
  85.   WScript.Echo "Reading folder: " & Folder.Path
  86.  
  87.   For Each File In Folder.Files
  88.     Ext = UCase(FSO.GetExtensionName(File.Name))
  89.     If Ext = "JPG" Or Ext = "JPEG" Then
  90.       If File.Size > 1.3 * 1024 * 1024 Then
  91.         If Not CompressPhoto(File) Then
  92.           CompressPhotos = False
  93.           Exit Function
  94.         End If
  95.       Else
  96.         WScript.Echo "Ignoring: " & File.Path
  97.       End If
  98.     End If
  99.   Next
  100.  
  101.   If Recursive Then
  102.     For Each SubFolder In Folder.SubFolders
  103.       If Not CompressPhotos(SubFolder, Recursive) Then
  104.         CompressPhotos = False
  105.         Exit Function
  106.       End If
  107.     Next
  108.   End If
  109.  
  110.   CompressPhotos = True
  111. End Function
  112.  
  113. '====================================================================
  114. Private Function CompressPhoto(ByRef File)
  115.   Dim SourceFileName
  116.   Dim TargetFileName
  117.   Dim CmdLine
  118.   Dim RC
  119.  
  120.   On Error Resume Next
  121.  
  122.   CompressPhoto = False
  123.  
  124.   SourceFileName = File.Path
  125.   TargetFileName = File.Path & " " & AppGUID & _
  126.                    "." & FSO.GetExtensionName(File.Name)
  127.  
  128.   '==================================================================
  129.  WScript.Echo "Converting: " & SourceFileName
  130.  
  131.   CmdLine = """Y:\PortableApps\IrfanView\i_view32.exe"" " & _
  132.             """" & SourceFileName & """ " & _
  133.             "/resize_long=2000 /resample /aspectratio " & _
  134.             "/convert=""" & TargetFileName & """"
  135.   RC = WShell.Run(CmdLine, 10, True)
  136.  
  137.   If Err.Number <> 0 Then
  138.     WScript.Echo "ERROR! Can't execute IrfanView."
  139.     WScript.Echo "ERROR! Command line: " & CmdLine
  140.     Exit Function
  141.   End If
  142.  
  143.   If RC <> 0 Then
  144.     WScript.Echo "ERROR! IrfanView returns " & CStr(RC) & "."
  145.     Exit Function
  146.   End If
  147.  
  148.   '==================================================================
  149.  CmdLine = "RecycleFile.EXE " & _
  150.             """" & SourceFileName & """"
  151.   RC = WShell.Run(CmdLine, 0, True)
  152.  
  153.   If Err.Number <> 0 Then
  154.     WScript.Echo "ERROR! Can't execute AJPapps - Recycle file."
  155.     WScript.Echo "ERROR! Command line: " & CmdLine
  156.     Exit Function
  157.   End If
  158.  
  159.   If RC <> 0 Then
  160.     WScript.Echo "ERROR! AJPapps - Recycle file returns " & CStr(RC) & "."
  161.     Exit Function
  162.   End If
  163.  
  164.   '==================================================================
  165.  FSO.MoveFile TargetFileName, SourceFileName
  166.  
  167.   If Err.Number <> 0 Then
  168.     WScript.Echo "ERROR! Can't move file: " & TargetFileName
  169.     Exit Function
  170.   End If
  171.  
  172.   '==================================================================
  173.  'WScript.Echo File.Path & " :: " & File.Size & " bytes"
  174.  'WScript.Echo SourceFileName & " ==> " & TargetFileName
  175.  'WScript.Echo CmdLine
  176.  'WScript.Echo File.Path & " :: RC ==> " & RC
  177.  
  178.   CompressPhoto = True
  179. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement