vbs读取服务器上的txt文件,vbs 批量读取文件夹中所有文本到excel中的代码

'-----读取文件夹中的所有文本入excel

'-----搜集整整 jquery中文网 http://www.jquerycn.cn

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\"

'**1.To create the FileSystemObject object************

Set fso= CreateObject("Scripting.FileSystemObject")'This is the way to create FileSystemObjecy

‘这句话在Excel VBA中也可以如此定义来引用FSO!

'Scripting是类库的名字,filesystemobject是所引用的对 '象, 说明了此时VBA所用的对象不是自带的,而是引用 '外界的。

'**2.To create the Folder and File object*************

If fso.FolderExists(FolderPath) Then

Set Folder = fso.GetFolder(FolderPath) 'This set command is neccessary!

Set Files=Folder.Files

fileNums=Files.Count

'Msgbox fileNums

For Each File In Folder.Files

if right(File.name,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)=File.Name

'MsgBox i & " " & FileString(i)

i=i+1

fileNums=i

End if

Next

End If

'**3.Create Excel and stroe the file name in it*******

Dim objExcel

Dim objWorkbook

Set objExcel = WScript.CreateObject("Excel.Application")

objExcel.Workbooks.Add

objExcel.Visible=True

Set objWorkbook = objExcel.ActiveWorkbook

For ii=1 to fileNums

objWorkbook.Worksheets(1).Cells(ii,1)=FileString(ii-1)

Next

objWorkbook.Worksheets(1).Range("A1:A1").Columns.AutoFit

objExcel.DisplayAlerts = False

objWorkbook.SaveAs(FolderPath & "xiao.xls")

objWorkbook.Close()'Close the Workbook

objExcel.Quit()'Quit

Set fso=Nothing

'**4.Open the files and read the first line.**********

Dim Range

Dim Range_i

Dim mfile

Dim sline

Dim iii

set fso=createobject("scripting.filesystemobject")

Set objExcel = WScript.CreateObject("Excel.Application")

objExcel.Visible=True

objExcel.Workbooks.open(FolderPath & "xiao.xls")

Set objWorkbook = objExcel.ActiveWorkbook

Set Range = objWorkbook.Activesheet.range("A1:A11")

For Range_i=1 to fileNums

set mfile=fso.opentextfile(Range(Range_i).value)

msgbox Range_i & " " & Range(Range_i).value

for iii=1 to 1

sline=mfile.readline

objWorkbook.Worksheets(1).Cells(Range_i,2)=sline

Next

mfile.close

Next

objWorkbook.Worksheets(1).Range("B1:B1").Columns.AutoFit

objExcel.DisplayAlerts = False

objWorkbook.SaveAs(FolderPath & "xiao.xls")

objWorkbook.Close()'Close the Workbook

objExcel.Quit()'Quit

Set fso=Nothing

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值