Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Option Explicit
- Const AppGUID = "{D96D136C-F6C5-4F99-8344-759DD645CFDB}"
- Dim FSO
- Dim WShell
- DoIt
- '====================================================================
- Private Sub DoIt()
- If DoIt2() Then
- WScript.Quit 0
- Else
- WScript.Quit 1
- End If
- End Sub
- '====================================================================
- Private Function DoIt2()
- Dim Folder
- Dim SourcePath
- Dim Recursive
- DoIt2 = False
- Set FSO = CreateObject("Scripting.FileSystemObject")
- Set WShell = CreateObject("WScript.Shell")
- If Not GetParams(SourcePath, Recursive) Then
- ShowUsage
- Exit Function
- End If
- On Error Resume Next
- Set Folder = FSO.GetFolder(SourcePath)
- If Err.Number <> 0 Then
- WScript.Echo "ERROR! Can't get source folder."
- Exit Function
- End If
- If Not CompressPhotos(Folder, Recursive) Then Exit Function
- WScript.Echo "=============="
- WScript.Echo "All done! ^_^v"
- DoIt2 = True
- End Function
- '====================================================================
- Private Function GetParams(ByRef lpSourcePath, _
- ByRef lpRecursive)
- Select Case WScript.Arguments.Count
- Case 1
- lpSourcePath = WScript.Arguments(0)
- lpRecursive = False
- GetParams = True
- Case 2
- If UCase(WScript.Arguments(0)) = "/R" Then
- lpSourcePath = WScript.Arguments(1)
- lpRecursive = True
- GetParams = True
- Else
- GetParams = False
- End If
- Case Else
- GetParams = False
- End Select
- End Function
- Private Sub ShowUsage()
- WScript.Echo "Usage: CompressPhotos.VBS [/R] SourcePath"
- End Sub
- '====================================================================
- Private Function CompressPhotos(ByRef Folder, _
- ByVal Recursive)
- Dim File
- Dim SubFolder
- Dim Ext
- On Error Resume Next
- WScript.Echo "Reading folder: " & Folder.Path
- For Each File In Folder.Files
- Ext = UCase(FSO.GetExtensionName(File.Name))
- If Ext = "JPG" Or Ext = "JPEG" Then
- If File.Size > 1.3 * 1024 * 1024 Then
- If Not CompressPhoto(File) Then
- CompressPhotos = False
- Exit Function
- End If
- Else
- WScript.Echo "Ignoring: " & File.Path
- End If
- End If
- Next
- If Recursive Then
- For Each SubFolder In Folder.SubFolders
- If Not CompressPhotos(SubFolder, Recursive) Then
- CompressPhotos = False
- Exit Function
- End If
- Next
- End If
- CompressPhotos = True
- End Function
- '====================================================================
- Private Function CompressPhoto(ByRef File)
- Dim SourceFileName
- Dim TargetFileName
- Dim CmdLine
- Dim RC
- On Error Resume Next
- CompressPhoto = False
- SourceFileName = File.Path
- TargetFileName = File.Path & " " & AppGUID & _
- "." & FSO.GetExtensionName(File.Name)
- '==================================================================
- WScript.Echo "Converting: " & SourceFileName
- CmdLine = """Y:\PortableApps\IrfanView\i_view32.exe"" " & _
- """" & SourceFileName & """ " & _
- "/resize_long=2000 /resample /aspectratio " & _
- "/convert=""" & TargetFileName & """"
- RC = WShell.Run(CmdLine, 10, True)
- If Err.Number <> 0 Then
- WScript.Echo "ERROR! Can't execute IrfanView."
- WScript.Echo "ERROR! Command line: " & CmdLine
- Exit Function
- End If
- If RC <> 0 Then
- WScript.Echo "ERROR! IrfanView returns " & CStr(RC) & "."
- Exit Function
- End If
- '==================================================================
- CmdLine = "RecycleFile.EXE " & _
- """" & SourceFileName & """"
- RC = WShell.Run(CmdLine, 0, True)
- If Err.Number <> 0 Then
- WScript.Echo "ERROR! Can't execute AJPapps - Recycle file."
- WScript.Echo "ERROR! Command line: " & CmdLine
- Exit Function
- End If
- If RC <> 0 Then
- WScript.Echo "ERROR! AJPapps - Recycle file returns " & CStr(RC) & "."
- Exit Function
- End If
- '==================================================================
- FSO.MoveFile TargetFileName, SourceFileName
- If Err.Number <> 0 Then
- WScript.Echo "ERROR! Can't move file: " & TargetFileName
- Exit Function
- End If
- '==================================================================
- 'WScript.Echo File.Path & " :: " & File.Size & " bytes"
- 'WScript.Echo SourceFileName & " ==> " & TargetFileName
- 'WScript.Echo CmdLine
- 'WScript.Echo File.Path & " :: RC ==> " & RC
- CompressPhoto = True
- End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement