$sFile = 'My File'
$sOld_Comments = FileGetVersion($sFile, 'Comments')
ConsoleWrite('Old Comments: ' & $sOld_Comments & @CRLF)
_FileSetVersion($sFile, 'Comments', 'My New Comments')
ConsoleWrite('New Comments: ' & FileGetVersion($sFile, 'Comments') & @CRLF)
_FileSetVersion($sFile, 'Comments', $sOld_Comments)
; #FUNCTION# ====================================================================================================================
; Name...........: _FileSetVersion
; Description ...: Updating the StringFileInfo section.
; Syntax.........: _FileSetVersion( FileName, "StringName", "StringText" )
; Parameters ....: FileName - Path and filename.
; StringName- name of the field info, default is "FileVersion".
; StringText- string to fill the field info, default is @AutoItVersion.
; Return values .: Success - Returns 1 and set @error to 0
; Failure - Returns 0 and set @error to 1
; Author ........: jscript "FROM BRAZIL"
; Modified.......:
; Remarks .......: The information contained in the StringFileInfo section is not necessarily more difficult
; to change, but it does have the constraint that the new information be the same size or smaller
; than the old information. Extra characters would inevitably write on something important.
; Related .......: FileGetVersion
; Link ..........;
; Example .......; Yes
; ===============================================================================================================================
Func _FileSetVersion($hFileName, $sStringName = "FileVersion", $sStringText = @AutoItVersion)
;==============================================
; Local Constant/Variable Declaration Section
;==============================================
Local $iResourceSize, $hFileOpen, $bFileRead, $bSearch, $bReplace, $bHex, $NewResSize, $iPos
Local $aStrSplit, $iResFieldSize, $bBinStrName, $sBinStrText, $sFileGetInfo, $iValue, $iCont
Local $bHeader = "34000000560053005f00560045005200530049004f004e005f0049004e0046004f"
Local $bEndHeader = "5400720061006e0073006c006100740069006f006e0000000000"
;
; If not file found return 0 and set @error to 1
If Not FileExists($hFileName) Then Return SetError(1, 0, 0)
;
; First open file in only binary read mode and close
$hFileOpen = FileOpen($hFileName, 16)
$bFileRead = FileRead($hFileOpen)
FileClose($hFileOpen)
;
; Get resource info, if no version information found, return 0 and set @error to 1
If FileGetVersion($hFileName) = "0.0.0.0" Then Return SetError(1, 0, 0)
#cs
;
; Determine the size of the resource information
$bSearch = StringMid($bFileRead, StringInStr($bFileRead, $bHeader, 0, -1) - 4, 4)
; Invert and return value in bytes
$iResourceSize = Dec(StringRight($bSearch, 2) & StringLeft($bSearch, 2))
#ce
;
; Update Resource information
If $sStringName = "FILEVERSION" Or $sStringName = "PRODUCTVERSION" Then
;
; Split FileVersion, format is: 00.00.0000.0000
$aStrSplit = StringSplit($sStringText, ".")
; If wrong FileVersion format, return 0 and set @error to 1
If $aStrSplit[0] > 4 Then Return SetError(1, 0, 0)
;
; Convert to Hex and Invert data
Dim $bReplace[5] = [5, Hex(0, 4), Hex(0, 4), Hex(0, 4), Hex(0, 4)]
For $i = 1 To $aStrSplit[0]
$bHex = Hex($aStrSplit[$i], 4)
$bReplace[$i] = StringRight($bHex, 2) & StringLeft($bHex, 2)
Next
;
; Set FILEVERSION / PRODUCTVERSION
If $sStringName = "FILEVERSION" Then $iPos = StringInStr($bFileRead, "bd04effe00000100", 0, -1) + 16
If $sStringName = "PRODUCTVERSION" Then $iPos = StringInStr($bFileRead, "bd04effe00000100", 0, -1) + 32
$bFileRead = StringReplace($bFileRead, $iPos, $bReplace[2] & $bReplace[1] & $bReplace[4] & $bReplace[3])
; free the memory
$aStrSplit = 0
$bReplace = 0
;
; If error found, return 0 and set @error to 1
If @error Then Return SetError(1, 0, 0)
Else
; If FileGetVersion returns error then
If @error Then Return SetError(1, 0, 0)
;
; Set the length of $sStringName resource field
$bBinStrName = __Binary(__NullEntryPoint($sStringName))
$iPos = StringInStr($bFileRead, $bBinStrName)
$iValue = StringMid($bFileRead, $iPos + StringLen($bBinStrName), 10)
For $i = 1 To 10
$iCont = StringMid($iValue, $i, 2)
If $iCont = "00" Then $bBinStrName &= __Binary(Chr("00"))
If $iCont <> "00" Then ExitLoop
$i += 1
Next
;
; Get the resource field size
$iValue = StringMid($bFileRead, $iPos + StringLen($bBinStrName), 400)
For $i = 1 To 400
If StringMid($iValue, $i, 6) = "000000" Then
$iValue = Int($i / 2) + 3
ExitLoop
EndIf
$i += 1
Next
$iResFieldSize = Dec(StringMid($bFileRead, $iPos - 8, 2))
If $iValue > $iResFieldSize Then $iResFieldSize = $iValue
;
If $iResFieldSize <= 1 Then Return SetError(1, 0, 0)
;
; fix $sStringText length
$sBinStrText = __Binary(StringMid(__NullEntryPoint(__NullRepeat($sStringText, $iResFieldSize, 32)), 1, $iResFieldSize - 3))
;
; Replace resource
$bFileRead = StringReplace($bFileRead, $iPos + StringLen($bBinStrName), $sBinStrText)
; If error found, return 0 and set @error to 1
If @error Then Return SetError(1, 0, 0)
;
; Update resource field size
$bHex = Hex($iResFieldSize, 2)
$bFileRead = StringReplace($bFileRead, StringInStr($bFileRead, $bBinStrName, 0, -1) - 8, $bHex, 1)
; If error found, return 0 and set @error to 1
If @error Then Return SetError(1, 0, 0)
EndIf
#cs
;
; Get new resource size
$bSearch = StringMid($bFileRead, StringInStr($bFileRead, $bHeader, 0, 1) - 4)
$bSearch = StringMid($bSearch, 1, StringInStr($bSearch, $bEndHeader, 0, -1) + 60)
$iResourceSize = Int(BinaryLen($bSearch) / 2)
; Convert to Hex, invert and set new resource size
$bHex = Hex($iResourceSize, 4)
$iResourceSize = StringRight($bHex, 2) & StringLeft($bHex, 2)
$bFileRead = StringReplace($bFileRead, StringInStr($bFileRead, $bHeader, 0, -1) - 4, $iResourceSize, 1)
;
; If error found, return 0 and set @error to 1
If @error Then Return SetError(1, 0, 0)
#ce
;
; Open file in binary write mode, save modified data and close
$hFileOpen = FileOpen($hFileName, 18)
; If error found, return 0 and set @error to 1
If $hFileOpen = -1 Then Return SetError(1, 0, 0)
FileWrite($hFileOpen, $bFileRead)
FileClose($hFileOpen)
;
; Set @error to 0 and return 1
Return SetError(0, 0, 1)
EndFunc ;==>_FileSetVersion
; #INTERNAL_USE_ONLY#============================================================================================================
; Name...........: __NullEntryPoint
; Description ...: Adding the Chr("00") enter each char.
; Syntax.........: __NullEntryPoint( $sString )
; Parameters ....: $sString - String to pass.
; Return values .: Success - Returns Chr("00") enter each char
; Failure -
; Author ........: jscript "FROM BRAZIL"
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........;
; Example .......;
; ===============================================================================================================================
Func __NullEntryPoint($sString)
;==============================================
; Local Constant/Variable Declaration Section
;==============================================
Local $iLen = StringLen($sString), $bNullString = "";Chr("00")
;
For $i = 1 To $iLen
$bNullString &= StringMid($sString, $i, 1)
If $i = $iLen Then ExitLoop
$bNullString &= Chr("00")
Next
Return $bNullString
EndFunc ;==>__NullEntryPoint
; #INTERNAL_USE_ONLY#============================================================================================================
; Name...........: __NullRepeat
; Description ...: Adding the Chr("00") at the end of the string
; Syntax.........: __NullRepeat( $sString, $iValue, $vChar = 0 )
; Parameters ....: $sString- String to pass.
; $iValue - Number of times to repeat
; $vChar - Char type, default is 00
; Return values .: Success - Returns Chr("00") enter each char
; Failure -
; Author ........: jscript "FROM BRAZIL"
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........;
; Example .......;
; ===============================================================================================================================
Func __NullRepeat($sString, $iValue, $vChar = 0)
;==============================================
; Local Constant/Variable Declaration Section
;==============================================
Local $sRepeat = ""
;
$iValue = $iValue - StringLen($sString)
If $iValue > 0 Then
For $i = 1 To $iValue
$sRepeat &= Chr($vChar)
Next
EndIf
Return $sString & $sRepeat
EndFunc ;==>__NullRepeat
; #INTERNAL_USE_ONLY#============================================================================================================
; Name...........: __Binary
; Description ...: Returns the binary representation of an expression.
; Syntax.........: __Binary( $sExpression )
; Parameters ....: $sExpression - An expression to convert into binary/byte data.
; Return values .: Success - Returns a Binary variant without: 0x
; Failure -
; Author ........: jscript "FROM BRAZIL"
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........;
; Example .......;
; ===============================================================================================================================
Func __Binary($sExpression)
Return StringTrimLeft(Binary($sExpression), 2)
EndFunc ;==>__Binary