我正在尝试使用VBScript计算文本文件中的行数。我已成功地为具有固定名称的文本文件执行此操作。 EG:“C:\ Orig \ sample.txt” 但是,我们的文件名每天都会更改,EG:“C:\ Orig \ sample * todaysdate * .txt”
我看起来很高和很低,以便“读取”具有变量名称的文件并且没有运气。
到目前为止,我所拥有的固定文件名是:
Dim oFso, oReg, sData, lCount, linesum
Const ForReading = 1, sPath = "C:\Orig\sample.txt"
Set oReg = New RegExp
Set oFso = CreateObject("Scripting.FileSystemObject")
sData = oFso.OpenTextFile(sPath, ForReading).ReadAll
With oReg
.Global = True
.Pattern = "\r\n" 'vbCrLf
lCount = .Execute(sData).Count + 1
End With
WScript.Echo("The total number of lines including the header is " & lCount)
Set oFso = Nothing
Set oReg = Nothing
这非常有效,但我找不到可变文件名的正确语法。
如果有任何帮助,我想查询的文件将是包含文件夹中的唯一文件。
有人能提供任何帮助吗?非常感谢。
我现在尝试了以下内容:
Dim objFso, objReg, sData, lCount
Const ForReading = 1
sPath = "C:\Orig"
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFso.GetFolder(sPath)
For Each objFile in objFolder.Files
Set objReg = New RegExp
sData = objFso.OpenTextFile(sPath, ForReading).ReadAll
With objReg
.Global = True
.Pattern = "\r\n" 'vbCrLf
lCount = .Execute(sData).Count + 1
End With
WScript.Echo("The total number of lines including the header is " & lCount)
Set objFso = Nothing
Set objReg = Nothing
Set objFolder = Nothing
set sData = Nothing
Next
但在第9行,我收到了“权限被拒绝”的错误。我检查了文件夹权限和文件权限,我拥有完整的权限。
有人有什么想法吗?
提前致谢。
答案 0 :(得分:0)
循环浏览文件夹中的文件。无需直接命名文件。
Dim oFso, oReg, sData, lCount, linesum
Const ForReading = 1
sPath = "C:\Orig\sample\"
Set oFso = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFso.GetFolder(sPath)
For Each objFile in objFolder.Files
Set oReg = New RegExp
sData = oFso.OpenTextFile(sPath, ForReading).ReadAll
With oReg
.Global = True
.Pattern = "\r\n" 'vbCrLf
lCount = .Execute(sData).Count + 1
End With
WScript.Echo("The total number of lines including the header is " & lCount)
Set oFso = Nothing
Set oReg = Nothing
Next