Link to home
Start Free TrialLog in
Avatar of bfuchs
bfuchsFlag for United States of America

asked on

Script to read all files of specific folder.

Hi Experts,
I am looking for a script that will read all files of a specific folder (.txt,.csv) and count how many records they're in total.
all files have fields header, however some files only contains the header and should not be count.
Thanks in advance.
Avatar of Subodh Tiwari (Neeraj)
Subodh Tiwari (Neeraj)
Flag of India image

You may give a try to the following VB Script.

Dim fso
Dim folderPath
Dim file
Dim oFile
Dim NewFileName
Dim NewFile
Dim fileContent
Dim arr
Dim cnt

Set fso=CreateObject("Scripting.FileSystemObject")
folderPath="C:\temp\"	'Change the folder path as required and remember to add a back slash in the end

NewFileName="Record Count.txt"	'Output will be written to this text file which will be created and saved in the same folder
Set NewFile=fso.CreateTextFile(folderPath & NewFileName,True)
NewFile.WriteLine "FileName" & vbTab & "Record Count"

For Each file In fso.GetFolder(folderPath).Files
	If (LCase(fso.GetExtensionName(file))="csv" Or LCase(fso.GetExtensionName(file))="txt") And file.Name<>NewFileName Then
		Set oFile=fso.OpenTextFile(file)
		fileContent=oFile.ReadAll
		arr=Split(fileContent,vbCrLf)
		cnt=UBound(arr)
		If cnt>0 Then
			NewFile.WriteLine file.Name & vbTab & cnt
		End If
		oFile.Close
		Set oFile=Nothing
	End If
Next
NewFile.Close
Set NewFile=Nothing
Set fso=Nothing

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Subodh Tiwari (Neeraj)
Subodh Tiwari (Neeraj)
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bfuchs

ASKER

Hi Subodh,
I'm out of the office today, will try to test on Sunday & keep you posted.
Have a nice weekend.
Thanks,
Ben
Avatar of bfuchs

ASKER

Thanks Subdoh this worked.
You're welcome Ben!