php中文网 | cnphp.com

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 479|回复: 0

vbs批量读取文件夹的文件

[复制链接]

3138

主题

3148

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

UID
1
威望
0
积分
7946
贡献
0
注册时间
2021-4-14
最后登录
2024-11-21
在线时间
763 小时
QQ
发表于 2022-7-8 08:11:42 | 显示全部楼层 |阅读模式
Get_file()
function Get_file()
dim a
a=InputBox("Please enter the full path name of the folder","Warm prompt!")
if a = vbEmpty  then
else
if a = "" then
msgbox "Cannot be empty, please re-enter!"
else
dim b
b=Split(a,",")
dim fso,f,h,j,m,m1,m2
set fso= CreateObject("scripting.FileSystemObject")
if fso.FolderExists(b(0)) then
   set f=fso.GetFolder(b(0))
   select case UBound(b)
   case 1
   h = b(1)
   m = Instr(h, "-")
   m1 = Left(h, m)
   m2 = Right(h, len(h)-m)
   case 2
   h = b(1)
   m = Instr(h, "-")
   m1 = Left(h,m)
   m2 = Right(h, len(h)-m)
   j = b(2)
end select

dim Arr1()
dim Arr2()
dim index
index = 0
For Each objfile in f.Files
   dim s1,s2,s3,s4,s5,s6,s7,s8,s9,s10,c1,c2,c3,c4,c5
    s1 = objfile.name
    s2 = InStrRev(s1, ".")
    if(s2 = 0) Then                    
        s3 = s1
    else
        s3 = Left(s1, s2 - 1)
    end if
   select case len(m2)
    case 1
       c5 = "0000" & m2
    case 2
       c5 = "000" & m2
    case 3
       c5 = "00" & m2
    case 4
       c5 = "0" & m2
    case 5
       c5 = m2
   end select
   s4 = m1 & c5 & "_" & s3 & " " & j
   s5 = f & "\" & s4

   s6 = Instr(s3, "(")
   if(s6 = 0) Then                    
        c1 = s3
    else
        c1 = Left(s3, s6 - 1)
    end if
     
    s7 = InStrRev(s3, "-")
    if(s7 = 0) Then                    
        c3 = s3
     else
        c2 = Right(s3, len(s3)-s7)
        s8 = Instr(c2, "(")
           if (s8 = 0) Then  
              c3 = c2
           else
             c3 = Left(c2,s8-1)
           end if
    end if
    ReDim Preserve Arr1(index)
    Arr1(index)=c1
    ReDim Preserve Arr2(index)
    Arr2(index)=c3
    index = index + 1
    s9 = f & "\" & s1
    s10 = s5 & "\" & s1
    if fso.FolderExists(s5) then
     if fso.fileExists(s10) then
      else
      fso.MoveFile s9,s10
      end if
   else
      fso.CreateFolder(s5)
      fso.MoveFile s9,s10
   end if
  m2 = m2 + 1
next
on error resume next
dim u_ss
u_ss=ubound(Arr1)
if err then
else
dim FileName,ExcelApp,ExcelBook,FolderName,CurrentDate,ExcelSheet,openexcel,rowcount,openSheet
CurrentDate = "New_Excel_" & year(Now)
FolderName = f & "\" & CurrentDate
if fso.FolderExists(FolderName) then  
      FileName = FolderName & "\" & CurrentDate & ".xlsx"
   else
      fso.CreateFolder(FolderName)
      FileName = FolderName & "\" & CurrentDate & ".xlsx"
   end if
if fso.fileExists(FileName) then
   else
    set ExcelApp = CreateObject("Excel.Application")
     set ExcelBook = ExcelApp.Workbooks.Add
     set ExcelSheet = ExcelBook.ActiveSheet
      ExcelSheet.Columns("A:B").AutoFit()
      dim ar1,ar2
      ar1=1
      ar2=1
      for each r1 in Arr1
          ExcelSheet.Range("A" & ar1)=r1
          ar1=ar1+1
      next
      for each r2 in Arr2
          ExcelSheet.Range("B" & ar2)=r2
          ar2=ar2+1
      next
      ExcelBook.SaveAs(FileName)
      ExcelBook.Close
      Set ExcelBook = Nothing
      set ExcelSheet = Nothing
      ExcelApp.Quit
      Set ExcelApp = Nothing
   end if
end if
else
    MsgBox "The entered folder does not exist."
end if
set fso = Nothing
end If
end If
end function

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|php中文网 | cnphp.com ( 赣ICP备2021002321号-2 )

GMT+8, 2024-11-22 04:06 , Processed in 0.277752 second(s), 35 queries , Gzip On.

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2020, Tencent Cloud.

申明:本站所有资源皆搜集自网络,相关版权归版权持有人所有,如有侵权,请电邮(fiorkn@foxmail.com)告之,本站会尽快删除。

快速回复 返回顶部 返回列表