从具有变量名的文件读取数据-VBScript
我正在尝试使用VBScript计算文本文件中的行数。对于具有固定名称的文本文件,我成功地做到了这一点。例如:“C:\Orig\sample.txt” 但是,我们的文件名每天都在更改,例如:“C:\Orig\sample*todaysdate*.txt” 我到处寻找一种方法来“读取”一个具有可变名称的文件,但没有找到运气 到目前为止,我对固定文件名的理解是:从具有变量名的文件读取数据-VBScript,vbscript,Vbscript,我正在尝试使用VBScript计算文本文件中的行数。对于具有固定名称的文本文件,我成功地做到了这一点。例如:“C:\Orig\sample.txt” 但是,我们的文件名每天都在更改,例如:“C:\Orig\sample*todaysdate*.txt” 我到处寻找一种方法来“读取”一个具有可变名称的文件,但没有找到运气 到目前为止,我对固定文件名的理解是: Dim oFso, oReg, sData, lCount, linesum Const ForReading = 1, sPath =
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行,我收到了一个“拒绝许可”错误。我已检查文件夹权限和文件权限,并且我拥有完全权限
有人有什么想法吗
提前感谢。而是循环浏览文件夹中的文件。不需要直接命名文件
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
嗨,谢谢你的帮助。我将objFso.GetFolder(sPath)更改为oFso.GetFolder(sPath),但第9行“未找到路径”中仍然出现错误,有什么想法吗?非常感谢。我已经解决了这个问题,我使用objFile.Name获取文件名,然后将其与文件路径组合为“objFullName”,然后将objFullName传递给…真是太棒了!