vbs删除文本文件的行的函数
- 格式:doc
- 大小:15.00 KB
- 文档页数:2
三种简单删除空白行的方法1.引言概述部分的内容可写作如下:1.1 概述在文本编辑和数据处理的过程中,我们常常会遇到需要删除空白行的情况。
空白行可能是文本文件中多余的空行,或者是数据集中的无效记录。
无论是为了美观地呈现文本内容,还是为了提高数据处理的效率,删除空白行都是一个重要的操作。
在本文中,我们将介绍三种简单且常用的方法来删除空白行。
这些方法分别是使用编程语言处理空白行、使用文本编辑器进行空白行删除,以及其他一些基于特定需求的方法。
首先,我们将详细介绍使用编程语言处理空白行的方法。
通过编写简单的脚本或程序,我们可以自动识别并删除文本文件或数据集中的空白行。
这种方法适用于对大量文本或数据进行操作的场景,可以提高处理效率并减少手动操作的工作量。
其次,我们将介绍使用文本编辑器进行空白行删除的方法。
大多数文本编辑器都提供了删除空白行的功能,我们只需利用相应的快捷键或命令,即可轻松完成这一操作。
这种方法适用于对单个文本文件或少量数据进行操作的情况,操作简便快捷。
最后,我们将探讨其他一些基于特定需求的方法。
这些方法可能不直接删除空白行,而是针对特定的应用场景进行处理。
例如,在某些情况下,我们可能只需要删除多余的空行,而保留一定数量的空行用于美观排版。
或者,在数据处理过程中,我们可能需要保留某些特殊的空白行作为分隔符或标识。
因此,根据实际需求,选择合适的方法来处理空白行很重要。
通过本文的介绍和讨论,希望读者能够了解并掌握三种简单删除空白行的方法,并能根据实际需求选择合适的方法进行操作。
删除空白行可以提高文本编辑和数据处理的效率,使得内容更加整洁和易读。
同时,我们也必须注意在删除空白行时保留必要的空行,以便于正确表达内容或满足特定的需求。
文章结构部分的内容如下:1.2 文章结构在本文中,我们将讨论三种简单删除空白行的方法。
文章将按照以下结构进行展开:1. 引言:在引言部分,我们将简要介绍本文的主题——删除空白行。
自动清理固定目录的文件及文件夹的VBS代码说明:(1)可根据需要修改保留文件及目录的规则,再用任务计划定时执行delete3filex.vbs文件。
(2)注意修改文件前面目录定义:deletepath="D:\Public\" '----------[删除目录]-------根据实际情况更改ruletxtpath="D:\Share\ruletxt.txt" '----------[规则文件路径]------根据实际情况更改deltimetype="d" '--[判断类型]---- yyyy年q 季m 月y 一年的日数d 日w 一周的日数ww周h 时n 分钟s 秒deltimenum=3 '--[保留时间数]--------根据实际情况更改,当类型为天,则为3天。
logtxtname="D:\Share\del3daylog.txt" '[删除日志记录文件]--------根据实际情况更改operatelogtxt="D:\Share\operatelog.txt" '[运行日志记录文件]--------根据实际情况更改1、规则文件:ruletxt.txt内容(以下内容直接存于ruletxt.txt文件):会议资料,工作流程,端口,操作说明-XP版.doc,文件管理.pptAdministrator,jz.zhudll,sys,steup,HH,测试,不Aministrator,everyone,Everyone--------文件说明,以下内容请勿删除---------文件规则说明:第一行表示不删除的文件及文件夹的关键字第二行表示不删除的文件及文件夹的所有者2、规则文件:delete3filex.vbs代码内容(以下内容直接存于delete3filex.vbs文件):'vervion:V0910-1 //1.加入删除日志记录功能;2.删除目录存在判断;3.日志写入错误捕获'//20120831增加公用常量deletepath[删除目录] ruletxtpath[规则文件路径]'//20120828 /增加通过文件夹,文件的所有者来控制删除动作;'20120828/1.增加删除规则,规则由记事本文件ruletxt.tx定义,对文件夹和文件同时适用[也可考虑分别用不同的文件定义规则;'修改内内容:1.改写删除目录函数与删除文件函数2.说明:n表示分钟,h表示小时;'Last date 2012/08/15dim deletepath,ruletxtpath,deltimetype,deltimenum,logtxtname,operatelogtxtdeletepath="D:\Public\" '----------[删除目录]-------根据实际情况更改ruletxtpath="D:\Share\ruletxt.txt" '----------[规则文件路径]------根据实际情况更改deltimetype="d" '--[判断类型]---- yyyy年q 季m 月y 一年的日数d 日w 一周的日数ww周h 时n 分钟s 秒deltimenum=3 '--[保留时间数]--------根据实际情况更改logtxtname="D:\Share\del3daylog.txt" '[删除日志记录文件]--------根据实际情况更改operatelogtxt="D:\Share\operatelog.txt" '[运行日志记录文件]--------根据实际情况更改'-------------------------运行日志开始部分------------------on error resume next '打开错误捕获Set operatelogfso = CreateObject("Scripting.FileSystemObject")Set operateobjStream = operatelogfso.OpenTextFile(operatelogtxt,8,true)'打开文件,不存在则创建if Err.number<>0 then '错误说明'WScript.Echo "运行日志文件不存在!"Wscript.quitErr.clear '清除err对象的内容end ifoperateobjStream.WriteLine("---------------------------------------------------------") operateobjStream.WriteLine(now()&" 程序开始执行!")'------------删目录---------------Function DeleteFolder()operateobjStream.WriteLine(now()&" 判断目录删除开始!")Dim objFSO,objFolder,strPath,targetFSO,subFSO,DirPathDirPath = deletepath'DirPath = "C:\Temp\Temp\"Set objFSO = CreateObject("Scripting.FileSystemObject")If objFSO.FolderExists(DirPath) ThenoperateobjStream.WriteLine(now()&" 删除目录存在!")'WScript.Echo "删除目录存在!"elseoperateobjStream.WriteLine(now()&" 删除目录不存在,程序执行结束!") 'WScript.Echo "删除不目录存在!"Wscript.quitEnd IfSet targetFSO = objFSO.GetFolder(DirPath)Set subFSO = targetFSO.SubFolders''WScript.Echo "2!"For Each subFolder in subFSOoperateobjStream.WriteLine(now()&" 取得所有目录名!")'If DateDiff("n",subFolder.DateCreated,now)>1 ThenIf DateDiff(deltimetype,subFolder.DateCreated,now)>deltimenum Then operateobjStream.WriteLine(now()&" 对大于某时间的目录进行删除目录条件判断开始!")'-----读取规则文件dim objtxt,objWorkbook,txtbookline,txtbookline1,delfieleID 'delfieleID表示删除标志delfieleID="true" '默认是允许删除Set objtxt=CreateObject("Scripting.FileSystemObject")'Set objWorkbook=objtxt.OpenTextFile("D:\Share\Public\ruletxt.txt",1,true)'Set objWorkbook=objtxt.OpenTextFile("E:\ruletxt.txt",1,true)Set objWorkbook=objtxt.OpenTextFile(ruletxtpath,1,true)'txtbookline=Trim(Split(Trim(objWorkbook.ReadLIne),"|")) 'Split返回一个一维数组ifobjWorkbook.AtEndOfStream<>true thentxtbookline=objWorkbook.ReadLIneoperateobjStream.WriteLine(now()&" 读取关键字规则成功!规则是:["&txtbookline&"]")end iftxtbookline1=Split(txtbookline,",")'WScript.Echotxtbookline'WScript.EchouBound(txtbookline1)operateobjStream.WriteLine(now()&" 关键字规则判断开始!")fori=0 to uBound(txtbookline1)ifInStr(1,,txtbookline1(i))>0 thendelfieleID="false"Exit forelseend ifnextobjWorkbook.CloseifdelfieleID="true" thenoperateobjStream.WriteLine(now()&" ["&&"] 符合关键字删除规则!")elseoperateobjStream.WriteLine(now()&" ["&&"] 不符合关键字删除规则!")end ifoperateobjStream.WriteLine(now()&" 关键字规则判断结束!")'-----规则遍历结束,满足条件已删除operateobjStream.WriteLine(now()&" 目录所有者规则判断开始!")'--------------------------判断文件夹所有者开始-------------'dim objtxt,objWorkbook,txtbookline,txtbookline1,delfieleID 'delfieleID表示删除标志'delfieleID="true" '默认是允许删除Set objtxt=CreateObject("Scripting.FileSystemObject")'Set objWorkbook=objtxt.OpenTextFile("E:\ruletxt.txt",1,true)Set objWorkbook=objtxt.OpenTextFile(ruletxtpath,1,true)'txtbookline=Trim(Split(Trim(objWorkbook.ReadLIne),"|")) 'Split返回一个一维数组objWorkbook.SkipLine() '跳过第一行ifobjWorkbook.AtEndOfStream<>true thentxtbookline=objWorkbook.ReadLIneoperateobjStream.WriteLine(now()&" 读取所有者规则成功!规则是!["&txtbookline&"]") end iftxtbookline1=Split(txtbookline,",")'WScript.Echotxtbookline'WScript.EchouBound(txtbookline1)'取得将要删除文件夹的所有者dimstrFileName,FileOwnerstrFileName = DirPath& '完整路径名Set objWMIService = GetObject("winmgmts:")Set objFileSecuritySettings = _objWMIService.Get("Win32_LogicalFileSecuritySetting='" &strFileName& "'")intRetVal = objFileSecuritySettings.GetSecurityDescriptor(objSD)If intRetVal = 0 Then''WScript.Echo "Owner: " &objSD.Owner.Domain& "\" &FileOwner=Else''WScript.Echo "Couldn't retrieve security descriptor."FileOwner=""End Iffori=0 to uBound(txtbookline1)ifInStr(1,FileOwner,txtbookline1(i))>0 thendelfieleID="false"Exit forend ifnextobjWorkbook.CloseifdelfieleID="true" thenoperateobjStream.WriteLine(now()&" ["&&"] 符合目录所有者删除规则!")elseoperateobjStream.WriteLine(now()&" ["&&"] 不符合目录所有者删除规则!")end ifoperateobjStream.WriteLine(now()&" 目录所有者规则判断结束!")'--------------------------判断文件夹所有者结束,特定所有者不删除-----------ifdelfieleID="true" thenstrPath = DirPath&'WScript.Echo '------日志记录开始-----'三个参数的分别为:以只读模式打开文件、以只写方式打开文件、打开文件并在文件末尾进行写操作'ConstintForReading = 1,intForWriting = 2,intForAppending = 8on error resume next '打开错误捕获dimstrText,intForAppendingintForAppending=8strText="已删除目录:"&now()&" "&FileOwner&" "&Set logfso = CreateObject("Scripting.FileSystemObject")Set objStream = logfso.OpenTextFile(logtxtname,intForAppending,true)'打开文件,不存在则创建objStream.WriteLine(strText)objStream.CloseSet logfso = nothingSet objStream = nothingif Err.number<>0 then '错误说明'WScript.Echo "写入日志失败!"operateobjStream.WriteLine(now()&" ["&&"] 目录删除日志写入失败,停止程序运行!")Wscript.quitErr.clear '清除err对象的内容elseoperateobjStream.WriteLine(now()&" ["&&"] 目录删除日志写入成功!")'WScript.Echo "日志写入成功!"end if'------日志记录结束-----objFSO.DeleteFolder(strPath)operateobjStream.WriteLine(now()&" ["&&"] 目录删除成功!")'WScript.Echo "文件夹删除成功!"elseoperateobjStream.WriteLine(now()&" ["&&"] 目录不能删除!") 'WScript.Echo &" 文件夹不能删除!"end if'strPath = DirPath&''WScript.EchostrPath''WScript.Echo "4!"'objFSO.DeleteFolder(strPath)''WScript.Echo "5!"End IfNextoperateobjStream.WriteLine(now()&" 对大于某时间的目录进行删除目录条件判断结束!")End Function'------------删文件------------------Function DeleteFile()operateobjStream.WriteLine(now()&" 判断文件删除开始!")Dim objFSO,objFolder,strPath,targetFSO,subFSO,DirPathDirPath = deletepath'DirPath = "C:\Temp\Temp\"Set objFSO = CreateObject("Scripting.FileSystemObject")If objFSO.FolderExists(DirPath) ThenoperateobjStream.WriteLine(now()&" 删除目录存在!")'WScript.Echo "删除目录存在!"elseoperateobjStream.WriteLine(now()&" 删除目录不存在,程序执行结束!") 'WScript.Echo "删除不目录存在!"Wscript.quitEnd IfSet targetFSO = objFSO.GetFolder(DirPath)Set subFSO = targetFSO.FilesoperateobjStream.WriteLine(now()&" 取得所有文件名!")''WScript.Echo "12!"For Each subFiles in subFSOoperateobjStream.WriteLine(now()&" 对大于某时间的文件进行删除文件条件判断开始!")''WScript.Echo "13!"'If DateDiff("n",subFolder.DateCreated,now)>1 ThenIf DateDiff(deltimetype,subFiles.DateCreated,now)>deltimenum Then'-----------------读取规则文件------------------------dim objtxt,objWorkbook,txtbookline,txtbookline1,delfieleID 'delfieleID表示删除标志delfieleID="true" '默认是允许删除Set objtxt=CreateObject("Scripting.FileSystemObject")'Set objWorkbook=objtxt.OpenTextFile("D:\Share\Public\ruletxt.txt",1,true)'Set objWorkbook=objtxt.OpenTextFile("E:\ruletxt.txt",1,true)Set objWorkbook=objtxt.OpenTextFile(ruletxtpath,1,true)'txtbookline=Trim(Split(Trim(objWorkbook.ReadLIne),"|")) 'Split返回一个一维数组ifobjWorkbook.AtEndOfStream<>true thentxtbookline=objWorkbook.ReadLIneoperateobjStream.WriteLine(now()&" 读取关键字规则成功!规则是:["&txtbookline&"]")end iftxtbookline1=Split(txtbookline,",")'WScript.Echotxtbookline'WScript.EchouBound(txtbookline1)fori=0 to uBound(txtbookline1)ifInStr(1,,txtbookline1(i))>0 thendelfieleID="false"Exit forend ifnextobjWorkbook.CloseifdelfieleID="true" thenoperateobjStream.WriteLine(now()&" ["&&"] 符合关键字删除规则!")elseoperateobjStream.WriteLine(now()&" ["&&"] 不符合关键字删除规则!")end ifoperateobjStream.WriteLine(now()&" 关键字规则判断结束!")'-------------------规则遍历结束,满足条件已删除--------------------operateobjStream.WriteLine(now()&" 文件所有者规则判断开始!")'--------------------------判断文件所有者开始-------------'dim objtxt,objWorkbook,txtbookline,txtbookline1,delfieleID 'delfieleID表示删除标志'delfieleID="true" '默认是允许删除Set objtxt=CreateObject("Scripting.FileSystemObject")'Set objWorkbook=objtxt.OpenTextFile("E:\ruletxt.txt",1,true)Set objWorkbook=objtxt.OpenTextFile(ruletxtpath,1,true)'txtbookline=Trim(Split(Trim(objWorkbook.ReadLIne),"|")) 'Split返回一个一维数组objWorkbook.SkipLine() '跳过第一行ifobjWorkbook.AtEndOfStream<>true thentxtbookline=objWorkbook.ReadLIneoperateobjStream.WriteLine(now()&" 读取所有者规则成功!规则是!["&txtbookline&"]") end iftxtbookline1=Split(txtbookline,",")'WScript.Echotxtbookline'WScript.EchouBound(txtbookline1)'取得将要删除文件的所有者dimstrFileName,FileOwnerstrFileName = DirPath& '完整路径名Set objWMIService = GetObject("winmgmts:")Set objFileSecuritySettings = _objWMIService.Get("Win32_LogicalFileSecuritySetting='" &strFileName& "'")intRetVal = objFileSecuritySettings.GetSecurityDescriptor(objSD)If intRetVal = 0 Then''WScript.Echo "Owner: " &objSD.Owner.Domain& "\" &FileOwner=Else''WScript.Echo "Couldn't retrieve security descriptor."FileOwner=""End Iffori=0 to uBound(txtbookline1)ifInStr(1,FileOwner,txtbookline1(i))>0 thendelfieleID="false"Exit forend ifnextobjWorkbook.CloseifdelfieleID="true" thenoperateobjStream.WriteLine(now()&" ["&&"] 符合文件所有者删除规则!")elseoperateobjStream.WriteLine(now()&" ["&&"] 不符合文件所有者删除规则!")end ifoperateobjStream.WriteLine(now()&" 文件所有者规则判断结束!")'--------------------------判断文件所有者结束,特定所有者不删除-----------ifdelfieleID="true" thenstrPath = DirPath&'WScript.Echo '------日志记录开始-----'三个参数的分别为:以只读模式打开文件、以只写方式打开文件、打开文件并在文件末尾进行写操作'ConstintForReading = 1,intForWriting = 2,intForAppending = 8on error resume next '打开错误捕获dimstrText,intForAppendingintForAppending=8strText="已删除文件:"&now()&" "&FileOwner&" "&Set logfso = CreateObject("Scripting.FileSystemObject")Set objStream = logfso.OpenTextFile(logtxtname,intForAppending,true)'打开文件,不存在则创建objStream.WriteLine(strText)objStream.CloseSet logfso = nothingSet objStream = nothingif Err.number<>0 then '错误说明operateobjStream.WriteLine(now()&" ["&&"] 文件删除日志写入失败,停止程序运行!")'WScript.Echo "写入日志失败!"Wscript.quitErr.clear '清除err对象的内容elseoperateobjStream.WriteLine(now()&" ["&&"] 文件删除日志写入成功!") 'WScript.Echo "日志写入成功!"end if'------日志记录结束-----objFSO.DeleteFile(strPath)operateobjStream.WriteLine(now()&" ["&&"] 文件删除成功!") 'WScript.Echo "文件删除成功!"elseoperateobjStream.WriteLine(now()&" ["&&"] 文件不能删除!") 'WScript.Echo &" 文件不能删除!"end if'strPath = DirPath&''WScript.EchostrPath''WScript.Echo "14!"'objFSO.DeleteFile(strPath)''WScript.Echo "15!"End IfNextSet objFSO = nothingSet targetFSO = nothingSet subFSO = nothingEnd FunctionCall DeleteFolder()Call DeleteFile()operateobjStream.WriteLine(now()&" 程序执行完成!") operateobjStream.WriteLine("---------------------------------------------------------") setoperatelogfso = nothingsetoperateobjStream = nothing。
VB中删除、替换或者插入内容到文本中某一行,及文本行列的处理实例RONG>VB中删除、替换或者插入内容到文本中某一行及解析文本行列的处理实例VB操作文本文件的方法很多,下面的例子是我自己作项目或者回答网友提问时做的,很有代表性,希望能够给各位朋友一些启发.´功能:删除、替换文本中一行,或者插入内容到文本中某一行´作者: soho_andy (冰)´参数:´strSourceFile原始文件完整名´strTargetFile生成新文件的完整名´intRow操作的行数Sub 操作文件中一行(strSourceFile As String, strTargetFile As String, intRow As Long)Dim filenum As IntegerDim fileContents As StringDim fileInfo() As StringDim i As IntegerDim j As Integerfilenum = FreeFileOpen strSourceFile For Binary As #filenumfileContents = Space(LOF(filenum))Get #filenum, , fileContentsClose filenumfileInfo = Split(fileContents, vbCrLf)´取出源文件行数,按照回车换行来分隔成数组filenum = FreeFileIf Dir(strTargetFile, vbNormal) <> "" Then Kill strTargetFileEnd IfDim Filestr() As String´删除一行代码块Open strTargetFile For Append As #filenum ´循环每一行For i = 0 To UBound(fileInfo) - 1If i <> intRow - 1 ThenPrint #filenum, fileInfo(i)End IfNextClose #filenum´替换一行代码块Open strTargetFile For Append As #filenum ´循环每一行For i = 0 To UBound(fileInfo) - 1If i = intRow - 1 ThenPrint #filenum, "你要替换进去的内容"End IfNextClose #filenum´插入一行代码块Open strTargetFile For Append As #filenum ´循环每一行For i = 0 To UBound(fileInfo) - 1If i = intRow - 1 ThenPrint #filenum, "你要插入到这行的内容"Print #filenum, fileInfo(i) ´保留原来的行,位置后移一位End IfNextClose #filenumMsgBox "完毕"End Sub´另外一个解决实际问题的例子´´网友的要求´设有文件a.txt,其中存放了两行数据,数据用逗号分隔,现在要读取第一行的奇数位置的数据写入到另一?的奇数位置的数据写入到第二行。
VBS读写文件及创建、删除文件set fso=createobject("scripting.filesystemobject")set zsc=createobject("scripting.dictionary")if (fso.fileexists("a.txt")) then'打开文件,参数1为forreading,2为forwriting,8为appendingset file=fso.opentextfile("a.txt",1,ture)else'创建文件,参数1为forreading,2为forwriting,8为appendingset file=fso.createtextfile( "a.txt",2,ture)'写入文件内容,有三种方法:write(x)写入x个字符,writeline写入换行,writeblanklines(n)写入n个空行file.writeline "welcome!"file.writeline "thanks!"set file=fso.opentextfile("a.txt",1,ture)end if'读取文件内容,有三种方法:read(x)读取x个字符,readline读取一行,readall读取全部'读取文件时跳行:skip(x) 跳过x个字符,skipline 跳过一行do while file.atendofstream<>trueline=line+1zsc.add line,file.readline'对话框显示msgbox "第" & line & "行: " & zsc(line) &"内容"loop'关闭文件file.close'运行CMD创建文件var ="cmd to file"set ws=CreateObject("WScript.Shell")ws.Run "cmd /c @echo "&var&">>zsc.txt",1'删除文件及文件夹:fso.deleteFile "a.txt"fso.deleteFolder "F:\abc"'创建多个文件'set fso=createobject("scripting.filesystemobject")for i=1 to 9999set file=fso.createtextfile("c:\aa"&i&".txt",true)file.closenext'把1231321321564646545746546497978654654数字分为每8位一行'Dim str,str2Const ForReading = 1, ForWriting = 2set FSO = CreateObject("Scripting.FileSystemObject")set f1 = FSO.OpenTextFile(".\a.txt",ForReading,true)str = f1.ReadLineset f1 = FSO.OpenTextFile(".\a.txt",ForWriting ,true)for i = 1 to len(str)-1 step 8str2 = mid(str,i,8)f1.WriteLine str2Next'产生随机数NrandomizeN = fix(rnd*60)'发送键盘TAB N次for index=1 to Nwshshell.SendKeys "{TAB}"next'vbs随机换桌面''服务器路径,只支持BMP格式ServerPath="\\server\share$\bmp"'本地文件名,将从服务器复制到本地后改名为这个文件名(包括完整路径)BMPname="e:\crt.bmp"Set FSO = CreateObject("Scripting.FileSystemObject") Set WshShell = WScript.CreateObject("WScript.Shell") Set F = FSO.GetFolder(ServerPath)Set FC = F.FilesNum = 0ReDim bmp(FC.Count)For Each F1 in FCIf UCase(FSO.GetExtensionName()) = UCase("BMP") thenNum = Num + 1bmp(Num) = F1.PathEnd IfNextRandomizeFSO.CopyFile bmp(Int(Num * Rnd + 1)),BMPname,TrueWshShell.RegWrite "HKEY_CURRENT_USER\Control Panel\Desktop\TileWallpaper","0","REG_SZ"WshShell.RegWrite "HKEY_CURRENT_USER\Control Panel\Desktop\Wallpaper",BMPname,"REG_SZ"WshShell.RegWrite "HKEY_CURRENT_USER\Control Panel\Desktop\WallpaperStyle","2","REG_SZ"WshShell.RegWrite"HKEY_CURRENT_USER\Software\Microsoft\Windows\Cu rrentVersion\Explorer\Advanced\ListviewShadow","1","RE G_DWORD"'如果桌面图标未透明,需要刷新组策略,如果已经透明,只需要刷新桌面WshShell.run "gpupdate /force",0'WshShell.run "RunDll32.exe USER32.DLL,UpdatePerUserSystemParameters"'VBS随机输入字符到网页对话框''set huobilie = Wscript.CreateObject("Wscript.Shell") 'huobilie.run "iexplore "Set ie = Wscript.CreateObject("InternetExplorer.Application")ie.Navigate "about:blank"ie.ToolBar = 0ie.StatusBar = 0ie.Width=400ie.Height = 100ie.Left = 300ie.Top = 300SynchronizeIE()ie.Visible = 1ie.Document.Body.InnerHTML = "自动输入"WScript.Sleep 2000ie.navigate ""SynchronizeIE()Set wshshell=CreateObject("wscript.shell")wscript.sleep 500wshshell.Sendkeys"{TAB}"wshshell.Sendkeys"{TAB}"wshshell.Sendkeys"{TAB}"wshshell.Sendkeys"{TAB}"wshshell.Sendkeys"{TAB}" wshshell.Sendkeys"{TAB}" wshshell.Sendkeys"{TAB}" wshshell.Sendkeys"{TAB}" wscript.Sleep 500wshshell.Sendkeys"a" wshshell.Sendkeys"d" wshshell.Sendkeys"m" wshshell.Sendkeys"i"wshshell.Sendkeys"n" wshshell.Sendkeys"{ENTER}"SynchronizeIE()ie.Visible=1'WShell.SendKeys "~" ' 回车'wscript.Sleep 5000'Wshell.SendKeys "^W" ' 关闭IE窗口'//等待IE操作结束。
方法一:#include <stdio.h>#include <stdlib.h>#include <string.h>#define BUF 100char buf[100][100];//用来存放文件中每一行的数据char temp[100];int main(int argc,char *argv[]){FILE *fp;int k=0;if((fp=fopen(argv[1],"r+"))==0){printf("can not open file\n");exit(1);}while((fgets(temp,BUF,fp))!=0){ k++;if (k==atoi(argv[2])){If ( fgets ( temp, BUF, fp)!=0)//t跳过所要删除的那一行{strcpy(buf[k],temp);}}elsestrcpy(buf[k],temp);}fclose(fp);remove(argv[1]);//删除原文件,用新建的同名文件,来存放数据if((fp1=fopen(argv[1],"w+"))==0){printf("can not recreat file\n");exit(1);}for(i=1;i<=k;i++)fputs(buf[i],fp1);fclose(fp1);return 0;}方法二://指针定位到要删除的那一行,原理同上,就是在删除最后一行(因为删除一行而使得最后一行为空,要删除掉)的时候用到的是定位。
#include"stdio.h"#include"stdlib.h"#include"string.h"#define BUF 100char buf[100][100];char temp[100];int main(int argc,char **argv ){int defileline(char *fname,int i);defileline(argv[1],atoi(argv[2]));return 0;}int defileline(char *fname,int i){ FILE *fp;int k=0;int j=0;int nowlength1;int nowlength2;if((fp=fopen(fname,"r+"))==0){printf("can not open file\n");exit(1);}while((fgets(temp,BUF,fp))!=0){ k++;if (k==(i-1){nowlength2=ftell(fp);}if(k==i){if(fgets(temp,BUF,fp)!=0){strcpy(buf[k],temp);}}elsestrcpy(buf[k],temp);}j=k;fseek(fp,nowlength2,SEEK_SET);for(k=i;k<j;k++){fputs(buf[k],fp);}nowlength1=ftell(fp);fseek(fp,nowlength1,SEEK_SET);//删除最后一行。
怎样用VB删除TXT文本的第一行?一个删除txt文本文件指定行数的函数,如果想删除某一行直接调用这个函数就行了Private Function DelLine(strFile As String, RLine As Long, newFile As String, SameLine As Boolean)Dim s As String, n As String, i As Longi = 1'//打开源文件Open strFile For Input As #1Do Until EOF(1)Line Input #1, sIf RLine = i Then '如果是指定的行数就进行下面的操作'------------------------------------------------------If SameLine = True Then '是否保持源文件行数不变(以空白字符替换这一行内容)的提示,True保持源文件的行数,False为直接删除这一行的内容s = ""n = n & s & vbCrLf '将空字符串赋给变量n,以保持源文件的行数' MsgBox strFile & " 文件中,第 " & RLine & " 行内容" & vbCrLf & s & vbCrLf & "已经删除", vbInformation, "消息提示"End If' s="也可以把这一行的内容改成自己需要的"'-------------------------------------------------------Else '如果不是指定的行数,就将s的内容赋给变量n 以存储数据n = n & s & vbCrLf '将s的内容赋给n 并以一个回车符号结束....End Ifi = i + 1LoopClose #1'//写入新文件,如果和源文件同名则会覆盖源文件Open newFile For Output As #2Print #2, n '将n变量里的数据写入新文件Close #2End Function'调用方法:'比如要把c:\1.txt 删除其中的第5行内容,并保留源文件总行数(删除的这行插入一空字符串)DelLine "C:\1.txt", 5, "C:\2.txt", True'删除C:\1.txt 删除里面的第一行,且不保留文件的总行数DelLine "C:\1.txt", 1, "C:\1.txt", False。
vbs 用法vbs 用法VBS(Visual Basic Script)是一种常用的脚本语言,通常用于编写Windows操作系统的脚本和宏。
它具有简单易懂、灵活性强的特点,可用于自动化任务、系统管理、数据处理等方面。
以下是一些常见的vbs用法及其详细解释:文件操作•创建文件夹:Dim fsoSet fso = CreateObject("")"C:\NewFolder"使用FileSystemObject对象的CreateFolder方法可以创建指定路径下的文件夹。
•复制文件:Dim fsoSet fso = CreateObject("")"C:\", "C:\"使用FileSystemObject对象的CopyFile方法可以复制文件到指定目标路径。
•删除文件:Dim fsoSet fso = CreateObject("")"C:\"使用FileSystemObject对象的DeleteFile方法可以删除指定路径下的文件。
字符串处理•字符串连接:Dim str1, str2, resultstr1 = "Hello"str2 = " World"result = str1 & str2MsgBox result使用&符号可以将两个字符串连接起来,生成新的字符串。
•字符串查找:Dim str, positionstr = "Hello World"position = InStr(str, "World")MsgBox position使用InStr函数可以在一个字符串中查找另一个字符串,并返回其位置。
•字符串替换:Dim strstr = "Hello World"str = Replace(str, "World", "VBS")MsgBox str使用Replace函数可以将字符串中的某个子串替换为新的字符串。
如何从文本文件中删除所有空白行?如何从文本文件中删除所有空白行?问:您好,脚本专家!如何从文本文件中删除所有空白行?-- RE答:您好,RE。
您知道,早在2002 年,当时的脚本专家们还只是一些默默无闻之辈时,我们就开始编写Microsoft Windows 2000 Scripting Guide(英文)了。
在整理该书时,我们遇到了来自某些人的巨大阻力,这些人认为即使是提到文本文件也是一个错误。
为什么会是这样?因为没有人还在使用文本文件,我们在一项过时的技术上浪费时间是有点傻。
注意:无论您是否相信,即便该书旨在向人们讲授如何使用VBScript 编写脚本,可是在将 VBScript 一章加入该书时我们还是遇到了相当大的阻力。
但这是另外一回事。
所有这一切说明了什么?现在是2006 年,您好,脚本专家!专栏所要解决的第一个问题就需要用文本文件进行处理。
这对于没人使用的过时技术来说还不算坏吧?假定您有一个类似如下的文本文件:Line 1Line 2Line 3而您希望该文本文件看起来是这样:Line 1Line 2Line 3使用脚本能够做到这一点吗?当然可以:Const ForReading = 1Const ForWriting = 2Set objFile = objFSO.OpenTextFile("C:\Scripts\Test.txt", ForReading)Do Until objFile.AtEndOfStreamstrLine = objFile.ReadlinestrLine = Trim(strLine)If Len(strLine) > 0 ThenstrNewContents = strNewContents & strLine & vbCrLfEnd IfLoopobjFile.CloseSet objFile = objFSO.OpenTextFile("C:\Scripts\Test.txt", ForWriting)objFile.Write strNewContentsobjFile.Close在了解脚本工作原理的细枝末节之前,让我们先讲一下基本思想。
vbs操作txt⽂本⽂件常⽤⽅法与函数代码'creat by 席飞剑(⼩席⽼师)'操作⽂本⽂件,操作fso对象(⽂件对象操作)函数代码创建⽂件dim fso, fset fso = server.CreateObject("Scripting.FileSystemObject")set f = fso.CreateTextFile("C:\test.txt", true) '第⼆个参数表⽰⽬标⽂件存在时是否覆盖f.Write("写⼊内容")f.WriteLine("写⼊内容并换⾏")f.WriteBlankLines(3) '写⼊三个空⽩⾏(相当于在⽂本编辑器中按三次回车)f.Close()set f = nothingset fso = nothing打开并读⽂件dim fso, fset fso = server.CreateObject("Scripting.FileSystemObject")set f = fso.OpenTextFile("C:\test.txt", 1, false) '第⼆个参数 1 表⽰只读打开,第三个参数表⽰⽬标⽂件不存在时是否创建f.Skip(3) '将当前位置向后移三个字符f.SkipLine() '将当前位置移动到下⼀⾏的第⼀个字符,注意:⽆参数response.Write f.Read(3) '从当前位置向后读取三个字符,并将当前位置向后移三个字符response.Write f.ReadLine() '从当前位置向后读取直到遇到换⾏符(不读取换⾏符),并将当前位置移动到下⼀⾏的第⼀个字符,注意:⽆参数response.Write f.ReadAll() '从当前位置向后读取,直到⽂件结束,并将当前位置移动到⽂件的最后if f.atEndOfLine thenresponse.Write("⼀⾏的结尾!")end ifif f.atEndOfStream thenresponse.Write("⽂件的结尾!")end iff.Close()set f = nothingset fso = nothing打开并写⽂件dim fso, fset fso = server.CreateObject("Scripting.FileSystemObject")set f = fso.OpenTextFile("C:\test.txt", 2, false) '第⼆个参数 2 表⽰重写,如果是 8 表⽰追加f.Write("写⼊内容")f.WriteLine("写⼊内容并换⾏")f.WriteBlankLines(3) '写⼊三个空⽩⾏(相当于在⽂本编辑器中按三次回车)f.Close()set f = nothingset fso = nothing判断⽂件是否存在dim fsoset fso = server.CreateObject("Scripting.FileSystemObject")if fso.FileExists("C:\test.txt") thenresponse.Write("⽬标⽂件存在")elseresponse.Write("⽬标⽂件不存在")end ifset fso = nothing移动⽂件dim fsoset fso = server.CreateObject("Scripting.FileSystemObject")call fso.MoveFile("C:\test.txt", "D:\test111.txt") '两个参数的⽂件名部分可以不同set fso = nothing复制⽂件dim fsoset fso = server.CreateObject("Scripting.FileSystemObject")call fso.CopyFile("C:\test.txt", "D:\test111.txt") '两个参数的⽂件名部分可以不同set fso = nothing删除⽂件dim fsoset fso = server.CreateObject("Scripting.FileSystemObject")fso.DeleteFile("C:\test.txt")set fso = nothing创建⽂件夹dim fsoset fso = server.CreateObject("Scripting.FileSystemObject")fso.CreateFolder("C:\test") '⽬标⽂件夹的⽗⽂件夹必须存在set fso = nothing判断⽂件夹是否存在dim fsoset fso = server.CreateObject("Scripting.FileSystemObject")if fso.FolderExists("C:\Windows") thenresponse.Write("⽬标⽂件夹存在")elseresponse.Write("⽬标⽂件夹不存在")end ifset fso = nothing删除⽂件夹dim fsoset fso = server.CreateObject("Scripting.FileSystemObject")fso.DeleteFolder("C:\test") '⽂件夹不必为空set fso = nothing这篇⽂章就介绍到这,更多的⼤家可以查看以前发布的关于vbs txt操作的相关⽂章。
有时候我们需要删除文本文件的指定的一行内容,下面是具体的函数Delete Line Function
代码如下:
Function DeleteLine(strFile, strKey, LineNumber, CheckCase)
'DeleteLine Function by TomRiddle 2008
'Remove line(s) containing text (strKey) from text file (strFile)
'or
'Remove line number from text file (strFile)
'or
'Remove line number if containing text (strKey) from text file (strFile)
'Use strFile = "c:\file.txt" (Full path to text file)
'Use strKey = "John Doe" (Lines containing this text string to be deleted) 'Use strKey = "" (To not use keyword search)
'Use LineNumber = "1" (Enter specific line number to delete)
'Use LineNumber = "0" (To ignore line numbers)
'Use CheckCase = "1" (For case sensitive search )
'Use CheckCase = "0" (To ignore upper/lower case characters)
Const ForReading=1:Const ForWriting=2
Dim objFSO,objFile,Count,strLine,strLineCase,strNewFile
Set objFSO=CreateObject("Scripting.FileSystemObject")
Set objFile=objFSO.OpenTextFile(strFile,ForReading)
Do Until objFile.AtEndOfStream
strLine=objFile.Readline
If CheckCase=0 then strLineCase=ucase(strLine):strKey=ucase(strKey) If LineNumber=objFile.Line-1 or LineNumber=0 then
If instr(strLine,strKey) or instr(strLineCase,strkey) or strKey="" then strNewFile=strNewFile
Else
strNewFile=strNewFile&strLine&vbcrlf
End If
Else
strNewFile=strNewFile&strLine&vbcrlf
End If
Loop
objFile.Close
Set objFSO=CreateObject("Scripting.FileSystemObject")
Set objFile=objFSO.OpenTextFile(strFile,ForWriting)
objFile.Write strNewFile
objFile.Close
End Function
使用方法:
DeleteLine "c:\1.txt", "", 1, 0。