替换相同项目的序列
Replace the sequence of the same item
我正在制作一个脚本,用户在其中列出一个列表,并在一些文本文件中寻找相同的列表,returns.txt 中的结果只遇到以下问题:
列表在界面上的组织方式如下:
Item1
item2
Item3
在文本文件中输出如下:
item1|item2|item3
第一个问题:
如果文件开头有一个空行:
item1
item2
item3
输出带有“|”更因为研究问题:
item1||item2||item3
第二题:
如果start/end有空行:
item1
item2
item3
搜索时输出也有问题:
|item1|item2|item3|
注意:开头、中间和结尾之间可能会有几行空行。
注2:脚本放在.hta里面,如有必要,代码.hta会放在post.
的末尾
注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>
来自https://skydrive.live.com/redir?resid=E2F0CE17A268A4FA!121 处的过滤器,一个文件中包含一组 19 个示例程序,用于处理文件。它有两个示例程序,一个用于删除顶部和底部的空行,另一个用于所有空行。 Filter 有一个批处理文件,可以轻松调用 vbs 脚本。
修剪线
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
空行
filter blankline {e|a}
删除文件中所有空 and/or 空行。
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", "||", "|")
我正在制作一个脚本,用户在其中列出一个列表,并在一些文本文件中寻找相同的列表,returns.txt 中的结果只遇到以下问题:
列表在界面上的组织方式如下:
Item1
item2
Item3
在文本文件中输出如下:
item1|item2|item3
第一个问题:
如果文件开头有一个空行:
item1
item2
item3
输出带有“|”更因为研究问题:
item1||item2||item3
第二题:
如果start/end有空行:
item1
item2
item3
搜索时输出也有问题:
|item1|item2|item3|
注意:开头、中间和结尾之间可能会有几行空行。
注2:脚本放在.hta里面,如有必要,代码.hta会放在post.
的末尾注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>
来自https://skydrive.live.com/redir?resid=E2F0CE17A268A4FA!121 处的过滤器,一个文件中包含一组 19 个示例程序,用于处理文件。它有两个示例程序,一个用于删除顶部和底部的空行,另一个用于所有空行。 Filter 有一个批处理文件,可以轻松调用 vbs 脚本。
修剪线
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
空行
filter blankline {e|a}
删除文件中所有空 and/or 空行。
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", "||", "|")