SoFunction
Updated on 2025-04-11

VBS batch reads all text in folders to Excel scripts


'This code is done by KangKang@
Option explicit ‘This is optional, but better to use.
Dim FolderPath,Folder
Dim fso,File,Files
Dim fileNums
Dim FileString()
Dim i
Dim ii

i=0
FolderPath="E:\TDDOWNLOAD\aa\"

'********************** create the FileSystemObject object********************************
Set fso= CreateObject("")'This is the way to create FileSystemObjecy
‘This sentence can also be defined in Excel VBA to reference FSO!
'Scripting is the name of the class library, filesystemobject is the referenced pair, indicating that the object used by VBA is not owned, but refers to the external world.

'********************** create the Folder and File object*********************************
If (FolderPath) Then
Set Folder = (FolderPath) 'This set command is neccessary!
Set Files=
fileNums=
'Msgbox fileNums
For Each File In
if right(,2)="rm" then
ReDim Preserve FileString(i) 'This is a Dynamic Array, so we should use the Redim command
'Be careful of the Preserve word, important!!!!
FileString(i)=
'MsgBox i & " " & FileString(i)
i=i+1
fileNums=i
End if
Next
End If

'********************** Excel and stroe the file name in it***************************
Dim objExcel
Dim objWorkbook

Set objExcel = ("")

=True

Set objWorkbook =
For ii=1 to fileNums
(1).Cells(ii,1)=FileString(ii-1)
Next
(1).Range("A1:A1").
= False
(FolderPath & "")

()'Close the Workbook
()'Quit
Set fso=Nothing

'********************** the files and read the first line.******************************
Dim Range
Dim Range_i
Dim mfile
Dim sline
Dim iii

set fso=createobject("")
Set objExcel = ("")
=True
(FolderPath & "")

Set objWorkbook =
Set Range = ("A1:A11")

For Range_i=1 to fileNums
set mfile=(Range(Range_i).value)
msgbox Range_i & " " & Range(Range_i).value
for iii=1 to 1
sline=
(1).Cells(Range_i,2)=sline
Next

Next
(1).Range("B1:B1").
= False
(FolderPath & "")

()'Close the Workbook
()'Quit
Set fso=Nothing