Advertisement
Linda-chan

CompressPhotos.VBS v2

Feb 4th, 2015
268
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.  
  24.   DoIt2 = False
  25.  
  26.   Set FSO = CreateObject("Scripting.FileSystemObject")
  27.   Set WShell = CreateObject("WScript.Shell")
  28.  
  29.   SourcePath = GetSourcePath()
  30.   If SourcePath = "" 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.Files) 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 GetSourcePath()
  53.   If WScript.Arguments.Count = 1 Then _
  54.     GetSourcePath = WScript.Arguments(0)
  55. End Function
  56.  
  57. Private Sub ShowUsage()
  58.   WScript.Echo "Usage: CompressPhotos.VBS SourcePath"
  59. End Sub
  60.  
  61. '====================================================================
  62. Private Function CompressPhotos(ByRef Files)
  63.   Dim File
  64.   Dim Ext
  65.  
  66.   For Each File In Files
  67.     Ext = UCase(FSO.GetExtensionName(File.Name))
  68.     If Ext = "JPG" Or Ext = "JPEG" Then
  69.       If File.Size > 1.3 * 1024 * 1024 Then
  70.         If Not CompressPhoto(File) Then
  71.           CompressPhotos = False
  72.           Exit Function
  73.         End If
  74.       Else
  75.         WScript.Echo "Ignoring: " & File.Path
  76.       End If
  77.     End If
  78.   Next
  79.  
  80.   CompressPhotos = True
  81. End Function
  82.  
  83. '====================================================================
  84. Private Function CompressPhoto(ByRef File)
  85.   Dim SourceFileName
  86.   Dim TargetFileName
  87.   Dim CmdLine
  88.   Dim RC
  89.  
  90.   On Error Resume Next
  91.  
  92.   CompressPhoto = False
  93.  
  94.   SourceFileName = File.Path
  95.   TargetFileName = File.Path & " " & AppGUID & _
  96.                    "." & FSO.GetExtensionName(File.Name)
  97.  
  98.   '==================================================================
  99.  WScript.Echo "Converting: " & SourceFileName
  100.  
  101.   CmdLine = """Y:\PortableApps\IrfanView\i_view32.exe"" " & _
  102.             """" & SourceFileName & """ " & _
  103.             "/resize_long=2000 /resample /aspectratio " & _
  104.             "/convert=""" & TargetFileName & """"
  105.   RC = WShell.Run(CmdLine, 10, True)
  106.  
  107.   If Err.Number <> 0 Then
  108.     WScript.Echo "ERROR! Can't execute IrfanView."
  109.     WScript.Echo "ERROR! Command line: " & CmdLine
  110.     Exit Function
  111.   End If
  112.  
  113.   If RC <> 0 Then
  114.     WScript.Echo "ERROR! IrfanView returns " & CStr(RC) & "."
  115.     Exit Function
  116.   End If
  117.  
  118.   '==================================================================
  119.  CmdLine = "RecycleFile.EXE " & _
  120.             """" & SourceFileName & """"
  121.   RC = WShell.Run(CmdLine, 0, True)
  122.  
  123.   If Err.Number <> 0 Then
  124.     WScript.Echo "ERROR! Can't execute AJPapps - Recycle file."
  125.     WScript.Echo "ERROR! Command line: " & CmdLine
  126.     Exit Function
  127.   End If
  128.  
  129.   If RC <> 0 Then
  130.     WScript.Echo "ERROR! AJPapps - Recycle file returns " & CStr(RC) & "."
  131.     Exit Function
  132.   End If
  133.  
  134.   '==================================================================
  135.  FSO.MoveFile TargetFileName, SourceFileName
  136.  
  137.   If Err.Number <> 0 Then
  138.     WScript.Echo "ERROR! Can't move file: " & TargetFileName
  139.     Exit Function
  140.   End If
  141.  
  142.   '==================================================================
  143.  'WScript.Echo File.Path & " :: " & File.Size & " bytes"
  144.  'WScript.Echo SourceFileName & " ==> " & TargetFileName
  145.  'WScript.Echo CmdLine
  146.  'WScript.Echo File.Path & " :: RC ==> " & RC
  147.  
  148.   CompressPhoto = True
  149. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement