替换相同项目的顺序

时间:2015-04-18 18:48:32

标签: batch-file vbscript

我正在编写一个脚本,其中用户列出了一个列表并在某些文本文件中寻找相同的内容并以.txt返回结果只会遇到以下问题:

列表在界面上按如下方式组织:

Item1
item2
Item3

并在文本文件中输出如下:

item1|item2|item3

第一个问题:

如果文件从头开始有一个空行:

item1

item2

item3

输出带有“|”更多,因为研究问题:

item1||item2||item3

第二个问题:

如果开头/结尾有一个空行:

 
item1
item2
item3
 

搜索时输出也有问题:

|item1|item2|item3|

注意:在开头,中间和结尾之间可能有几个空行。

注意2:脚本进入.hta,如果需要,代码.hta将在帖子结束。

注3:解决方案可以是批处理,外部程序甚至是vbs。

Option Explicit
    Window.resizeTo 373,610
Const csFSpec = "List.ini"
Dim goFS : Set goFS = CreateObject("Scripting.FileSystemObject")

Sub Window_OnLoad()
  If goFS.FileExists(csFSpec) Then
     document.all.DataArea.value = goFS.OpenTextFile(csFSpec).ReadAll()
     document.all.DataArea.value = Replace(document.all.DataArea.value,"|", vbcrlf)
  Else

     self.close
  End If

  If document.all.DataArea.value =vbcrlf Then

document.all.DataArea.value =""
Else

  End If
End Sub

Sub SaveFile()
If document.all.DataArea.value = "" Then

document.all.DataArea.value =vbcrlf
  goFS.CreateTextFile(csFSpec).Write document.all.DataArea.value

     self.close
