Vbscript ASP/VB经典清除文件中的一行

Vbscript ASP/VB经典清除文件中的一行,vbscript,asp-classic,Vbscript,Asp Classic,我有一个文件如下所示: Alpha,25,SomeBrand,Info Gamma,2039,Crisps,Foobar Epic,240,Win,Post Alpha,25,SomeBrand,Info Epic,240,Win,Post 我想清除这个文件中的某一行,比如第2行,这样看起来像这样: Alpha,25,SomeBrand,Info Gamma,2039,Crisps,Foobar Epic,240,Win,Post Alpha,25,SomeBrand,Info Epi

我有一个文件如下所示:

Alpha,25,SomeBrand,Info
Gamma,2039,Crisps,Foobar
Epic,240,Win,Post
Alpha,25,SomeBrand,Info

Epic,240,Win,Post
我想清除这个文件中的某一行,比如第2行,这样看起来像这样:

Alpha,25,SomeBrand,Info
Gamma,2039,Crisps,Foobar
Epic,240,Win,Post
Alpha,25,SomeBrand,Info

Epic,240,Win,Post

我怎样才能有效地做到这一点?这个文件有18000多行,我试着读入完整的文件并写回,但速度太慢了。

由于无法使用文件指针移动技巧在VBScript中更改“磁盘上”的文件,您必须重新写入它。您是否测试了使用.ReadAll和.Write是否足够快?如果是,我们可以讨论一种有效地进行修改的方法。第一个问题:您是要删除有问题的行,还是应该将其替换为空行

下一步:

此VBScript代码:

  Dim goFS      : Set goFS     = CreateObject( "Scripting.FileSystemObject" )

  Dim sDir      : sDir         = "..\testdata\editlargefile"
  Dim sSrcFSpec : sSrcFSpec    = goFS.BuildPath( sDir, "lines.txt" )
  Dim nLine     : nLine        = 5
  Dim nSize     : nSize        = goFS.GetFile( sSrcFSpec ).Size
  WScript.Echo nSize, "bytes in", sSrcFSpec

  ReDim aHead( nLine - 2 )
  Dim oTS, nIdx, sTail

  Set oTS = goFS.OpenTextFile( sSrcFSpec )
  For nidx = 0 To UBound( aHead )
      aHead( nIdx ) = oTS.ReadLine()
  Next
  oTS.ReadLine
  sTail = oTS.ReadAll()
  oTS.Close

  WScript.Echo Left( Join( aHead, vbCrLf ) & vbCrLf & vbCrLf & Left( sTail, 100 ), 150 )

  Set oTS = goFS.CreateTextFile( sSrcFSpec, True )
  oTS.Write Join( aHead, vbCrLf )
  oTS.Write vbCrLf & vbCrLf
  oTS.Write sTail
  oTS.Close
输出:

20888896 bytes in ..\testdata\editlargefile\lines.txt
This is line 1
This is line 2
This is line 3
This is line 4

This is line 6
This is line 7
This is line 8
This is line 9
This is line 10
Thi
=====================================================
xplfs.vbs: Erfolgreich beendet. (0) [11.42188 secs]
演示了我能想到的最快的VBScript方式。伪代码 对于一种能够做文件指针技巧的语言来说

Open the file in read+write mode
Loop over the head lines to keep
If Delete
   Skip line to delete
   Reset write file pointer to end of previous line
   Block write to file pointer till end
Else
   Fill line to delete with spaces
End
Close the file
你打算用什么语言

我不知道你的文件大小,但我已经写了一个脚本asp在2.5秒内执行。 文本文件大小为3500万字节,有35000行

在这里:


你清除行的标准是什么?@Jennifer S:除了行号之外没有别的。它必须被替换为空的行号,即使是文件指针技巧所需的所有空格。正如我说的:你不能在VBScript中执行文件指针技巧。您是否测试了.ReadAll+.Write是否足够快?如果没有,你是否考虑使用另一种语言C++,C,perl,…对于这个子任务?是的,我必须考虑另一种语言,因为读和写不够快,10秒。EKKHADH.HONER:没有ASP解决方案对我来说足够快,所以我写了一些东西在C++中。燃烧得很快。