返回列表 发帖

[问题求助] [已解决]vbs如何实现在txt文本行与行之间插入/添加空行或删除所有空行

本帖最后由 pcl_test 于 2016-7-22 22:10 编辑

放某文件夹后执行可以对这个文件夹内的TXT进行操作。
运行后出现两个选项,第一个添加空行,第二个删除空行,可以任意选择功能执行。

空行加在什么地方?
你需要VBS还是BAT呢?

TOP

VBS的,TXT内容没有空行,所以行之间隔都要添加空行,第二个是删除所有空行

TOP

本帖最后由 apang 于 2014-9-11 19:53 编辑
Dim strPrompt, strInput
strPrompt = "1.插入空行" & vbLf & "2.删除空行" & vbLf & vbLf & "输入1或2"
Do while true
    strInput = InputBox(strPrompt, "", "1")
    If IsEmpty(strInput) Then
        WScript.Quit
    ElseIf strInput = "1" or strInput = "2" Then
        Exit Do
    End If
Loop
Dim fso, file, f, txt
Set fso = CreateObject("Scripting.FileSystemObject")
For Each file In fso.GetFolder(".").Files
    If LCase(Right(file, 4)) = ".txt" Then
        Set f = fso.OpenTextFile(file)
        txt = f.ReadAll
        f.Close
        Set f = Nothing
        If strInput = "1" Then
            txt = RegEx(txt, "\r\n", "$&$&")
        Else
            txt = RegEx(txt & vbCrLf, "(\s*\n)+", vbCrLf)
            If Left(txt, 2) = vbCrLf Then txt = Mid(txt, 3)
        End If
        fso.OpenTextFile(file, 2).Write txt
    End If
Next
MsgBox "OK"
Function RegEx(s, pattern, s1)
    Dim re
    Set re = New RegExp
    re.Pattern = pattern
    re.Global = true
    RegEx = re.Replace(s, s1)
End FunctionCOPY
1

评分人数

    • 老卡机: 谢谢,很强大,BAT的好慢,这个快多了技术 + 1

TOP

学习了,正好要用

TOP

返回列表