我需要创建一个批处理文件或vbscript来删除驱动器中文本文件的每一行。所以,一个文本文件
Hello.
Blah blah blah.
Goodbye.
将成为
Hello.
Blah blah blah.
TIA。
PS:我正在创建学习批处理和vbscript的项目。我的最终目标是学习脚本语言。所以,我想请求代码旁边的解释,以便我能理解。感谢。
答案 0 :(得分:1)
首先查看这些命令。
for
,
for /f
,
for /r
使用/?
参数获取帮助。
收藏这些!
答案 1 :(得分:0)
以下是针对您的评论解决方案。
请注意,在整个驱动器上运行此类操作可能是一项危险的操作 - VBScript不能很好地处理Unicode文件,因此如果您针对整个驱动器运行此操作,请确保您知道要更改的内容。
我已将它设置为仅针对sub-directoy(C:\ Somedirectory),在我的示例中,您需要将其更改为您想去的地方。另外,虽然我测试了这个,但我不能保证它不会出现行为异常:考虑一下 - 如果你多次运行,最终会得到一大堆0字节(空)文件!
警告:这将永久删除您无法取回的数据。我不建议盲目使用整个驱动器!!
免责声明:使用风险自负。
Option Explicit
' Option Explicit forces all variables to be declared good way to sanity check some times.
Const ForWriting = 2
Const ForReading = 1
' Set our Contsants for manipulating files read/writes
' Dimension our variables (in our case, the main loop only has one.. objFSO
Dim objFSO
' Load up the FileSystemObject for our file operations later
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Call our routine to recursively scan folders for txt files.
Call Recurse("C:\SomeFolder\")
Sub Recurse(strFolderPath)
' This routine recursively searches a folder/drive for txt files
Dim objFolder, objFile, objSubFolder
' Load up the Folder path into objFolder
Set objFolder = objFSO.GetFolder(strFolderPath)
On Error Resume Next
' Instruct VBScript to continue if an error occurs.
For Each objFile In objFolder.Files
If Err Then
' If there's an error accessing the file, likely to be access denied for files in use, etc. Let's ignore and continue
Err.clear
Else
If (InStr(objFile.Name, ".") > 0) Then
'If the file's extension is "txt", chomp it! (This calls the Function FileChomp with the path\filename.txt)
If LCase(objFso.GetExtensionName(objFile.name)) = "txt" Then FileChomp (objFile.Path )
End If
End If
Next
For Each objSubFolder In objFolder.SubFolders
Call Recurse(objSubFolder.Path)
Next
End Sub
Function FileChomp (FCName)
' This function chomps the last line of a text file.
Dim strdata, objFile, arrlines, strlines, filesize
' This section gets our text file size.
filesize = 0
Set objFile = objFSO.GetFile(FCName)
FileSize = objFile.Size
Set objFile = Nothing
'This resets our array for lines, incase there's something in there already from last time?
ReDim arrlines(0)
' Open the file for reading
Set objFile = objFSO.OpenTextFile(FCName, ForReading)
' If the filesize we retrieved earlier is greater than 0, go do this
If FileSize > 0 then
strLines = 0
Do While objFile.atendofstream = false
' This loop reads in the file we're going to chomp and stores it line by line into arrLines as an array
ReDim Preserve arrLines(strLines)
arrLines(strlines) = objFile.Readline
strLines = strLines + 1
Loop
' This sets the file as we are going to write back to it (less the last line) - BE WARNED This may not work for unicode text files.
Set objFile = objFSO.OpenTextFile(FCName, ForWriting)
For strLines = 0 to UBound(arrLines) - 1
' This For Loop writes out the file (less the last line)
objFile.WriteLine arrLines(strLines)
Next
End If
objFile.Close
End Function
答案 2 :(得分:0)
此删除最后N行的脚本适用于您:
设置Line = Line - N,只保留处理行号
@echo OFF
setlocal EnableDelayedExpansion
set LINES=0
for /f "delims==" %%I in (input.txt) do (
set /a LINES=LINES+1
)
echo Total Lines : %LINES%
echo.
:: n = 1 , last 1 line will be ignored
set /a LINES=LINES-1
call:PrintFirstNLine > output.txt
goto EOF
:PrintFirstNLine
set cur=0
for /f "delims==" %%I in (input.txt) do (
echo %%I
::echo !cur! : %%I
set /a cur=cur+1
if "!cur!"=="%LINES%" goto EOF
)
:EOF
exit /b
此处call:PrintFirstNLine > output.txt
将输出外部文件名作为output.txt。