vba - Count files in folder and subfolders, exlucing folders with string -
given folder tree:
c:\example\ c:\example\2014-01-01\ c:\example\2014-01-01\entered\ c:\example\2014-01-02\ c:\example\2014-01-02\entered etc.
i want count pdf files in tree, excluding in "entered\" subfolders.
is possible vba? count needs spit out onto excel sheet.
copy code in excel-vba module. if want use button should use cntfiles()
on button. if don't want use button can use fcount(strpath)
formula on worksheet i.e =fcount("your-path")
, parameter string make double-quoted when using on worksheet.
function fcount(strpath) dim fcnt integer fcnt = showfolderlist(strpath) fcount = fcnt end function sub cntfiles() dim strpath string strpath = "a:\asif\answers\abc" showfolderlist (strpath) end sub function showfolderlist(path) dim fso, folder, subflds, fld dim tfiles integer tfiles = showfileslist(path) set fso = createobject("scripting.filesystemobject") set folder = fso.getfolder(path) set subflds = folder.subfolders each fld in subflds if fld.name = "entered" goto skipfld: else path = fld.path tfiles = tfiles + showfileslist(path) end if skipfld: next 'msgbox tfiles & " files" showfolderlist = tfiles end function function showfileslist(folderspec) dim fso, f, f1, fc, s dim cnt integer set fso = createobject("scripting.filesystemobject") set f = fso.getfolder(folderspec) set fc = f.files each f1 in fc if getanextension(f1) = "pdf" cnt = cnt + 1 else end if next showfileslist = cnt end function function getanextension(drivespec) dim fso set fso = createobject("scripting.filesystemobject") getanextension = fso.getextensionname(drivespec) end function
this code count files in specified folder sub-folders excluding folder named "entered" specified.
Comments
Post a Comment