Announcement

Collapse
No announcement yet.

Changing the ini file from Excel VBA

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Changing the ini file from Excel VBA

    I use Excel VBA to call IrfanView in the command line mode. Some settings can be passed in the argument, and others have to be changed in the ini file. And a few, like Save as Progressive JPG, default to off and cannot be turned on at all. Please fix this Irfan.

    To make changes in the ini file, I wrote of bunch of functions, which I'd like to share here. (There were indents.)

    Option Explicit
    ' Excel 2002 doesn't recognize %APPDATA%, so user must be hardcoded here
    Public Const PathToIrfanIni = "C:\Users\donwi\AppData\Roaming\IrfanView\i_vi ew64 .ini"

    Private Sub ChangeIrfanSettings(WantedSettings As String)
    ' can change multiple, deliminate with ";"
    ' settings are found in %APPDATA%\IrfanView\i_view64.ini (open Notepad, Ctrl-O, and paste that in with Ctrl-V)
    ' arg examples: "SaveOption=0;SaveQuality=75" and

    Dim j As Integer
    Dim ini As String, Oldini As String, OldSetting As String, setting As String, tail As String
    Dim val As String, WantedSetting As String

    ini = ReadInUnicode(PathToIrfanIni, True)
    Oldini = ini
    WantedSettings = WantedSettings & ";"

    LoopTop:
    ' pick off this loop's setting
    j = InStr(WantedSettings, ";")
    WantedSetting = Left(WantedSettings, j - 1)
    WantedSettings = DropStr(WantedSettings, j)
    ' we test, and skip if wanted setting is already set
    If 0 = InStr(ini, WantedSetting) Then
    j = InStr(WantedSetting, "=")
    setting = Left(WantedSetting, j - 1)
    val = DropStr(WantedSetting, j)
    j = InStr(ini, setting)
    ' after = sign
    tail = DropStr(ini, j + Len(setting))
    ' remove old value
    tail = DropStr(tail, InStr(tail, vbCr) - 1)
    ' rebuild
    ini = Left(ini, j - 1) & setting & "=" & val & tail
    End If
    If Len(WantedSettings) > 0 Then GoTo LoopTop:

    If Oldini <> ini Then SaveFile PathToIrfanIni, ini

    End Sub

    Function ReadInUnicode(FileName As String, Optional RemIrfanMsg As Boolean) As String
    Dim NewStr As String
    Dim i As Long
    Dim res As Variant

    res = ReadByteArrFromFile(FileName)

    If (res(0) = 255 And res(1) = 254) Or (res(0) = 254 And res(1) = 255) Then
    For i = 2 To UBound(res)
    If res(i) <> 0 Then
    NewStr = NewStr & Chr(res(i))
    End If
    Next i
    ' for IrfanView we need to remove from beginning
    If RemIrfanMsg Then
    NewStr = DropStr(NewStr, InStr(NewStr, "[Lan") - 1)
    End If
    Else
    For i = 0 To UBound(res)
    NewStr = NewStr & Chr(res(i))
    Next i
    End If

    ReadInUnicode = NewStr

    End Function


    Function ReadByteArrFromFile(FilePath) As Byte()
    ' needed to read in Unicode, which isn't character text
    ' From: https://www.codestack.net/visual-bas...d-binary-file/
    Dim buff() As Byte
    Dim fileNum As Integer
    fileNum = FreeFile
    Open FilePath For Binary Access Read As fileNum
    ReDim buff(0 To LOF(fileNum) - 1)
    Get fileNum, , buff
    Close fileNum
    ReadByteArrFromFile = buff
    End Function

    Sub SaveFile(ByVal PathName As String, D As Variant)
    If FileExists(PathName) Then Kill PathName
    Open PathName For Output As Home
    ' VBA likes to add a CRLF at the end. So we remove ours.
    ' -> be careful. be sure you have one to remove
    If Len(D) > 2 Then
    D = DropStr(D, -2)
    End If
    Print #1, D
    Close Home
    End Sub

    Function DropStr(ByVal S As String, ByVal n As Long) As String
    ' if n is negative drops from end
    If n = 0 Then
    DropStr = S
    ElseIf n < 0 Then
    DropStr = Left(S, Len(S) + n)
    Else
    DropStr = Right(S, Len(S) - n)
    End If
    End Function

    Function FileExists(ByVal FilePath As String) As Boolean
    ' Checks if a file or path exists (using the Dir function). does not distinguish between them
    On Error Resume Next
    If Len(FilePath) > 0 Then
    If Not Dir(FilePath, vbDirectory) = vbNullString Then FileExists = True
    End If
    On Error GoTo 0
    End Function


    #2
    I believe "Save as Progressive JPG" is available as:

    [JPEG]
    Save Quality=80
    Save Progressive=1 <-
    Save Grayscale=0

    in AppData\Roaming\IrfanView\i_view64.ini

    Comment


      #3
      By the way, I use this to edit the ini file for IrfanView, much the way you describe:

      Comment


        #4
        Yes, Save Progressive is in the ini file, but when running under the command prompt, it is ignored, and there is no way to get Progressive under batch. Irfan knows this.
        Does Schaeffer's handle Unicode cleanly? I use VBA, as all my processing is in VBA, and this is easily used there.
        Last edited by donwiss; 04.10.2024, 11:00 PM.

        Comment


          #5
          I tested it on my machine, Win 10 Home 22H2 with IrfanView 64 4.66, and the Save Progressive is honored on my machine. I use a copy of he original ini file (so I don't have to worry about changing it when I run IV interactive) and the following batch command:

          i_view64.exe %%~nx1 /ini="C:\Programs_OS\command\irfanview" /advancedbatch /convert=%%~n1_dark%%~x1

          where the /convert creates a file with _dark appended to the name. The /ini option specified the directory that the ini file is in.

          Schaeffer's has modified my ini many times with no problems. I suggest:

          inifile C:\Programs_OS\command\irfanview\i_view64.ini [jpeg] Save Progressive=1

          to save jpeg progressive from command line batch. I hope this helps. I noticed in the inifile program help that "set Save Progressive=1" adds an item to the inifile which is not what you want. This is from "sends SET statement(s) to STDOUT". The "set " is not needed and should not be used.
          Last edited by IrfanUserGuyxyx; 05.10.2024, 05:13 PM.

          Comment


            #6
            My ini file change routine works flawlessly.
            The Save Progressive is honored if you are creating the file. But not if you simply want to optimize a file. Like recursively optimize all jpegs on the hard drive. See my post on "Getting file as small as possible?" just below this thread.?

            Comment


              #7
              I seems to work in save as. If you append a unique string to each file processed as in the IV command line in my previous post you could then overwrite the original files with the new files in a single command. Can you even /convert to the same filename?

              Can you copy or delete files in VBA?
              Last edited by IrfanUserGuyxyx; 07.10.2024, 03:37 AM.

              Comment


                #8
                Originally posted by IrfanUserGuyxyx View Post
                It seems to work in save as. If you append a unique string to each file processed as in the IV command line in my previous post you could then overwrite the original files with the new files in a single command. Can you even /convert to the same filename?
                Can you copy or delete files in VBA?
                Yes. I can do all that. And I was when using a different program. I don't know about a single command. I hadn't realized that Save As worked, though it makes sense, as Progressive works when creating after a resize. I can Save As, then delete original, and then rename. I consider this a bug. I'd like to optimize a hard drive with thousands of images.

                Comment


                  #9
                  i_view64.exe %%~nx1 /ini="C:\Programs_OS\command\irfanview" /advancedbatch /convert=%%~n1_dark%%~x1

                  worked with Progressive

                  Comment

                  Working...
                  X