zoukankan      html  css  js  c++  java
  • NSIS 中替换文件中的字符

    More advanced replace text in file

    From NSIS Wiki

    Author: Afrow UK (talk, contrib)


    Contents

    [hide]

    [edit] Description

    This function allows you to replace pieces of text in a file. Instead of replacing all text found in the file, you have the extra option of replacing text after the x times of it occurring, and x times to replace the text after that occurrence.

    [edit] Usage 1

    Push hello #text to be replaced
    Push blah #replace with
    Push 3 #start replacing after 3rd occurrence
    Push 4 #replace next 4 occurrences
    Push C:\temp1.bat #file to replace in
     Call AdvReplaceInFile

    [edit] Usage 2

    Push hello #text to be replaced
    Push blah #replace with
    Push 3 #start replacing after 3rd occurrence
    Push all #replace all other occurrences
    Push C:\temp1.bat #file to replace in
     Call AdvReplaceInFile

    [edit] Usage 3

    Push hello #text to be replaced
    Push blah #replace with
    Push all #replace all occurrences
    Push all #replace all occurrences
    Push C:\temp1.bat #file to replace in
     Call AdvReplaceInFile

    [edit] The Function

    Function AdvReplaceInFile
    Exch $0 ;file to replace in
    Exch
    Exch $1 ;number to replace after
    Exch
    Exch 2
    Exch $2 ;replace and onwards
    Exch 2
    Exch 3
    Exch $3 ;replace with
    Exch 3
    Exch 4
    Exch $4 ;to replace
    Exch 4
    Push $5 ;minus count
    Push $6 ;universal
    Push $7 ;end string
    Push $8 ;left string
    Push $9 ;right string
    Push $R0 ;file1
    Push $R1 ;file2
    Push $R2 ;read
    Push $R3 ;universal
    Push $R4 ;count (onwards)
    Push $R5 ;count (after)
    Push $R6 ;temp file name
     
      GetTempFileName $R6
      FileOpen $R1 $0 r ;file to search in
      FileOpen $R0 $R6 w ;temp file
       StrLen $R3 $4
       StrCpy $R4 -1
       StrCpy $R5 -1
     
    loop_read:
     ClearErrors
     FileRead $R1 $R2 ;read line
     IfErrors exit
     
       StrCpy $5 0
       StrCpy $7 $R2
     
    loop_filter:
       IntOp $5 $5 - 1
       StrCpy $6 $7 $R3 $5 ;search
       StrCmp $6 "" file_write1
       StrCmp $6 $4 0 loop_filter
     
    StrCpy $8 $7 $5 ;left part
    IntOp $6 $5 + $R3
    IntCmp $6 0 is0 not0
    is0:
    StrCpy $9 ""
    Goto done
    not0:
    StrCpy $9 $7 "" $6 ;right part
    done:
    StrCpy $7 $8$3$9 ;re-join
     
    IntOp $R4 $R4 + 1
    StrCmp $2 all loop_filter
    StrCmp $R4 $2 0 file_write2
    IntOp $R4 $R4 - 1
     
    IntOp $R5 $R5 + 1
    StrCmp $1 all loop_filter
    StrCmp $R5 $1 0 file_write1
    IntOp $R5 $R5 - 1
    Goto file_write2
     
    file_write1:
     FileWrite $R0 $7 ;write modified line
    Goto loop_read
     
    file_write2:
     FileWrite $R0 $R2 ;write unmodified line
    Goto loop_read
     
    exit:
      FileClose $R0
      FileClose $R1
     
       SetDetailsPrint none
      Delete $0
      Rename $R6 $0
      Delete $R6
       SetDetailsPrint both
     
    Pop $R6
    Pop $R5
    Pop $R4
    Pop $R3
    Pop $R2
    Pop $R1
    Pop $R0
    Pop $9
    Pop $8
    Pop $7
    Pop $6
    Pop $5
    Pop $0
    Pop $1
    Pop $2
    Pop $3
    Pop $4
    FunctionEnd

    [edit] Versions History

    2010-10-09
    Bug fixed that doesn't change "all" occurrences
    2006-09-28
    Bug fixed that restoring $0~$4 order -- Maxi Ng
    2005-July-18
    Bug fixed that doesn't replace a text when you have only 1 line in the text file - Mark Bryan Yu
    2004-Feb-18
    Bug fixed that stopped it replacing parts of text on a line. The bug was only allowing it to replace whole lines.
    2003-May-27
    Re-written.

    [edit] Credits

    Written by me for modifying batch files.

    -Stu (Afrow UK)

  • 相关阅读:
    VS2005入门.Net2.0系列视频教程181级打包下载
    Asp.Net2.0视频教程 之 WebPart概述 [视频]
    MemberShip,角色,WebPart在web.config文件中的参数简述
    vs2005入门 .Net2.0视频教程 之 SQL查询语法基础 [视频]
    关于进期教程发布事宜通告
    从我博客的访客地域分布分析看我国学.net的人
    《Vs2005网站编程》目录雏形
    Asp.Net2.0视频教程 之 WebPart 一 [视频]
    vs2005入门 .Net2.0视频教程 之 浅尝存储过程[视频]
    vs2005视频教程 之 TreeView高级使用 [视频]
  • 原文地址:https://www.cnblogs.com/zdxster/p/2015496.html
Copyright © 2011-2022 走看看