UNIX to DOS text file format conversion script
From NSIS Wiki
Jump to navigationJump to search
It can sometimes be useful to convert DOS style line endings of text files, such as the README or CHANGELOG, because some windows apps such as notepad can't display files UNIX style line endings properly. The function below converts line endings to DOS style directly from within NSIS without the need for any external plugin or external converter.
Function unix2dos ; strips all CRs ; and then converts all LFs into CRLFs ; (this is roughly equivalent to "cat file | dos2unix | unix2dos") ; ; usage: ; Push "infile" ; Push "outfile" ; Call unix2dos ; ; beware that this function destroys $0 $1 $2 ClearErrors Pop $2 FileOpen $1 $2 w Pop $2 FileOpen $0 $2 r Push $2 ; save name for deleting IfErrors unix2dos_done ; $0 = file input (opened for reading) ; $1 = file output (opened for writing) unix2dos_loop: ; read a byte (stored in $2) FileReadByte $0 $2 IfErrors unix2dos_done ; EOL ; skip CR StrCmp $2 13 unix2dos_loop ; if LF write an extra CR StrCmp $2 10 unix2dos_cr unix2dos_write unix2dos_cr: FileWriteByte $1 13 unix2dos_write: ; write byte FileWriteByte $1 $2 ; read next byte Goto unix2dos_loop unix2dos_done: ; close files FileClose $0 FileClose $1 ; delete original Pop $0 Delete $0 FunctionEnd
Example
In the following example, the installer extracts the original README, makes a copy of it named README.txt with CRLF line endings, and then deletes the original README.
Section ... SetOutPath $INSTDIR File README Push "$INSTDIR\README" Push "$INSTDIR\README.txt" Call unix2dos ... CreateShortCut "$SMPROGRAMS\Your Program\Readme.lnk" "$INSTDIR\README.txt" ... SectionEnd