Advertisement
Linda-chan

CompressPhotos.VBS

Feb 3rd, 2015
325
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.   Dim Folder
  13.   Dim SourcePath
  14.  
  15.   Set FSO = CreateObject("Scripting.FileSystemObject")
  16.   Set WShell = CreateObject("WScript.Shell")
  17.  
  18.   SourcePath = GetSourcePath()
  19.   If SourcePath = "" Then
  20.     ShowUsage
  21.     Exit Sub
  22.   End If
  23.  
  24.   On Error Resume Next
  25.  
  26.   Set Folder = FSO.GetFolder(SourcePath)
  27.   If Err.Number <> 0 Then
  28.     WScript.Echo "ERROR! Can't get source folder."
  29.     Exit Sub
  30.   End If
  31.  
  32.   If Not CompressPhotos(Folder.Files) Then Exit Sub
  33.  
  34.   WScript.Echo "=============="
  35.   WScript.Echo "All done! ^_^v"
  36. End Sub
  37.  
  38. '====================================================================
  39. Private Function GetSourcePath()
  40.   If WScript.Arguments.Count = 1 Then _
  41.     GetSourcePath = WScript.Arguments(0)
  42. End Function
  43.  
  44. Private Sub ShowUsage()
  45.   WScript.Echo "Usage: CompressPhotos.VBS SourcePath"
  46. End Sub
  47.  
  48. '====================================================================
  49. Private Function CompressPhotos(ByRef Files)
  50.   Dim File
  51.   Dim Ext
  52.  
  53.   For Each File In Files
  54.     Ext = UCase(FSO.GetExtensionName(File.Name))
  55.     If Ext = "JPG" Or Ext = "JPEG" Then
  56.       If File.Size > 1.5 * 1024 * 1024 Then
  57.         If Not CompressPhoto(File) Then
  58.           CompressPhotos = False
  59.           Exit Function
  60.         End If
  61.       Else
  62.         WScript.Echo "Ignoring: " & File.Path
  63.       End If
  64.     End If
  65.   Next
  66.  
  67.   CompressPhotos = True
  68. End Function
  69.  
  70. '====================================================================
  71. Private Function CompressPhoto(ByRef File)
  72.   Dim SourceFileName
  73.   Dim TargetFileName
  74.   Dim CmdLine
  75.   Dim RC
  76.  
  77.   On Error Resume Next
  78.  
  79.   CompressPhoto = False
  80.  
  81.   SourceFileName = File.Path
  82.   TargetFileName = File.Path & " " & AppGUID & _
  83.                    "." & FSO.GetExtensionName(File.Name)
  84.  
  85.   '==================================================================
  86.  WScript.Echo "Converting: " & SourceFileName
  87.  
  88.   CmdLine = """Y:\PortableApps\IrfanView\i_view32.exe"" " & _
  89.             """" & SourceFileName & """ " & _
  90.             "/resize_long=2000 /resample /aspectratio " & _
  91.             "/convert=""" & TargetFileName & """"
  92.   RC = WShell.Run(CmdLine, 10, True)
  93.  
  94.   If Err.Number <> 0 Then
  95.     WScript.Echo "ERROR! Can't execute IrfanView."
  96.     WScript.Echo "ERROR! Command line: " & CmdLine
  97.     Exit Function
  98.   End If
  99.  
  100.   If RC <> 0 Then
  101.     WScript.Echo "ERROR! IrfanView returns " & CStr(RC) & "."
  102.     Exit Function
  103.   End If
  104.  
  105.   '==================================================================
  106.  CmdLine = "RecycleFile.EXE " & _
  107.             """" & SourceFileName & """"
  108.   RC = WShell.Run(CmdLine, 0, True)
  109.  
  110.   If Err.Number <> 0 Then
  111.     WScript.Echo "ERROR! Can't execute AJPapps - Recycle file."
  112.     WScript.Echo "ERROR! Command line: " & CmdLine
  113.     Exit Function
  114.   End If
  115.  
  116.   If RC <> 0 Then
  117.     WScript.Echo "ERROR! AJPapps - Recycle file returns " & CStr(RC) & "."
  118.     Exit Function
  119.   End If
  120.  
  121.   '==================================================================
  122.  FSO.MoveFile TargetFileName, SourceFileName
  123.  
  124.   If Err.Number <> 0 Then
  125.     WScript.Echo "ERROR! Can't move file: " & TargetFileName
  126.     Exit Function
  127.   End If
  128.  
  129.   '==================================================================
  130.  'WScript.Echo File.Path & " :: " & File.Size & " bytes"
  131.  'WScript.Echo SourceFileName & " ==> " & TargetFileName
  132.  'WScript.Echo CmdLine
  133.  'WScript.Echo File.Path & " :: RC ==> " & RC
  134.  
  135.   CompressPhoto = True
  136. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement