2015-04-18 21 views
1

我正在做一個腳本,用戶進行列表,並同樣是在一些文本文件要求,並返回一個.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

我沒有看到問題。 – dbenham

回答

2

從過濾器在https://skydrive.live.com/redir?resid=E2F0CE17A268A4FA!121處理文件一組在一個文件19個示例程序。它有兩個示例程序,一個用於刪除空白行的頂部和底部,另一個用於所有空白行。過濾器有一個批處理文件,可以方便地調用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. 

修復取勝。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", "||", "|")