vba sleep,read and write text file utf8

28 篇文章 2 订阅
8 篇文章 0 订阅

SLEEP function

 Option Explicit
 Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Sub sleepp(Optional time_tmp As Long = 1000)
    Sleep time_tmp ' Pause for 1 second (1000 milliseconds)
End Sub

[Open path1 For Input As #1] can only write save file in sjis

CreateObject(“ADODB.Stream”) can save file in sjis/utf8/utf8bom

logic of function replace_str_file:

  1. open file in utf8
  2. set position 0,read content to buffer
  3. reopen the file, otherwise u cannot write
  4. edit content,then write the file,contents of the file will all delete before writing
  5. if the file has bom, remove the comment from ここから to ここまで ,it will remove bom
'sjis or utf8
Function replace_str_file(ByVal path1, ByVal find As String, ByVal replace_str As String)
    Dim buffer As String, stream_tmp
    Set stream_tmp = CreateObject("ADODB.Stream")
    With stream_tmp
     
         ' ストリームの文字コードをUTF8に設定する
         .Charset = "UTF-8"
         ' ファイルのタイプ(1:バイナリ 2:テキスト)
         .Type = 2
         ' ストリームを開く
         .Open
         ' ストリームにファイルを読み込む
         .LoadFromFile (path1)
         
         .Position = 0
         ' ファイルの中身をbufferへ代入
         buffer = .ReadText
         ' ストリームを閉じる
         .Close
        .Open
        buffer = replace(buffer, find, replace_str)
        ' ストリームの保存形式をテキスト形式にする
        .WriteText buffer
        
                ' ---------- ここから ----------
    
            ' タイプをバイナリにして、先頭の3バイトをスキップ
            .Position = 0
            .Type = 1 ' タイプ変更するにはPosition = 0である必要がある
            .Position = 3
            ' 一時格納用
            Dim p_byteData() As Byte
            p_byteData = .Read
            .Close ' 一旦閉じて
            .Open ' 再度開いて
            .Write p_byteData ' ストリームに書き込む
    
            ' ---------- ここまで を追加 ----------
            
        ' ストリームに名前を付けて保存する(1は新規作成 2は上書き保存)
        .SaveToFile (path1), 2
         
         .Close
     End With
     Set stream_tmp = Nothing
End Function
'sjis
Function replace_str_file_sjis(ByVal path1, ByVal find As String, ByVal replace_str As String)
    Dim text, text_line As String
    Open path1 For Input As #1
    Do While Not EOF(1)
        Line Input #1, text_line
        If text = "" Then
            text = text_line
        Else
            text = text & vbLf & text_line
        End If
    Loop
    Close #1
    text = replace(text, find, replace_str)
    text = replace(text, ChrW(&HFEFF), "")
    Open path1 For Output As #1
    Print #1, text
    Close #1
End Function

how to save text file in utf8 and by LF

if using sakura editor,ctrl+shifl+s,set as below
在这里插入图片描述

what file format is needed in linux

  • LF+UTF8 is ok
  • if CRLF ,it will raise error if u executethe file(ep. . TT.sh)
  • if sjis,it will output 文字化け,but can be executed
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值