Else
document.all.DataArea.value = Replace(document.all.DataArea.value, "\", "_")
document.all.DataArea.value = Replace(document.all.DataArea.value, "/", "_")
document.all.DataArea.value = Replace(document.all.DataArea.value, ":", "_")
document.all.DataArea.value = Replace(document.all.DataArea.value, "*", "_")
document.all.DataArea.value = Replace(document.all.DataArea.value, "?", "_")
document.all.DataArea.value = Replace(document.all.DataArea.value, """", "_")
document.all.DataArea.value = Replace(document.all.DataArea.value, "<", "_")
document.all.DataArea.value = Replace(document.all.DataArea.value, ">", "_")
document.all.DataArea.value = Replace(document.all.DataArea.value, "|", "_")
document.all.DataArea.value = Replace(document.all.DataArea.value, "&", "_")
document.all.DataArea.value = Replace(document.all.DataArea.value, "!", "_")
document.all.DataArea.value = Replace(document.all.DataArea.value,vbcrlf,"|")
  goFS.CreateTextFile(csFSpec).Write document.all.DataArea.value
     self.close
  End if
End Sub

Sub QuitEdit()

     self.close
End Sub

Sub Redefine()

document.all.DataArea.value ="Item1" & vbcrlf & "Item2" & vbcrlf & "Item3"

End Sub

Sub Clean()

document.all.DataArea.value = ""

End Sub

完整代码:

<html>
 <head>
  <title>List</title>
 <HTA:Application
      Border= "thin"
      Application="/md/input"
      Scoll="NO"
      Singleinstance="Yes"
      SysMenu=NO
      Icon="%Windir%\System32\wscript.exe">
      ShowInTaskbar="Yes"
      Caption="Yes">
  <script type="text/vbscript">
Option Explicit
    Window.resizeTo 373,610
Const csFSpec = "List.ini"
Dim goFS : Set goFS = CreateObject("Scripting.FileSystemObject")

Sub Window_OnLoad()
  If goFS.FileExists(csFSpec) Then
     document.all.DataArea.value = goFS.OpenTextFile(csFSpec).ReadAll()
     document.all.DataArea.value = Replace(document.all.DataArea.value,"|", vbcrlf)
  Else

     self.close
  End If

  If document.all.DataArea.value =vbcrlf Then

document.all.DataArea.value =""
Else

  End If
End Sub

Sub SaveFile()
If document.all.DataArea.value = "" Then

document.all.DataArea.value =vbcrlf
  goFS.CreateTextFile(csFSpec).Write document.all.DataArea.value

     self.close
Else
document.all.DataArea.value = Replace(document.all.DataArea.value, "\", "_")
document.all.DataArea.value = Replace(document.all.DataArea.value, "/", "_")
document.all.DataArea.value = Replace(document.all.DataArea.value, ":", "_")
document.all.DataArea.value = Replace(document.all.DataArea.value, "*", "_")
document.all.DataArea.value = Replace(document.all.DataArea.value, "?", "_")
document.all.DataArea.value = Replace(document.all.DataArea.value, """", "_")
document.all.DataArea.value = Replace(document.all.DataArea.value, "<", "_")
document.all.DataArea.value = Replace(document.all.DataArea.value, ">", "_")
document.all.DataArea.value = Replace(document.all.DataArea.value, "|", "_")
document.all.DataArea.value = Replace(document.all.DataArea.value, "&", "_")
document.all.DataArea.value = Replace(document.all.DataArea.value, "!", "_")
document.all.DataArea.value = Replace(document.all.DataArea.value,vbcrlf,"|")
  goFS.CreateTextFile(csFSpec).Write document.all.DataArea.value
     self.close
  End if
End Sub

Sub QuitEdit()

     self.close
End Sub

Sub Redefine()

document.all.DataArea.value ="Item1" & vbcrlf & "Item2" & vbcrlf & "Item3"

End Sub

Sub Clean()

document.all.DataArea.value = ""

End Sub

  </script>
 </head>
<body style="overflow:hidden" bgColor="#000080"></body>
<caption></caption><hr></hr>
<font color="#FFFFFF" Times New Roman" size="13"><center><B>List</b></center></font>
<caption></caption><hr></hr>
     <TR><td>
<input style="background-color:#F0F0F0; color: #000000; border: 2px transparent; float: left;" type="BUTTON" value="Clean" class="btn" id="btna" onclick="Clean" onmouseover="btna.style.background = '#808080'" onmouseout="btna.style.background = '#F0F0F0'">
<input style="background-color:#F0F0F0; color: #000000; border: 2px transparent; float: right;" type="BUTTON" value="Redefine" class="btn" id="btnb" onclick="Redefine" onmouseover="btnb.style.background = '#808080'" onmouseout="btnb.style.background = '#F0F0F0'">
   </TR></td>
   <Table border="3"  style="width:100%; text-align: center" BORDERCOLOR=#F0F0F0>
   <TR><td>
  <form>
<textarea name="DataArea" rows="23" cols=37></textarea>  </Table>
   <TR><td>
   <p>
 <div align="right"><input style="background-color:#F0F0F0; color: #000000; border: 2px transparent" type="BUTTON" value=" OK " class="btn" id="btnc" onclick="SaveFile" onmouseover="btnc.style.background = '#808080'" onmouseout="btnc.style.background = '#F0F0F0'">
<input style="background-color:#F0F0F0; color: #000000; border: 2px transparent" type="BUTTON" value="Cancel" class="btn" id="btnd" onclick="QuitEdit" onmouseover="btnd.style.background = '#808080'" onmouseout="btnd.style.background = '#F0F0F0'">
   </div>
  </td></TR>
  </form>

 </body>
</html>

1 个答案:

答案 0 :(得分:2)

https://skydrive.live.com/redir?resid=E2F0CE17A268A4FA!121过滤器,在一个文件中使用一组19个示例程序来处理文件。它有两个示例程序,1个用于清除顶部和底部的空白行,另一个用于所有空白行。 Filter有一个批处理文件,可以轻松调用vbs脚本。

<强> TRIMLINE

filter trimline {top|end|both}
filter tl {t|e|b}

修剪文件顶部和底部的空白行。

top - removes blank lines from top of file.
end - removes blank lines from end of file.
both - removes blank lines from top and end of file.

示例

修复win.ini,而不是需要修复,并将其发送到屏幕

filter trimline both < "%systemroot%\win.ini"

<强>脚本

Sub TrimLines
Set Arg = WScript.Arguments
set WshShell = createObject("Wscript.Shell")
Set Inp = WScript.Stdin
Set Outp = Wscript.Stdout

    If LCase(Arg(1)) = "top" or LCase(Arg(1)) = "t" then 
        Flags = 0
        Do Until Inp.AtEndOfStream
            Line=Inp.readline
            If Line <> "" then Flags = 1
            If Flags = 1 then outp.writeline Line
        Loop
    ElseIf LCase(Arg(1)) = "e" or LCase(Arg(1)) = "end" then
        PendingLines = ""
        Do Until Inp.AtEndOfStream
            Line=Inp.readline
            If Line <> "" then 
                outp.writeline PendingLines & Line
                PendingLines = ""
            Else
                PendingLines=PendingLines & vbcrlf
            End If
        Loop
    ElseIf LCase(Arg(1)) = "b" or LCase(Arg(1)) = "both" then
        Flags = 0
        Do Until Inp.AtEndOfStream
            Line=Inp.readline
            If Line <> "" then Flags = 1
            If Flags = 1 then 
                If Line <> "" then 
                    outp.writeline PendingLines & Line
                    PendingLines = ""
                Else
                    PendingLines=PendingLines & vbcrlf
                End If
            End If
        Loop
    End If
End Sub

<强> BlankLine

filter blankline {e|a}

修剪文件中的所有空行和/或空行。

e - removes blank lines from a file.
a - removes blank lines and lines only composed of space or tab from a file.

实施例

修复win.ini,而不是需要修复,并将其发送到屏幕

filter blankline a < "%systemroot%\win.ini"

<强>脚本

Sub BlankLine
Set Arg = WScript.Arguments
set WshShell = createObject("Wscript.Shell")
Set Inp = WScript.Stdin
Set Outp = Wscript.Stdout
    Set RegEx = New RegExp
    RegEx.Pattern = "^\s+$"
        If LCase(Arg(1)) = "e" then
            Do Until Inp.AtEndOfStream
                Line=Inp.ReadLine
                If Len(Line) <> 0 Then
                    OutP.WriteLine Line
                End If
            Loop
        ElseIf Lcase(Arg(1)) = "a" then
            Do Until Inp.AtEndOfStream
                Line=Inp.ReadLine
                If Len(Line) <> 0 Then
                    If RegEx.Test(Line) = False then
                        OutP.WriteLine Line
                    End If
                End If
            Loop
        End If
End Sub

您也可以使用VBS替换命令。

A = Replace("blah||blah", "||", "|")