PDA

View Full Version : Handling Unicode Text Files from Irfanview v4.50 using a Visual Basic script



Mij
20.12.2017, 04:59 PM
As you will probably have discovered Irfanview from version 4.50 on converts the initialisation file (i_view32.ini or i_view64.ini) into a Unicode Text File (UTF) when you first change one of the Properties/Settings. It does the same with lists of files that you save from the Slideshow or Thumbnails windows.
That means that a lot of my Visual basic scripts that operate on those files no longer work. VB Scripts can work with either Unicode or ASCII files but you need to know which type it is before you open it.

So I am posting a working script to show how I handle the problem. This is a script I am using that lets me change the settings of keys within the ini file from a VB script that has been launched in Irfanview as an External editor. The script file is what I have used before but the lines with comments against them are new (comments in a line of script start after a ' character). Basically I assume the file is Unicode, open it as that and read in the file contents. I then look for an end of line sequence (Carriage return, Line Feed) and if I cannot find one, assume that it is an ASCII file and open in that form instead (a CR,LF from an ASCII file looks like some Chinese character in Unicode).



'Drop an ini file name onto VBS file icon. Edits contents as either ASCII or Unicode text format

If Wscript.Arguments.Count<>1 then Wscript.Quit
Set objFSO= CreateObject("Scripting.FileSystemObject")
IniFile=WScript.Arguments(0)
UTF=-1 : Set myFile =objFSO.OpenTextFile (IniFile, 1, False, UTF) 'Assume to be UTF and open
content=myFile.ReadAll : myFile.close 'Read whole file in UTF
If instr(content,vbCrLf)=0 then
UTF=0 : Set myFile =objFSO.OpenTextFile (IniFile, 1, False, UTF) 'Not UTF so open as ASCII
Content=myFile.ReadAll : myfile.close 'Read whole file in ASCII
End If

Edit_ini Content,"WinPosition","Maximized=1" : Edit_ini Content,"Viewing","ViewAll=0" 'This line calls the subroutine Edit_ini

Set myFile =objFSO.OpenTextFile (IniFile, 2, False, UTF) 'Open for writing as original type
myFile.write content : myFile.close 'Write whole file as original type
Set ObjFSO=Nothing : WScript.Quit

Sub Edit_ini (strAll,header,entry)
Eq=Instr(1,entry,"=") : key=left(entry,Eq) : header= "[" & header & "]"
b1=instr(strAll,header)
If b1=0 then
strAll=strAll & header &vbCrLf & entry &vbCrLf
else
b2=instr(b1+1,strAll & "[",vbCrLf&"[") : e1=instr(b1,strAll,key)
If e1=0 then
strAll=left(strAll,b2+1) & entry & right(strAll,len(strAll)-b2+1)
else
e2=instr(e1,strAll,vbCrLf) : old_entry = mid(strAll,e1,e2-e1)
strAll=replace(strAll,old_entry,entry)
End If
End If
End sub


If you want to try it I have also attached it below as a text file. Just rename the file with ".vbs" on the end instead of ".txt" then drag and drop an "i_view 32.ini" file onto the Script file icon and you will see that the "Maximized" key in the "WinPosition" block changes to "=1" and "ViewAll" in the "Viewing" block to "=0". This is something I use to launch the current image in a frameless Irfanview window when I want to examine details using zoom and pan controls without the risk of jumping to a new image all the time.