Skip to content
⌘ NSIS Forum Archive

Convert TXT file in INI Format

4 posts

dummy9999#

Convert TXT file in INI Format

Hi all,

I'm new here in this Forum and I hope you can help me with my Problem.
I found many Solutions for other programming Problems with NSIS in your Forum, but
for my new Problem I cant't find anything or I don't know what is the richt search string.

I discripe here my new Problem.

I will convert a TXT file with the following Content

Name=Adobe Flash Player 15 ActiveX
PackageName=install_flash_player_15_active_x.msi
Transforms=Adobe_Flash_Player_15.mst
Version=15.0.0.167


Name=Adobe Flash Player 15 Plugin
PackageName=install_flash_player_15_plugin.msi
Transforms=Adobe_Flash_Player_15_Plugin.mst
Version=15.0.0.167


to this INI Format:

[Adobe Flash Player 15 ActiveX]
PackageName=install_flash_player_15_active_x.msi
Transforms=Adobe_Flash_Player_15.mst
Version=15.0.0.167


[Adobe Flash Player 15 Plugin]
PackageName=install_flash_player_15_plugin.msi
Transforms=Adobe_Flash_Player_15_Plugin.mst
Version=15.0.0.167


With the following code I can Change the first part of the INI Section:
 	
# PackageName= search
${textreplace::FindInFile} "C:\temp\1.txt" "PackageName=" "/S=1" $0
# Rename PackageName= in PackageNome=
${textreplace::ReplaceInFile} "C:\temp\1.txt" "C:\Temp\output.txt" "PackageName=" "PackageNome=" "/S=1 /C=1 /AO=1" $0
# Name= search
${textreplace::FindInFile} "C:\temp\output.txt" "Name=" "/S=1" $0
LogText 'var = $0'
# Rename Name= in [ ${textreplace::ReplaceInFile} "C:\temp\output.txt" "C:\Temp\output.txt" "Name=" "[" "/S=1 /C=1 /AO=1" $0
# Search PackageNome=
${textreplace::FindInFile} "C:\temp\output.txt" "PackageNome=" "/S=1" $0
# Rename PackageNome= in PackageName=
${textreplace::ReplaceInFile} "C:\temp\output.txt" "C:\Temp\output.txt" "PackageNome=" "PackageName=" "/S=1 /C=1 /AO=1" $0
${textreplace::Unload}
This code changed the TXT file as follows:

[Adobe Flash Player 15 ActiveX
PackageName=install_flash_player_15_active_x.msi
Transforms=Adobe_Flash_Player_15.mst
Version=15.0.0.167


[Adobe Flash Player 15 Plugin
PackageName=install_flash_player_15_plugin.msi
Transforms=Adobe_Flash_Player_15_Plugin.mst
Version=15.0.0.167


My Problem is, how can I set at the end of the line "]" for end of the INI section?

I hope anybody will have an idea.
jpderuiter#
Replace your code with:
${LineFind} "C:\temp\1.txt" "C:\temp\output.txt" "1:-1" "ConvertTxtLineToINI"
and add the following function:
Function ConvertTxtLineToINI
StrCpy $0 ""
${WordFindS} $R9 "PackageName=" "E+1" $R0
IfErrors 0 end ;If Line contains "PackageName=" skip
${WordReplaceS} $R9 "Name=" "[" "+1" $R0 ;Replace "Name=" with "[", and place string in $R0
IfErrors end ;If Line does not contain "Name=" skip
StrCpy $R9 "$R0]" ;Add "]" at the end, and place string back in $R9
end:
Push $0
FunctionEnd
You must include the header files "TextFunc.nsh" and "WordFunc.nsh" for this code to work (both are standard available in NSIS).
For details about the functions: LineFindS and WordFindS
PLEASE NOTE: I think this code will work, but I am not able to test it, because I do not have NSIS installed on this machine.
dummy9999#
Thanks for your help. 🙂

I tested your code and I found one Problem.
At the end of the line in the text file is a line break,
so the script will Change the line as follows:

[Adobe Flash Player 15 Plugin
]


How can I remove the line break?

At the end I have Changed the codeline to add the line break again.

StrCpy $R9 "$R0]$\r$\n" ;Add "] + line break" at the end, and place string back in $R9