Sign in to follow this  
Followers 0
ezzetabi

Picture converter.

3 posts in this topic

#1 ·  Posted (edited)

topng.exe a freeware command line tool is needed.

This wrapper allows to easy convert whole folders, from many formats to many other. I know that most of us can do a script like this sleeping, still I like it because topng.exe works with a single file per time.

This code assumes you have topng.exe in a %path% folder, if you don't alter the line 42.

; You can also automate giving the starting dir, the destination dir and type as $cmdline [1] [2] and [3].
Opt ('MustDeclareVars', 1)

Dim $sOrigin, $sDest, $sType, $sOriginType, $aFiles, $sDestType, $c, $sBatFile
$sOriginType = 'bmp|jpg|jif|jpeg|png|ico|jng|koa|iff|lbm|mng|pbm|pcd|pcx|pgm|ppm|ras|tga|targa|tif|tiff|wbmp|wap

|psd|cut'
$sDestType = 'bmp|jpeg|png|pbm|pgm|ppm|tiff'

If $cmdline[0] > 0 Then
   If Not FileExists($cmdline[1]) Then Exit
   $sOrigin = $cmdline[1]
Else
   $sOrigin = FileSelectFolder('Select where are original files.', @HomeDrive)
   If @error Then Exit
EndIf
If StringRight($sOrigin, 1) <> '\' Then $sOrigin = $sOrigin & '\'

If $cmdline[0] > 1 Then
   If Not FileExists($cmdline[2]) Then Exit
   $sDest = $cmdline[2]
Else
   $sDest = FileSelectFolder('Select destination.', @HomeDrive)
   If @error Then Exit
EndIf
If StringRight($sDest, 1) <> '\' Then $sDest = $sDest & '\'

If $cmdline[0] > 2 Then
   If Not _Any($sDestType, $cmdline[3]) Then Exit
   $sType = $cmdline[3]
Else
   Do
      $sType = InputBox('Select', 'Select destination type:' & @LF & StringReplace($sDestType, '|', ' '), 'bmp')
      If @error Then Exit
   Until _Any($sDestType, $sType)
EndIf

ToolTip('Working...', @DesktopWidth / 2, @DesktopHeight / 2)
$aFiles = _FileSearch($sOrigin, 5)

$sBatFile = '@echo off' & @CRLF
For $c = 1 To $aFiles[0]
   If _Any($sOriginType, StringTrimLeft($aFiles[$c], StringInStr($aFiles[$c], '.', 0, -1))) Then
      $sBatFile = $sBatFile & 'topng "' & $aFiles[$c] & '" ' & $sType & @CRLF & 'move "' & StringLeft($aFiles[$c], StringInStr($aFiles[$c], '.', 0, -1)) & $sType & '" "' & $sDest & '"' & @CRLF
   EndIf
Next
$sBatFile = $sBatFile & 'del "' & @TempDir & '\~execute.bat"' & @CRLF
FileWrite(@TempDir & '\~execute.bat', $sBatFile)

RunWait(@TempDir & '\~execute.bat', '')
ToolTip('')


Exit
Func _Any($sList, $sItem)
   Local $aList, $c
   $aList = StringSplit($sList, '|')
   For $c = 1 To $aList[0]
      If $sItem = $aList[$c] Then Return 1
   Next
   Return 0
EndFunc  ;==>_Any

Func _FileSearch($sIstr, $iSF)
  ; $iSF can sum up.
  ; $iSF = 4 means don't return also folders, only files.
  ; $iSF = 2 means stop at the first found.
  ; $iSF = 1 means looking in subfolders.
   
  ; An array is returned with the full path of all files found. The pos [0] keeps the number of elements.
   Local $sIstr, $iSF, $sCriteria, $sBuffer, $iH, $iH2, $sCS, $sCF, $sCF2, $sCP, $sFP, $sOutPut = '', $aNull[1]
   $sCP = StringLeft($sIstr, StringInStr($sIstr, '\', 0, -1))
   If $sCP = '' Then $sCP = @WorkingDir & '\'
   $sCriteria = StringTrimLeft($sIstr, StringInStr($sIstr, '\', 0, -1))
   If $sCriteria = '' Then $sCriteria = '*.*'
   
  ;To begin we seek in the starting path.
   $sCS = FileFindFirstFile($sCP & $sCriteria)
   While $sCS <> - 1
      $sCF = FileFindNextFile($sCS)
      If @error Then
         FileClose($sCS)
         ExitLoop
      EndIf
      If $sCF = '.' Or $sCF = '..' Then ContinueLoop
      If Not (BitAND($iSF, 4) And StringInStr(FileGetAttrib($sCP & $sCF), 'd')) Then
         $sOutPut = $sOutPut & $sCP & $sCF & @LF
         If BitAND($iSF, 2) Then ExitLoop
      EndIf
   Wend
   
  ;And after, if needed, in the rest of the folders.
   If (BitAND($iSF, 2) Or BitAND($iSF, 3) And $sOutPut = '') Or (BitAND($iSF, 1) And Not BitAND($iSF, 2)) Then
      $sBuffer = @CR & $sCP & '*' & @LF;The buffer is set for keeping the given path plus a *.
      Do
         $sCS = StringTrimLeft(StringLeft($sBuffer, StringInStr($sBuffer, @LF, 0, 1) - 1), 1);current search.
         $sCP = StringLeft($sCS, StringInStr($sCS, '\', 0, -1));Current search path.
         $iH = FileFindFirstFile($sCS)
         While $iH <> - 1
            $sCF = FileFindNextFile($iH)
            If @error Then
               FileClose($iH)
               ExitLoop
            EndIf
            If $sCF = '.' Or $sCF = '..' Then ContinueLoop
            If StringInStr(FileGetAttrib($sCP & $sCF), 'd') Then
               $sBuffer = @CR & $sCP & $sCF & '\*' & @LF & $sBuffer;Every folder found is added in the begin of buffer
               $sFP = $sCP & $sCF & '\';                            for future search
               $iH2 = FileFindFirstFile($sFP & $sCriteria);         and checked with the criteria.
               While $iH2 <> - 1
                  $sCF2 = FileFindNextFile($iH2)
                  If @error Then
                     FileClose($iH2)
                     ExitLoop
                  EndIf
                  If $sCF2 = '.' Or $sCF2 = '..' Then ContinueLoop
                  If Not (BitAND($iSF, 4) And StringInStr(FileGetAttrib($sFP & $sCF2), 'd')) Then
                     $sOutPut = $sOutPut & $sFP & $sCF2 & @LF;Found items are put in the Output.
                     If BitAND($iSF, 2) Then
                        FileClose($iH2)
                        FileClose($iH)
                        ExitLoop 3
                     EndIf
                  EndIf
               Wend
            EndIf
         Wend
         $sBuffer = StringReplace($sBuffer, @CR & $sCS & @LF, '')
      Until $sBuffer = ''
   EndIf
   
   If $sOutPut = '' Then
      $aNull[0] = 0
      Return $aNull
   Else
      Return StringSplit(StringTrimRight($sOutPut, 1), @LF)
   EndIf
EndFunc  ;==>_FileSearch

:)

Edited by ezzetabi

Share this post


Link to post
Share on other sites



I see you've been to shellcity.net... :)


Who else would I be?

Share this post


Link to post
Share on other sites

Seems so.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0