excel - Copying the location of file names with specific extensions -
i have following code list out files in folder , sub folder path.but want list out files have specific extensions only(ex:.txt files alone)
how can that..?
code: sub test() call listfilesinfolder("d:\downloads", true) end sub sub listfilesinfolder(byval sourcefoldername string, byval includesubfolders boolean) dim fso object dim sourcefolder object dim subfolder object dim fileitem object dim r long set fso = createobject("scripting.filesystemobject") set sourcefolder = fso.getfolder(sourcefoldername) r = range("a65536").end(xlup).row + 1 each fileitem in sourcefolder.files cells(r, 1).formula = fileitem.name cells(r, 2).formula = fileitem.path r = r + 1 ' next row number x = sourcefolder.path next fileitem if includesubfolders each subfolder in sourcefolder.subfolders listfilesinfolder subfolder.path, true next subfolder end if set fileitem = nothing set sourcefolder = nothing set fso = nothing end sub
you can use getextensionname method this:
sub listfilesinfolder(byval sourcefoldername string, byval includesubfolders boolean) dim fso object dim sourcefolder object dim subfolder object dim fileitem object dim r long set fso = createobject("scripting.filesystemobject") set sourcefolder = fso.getfolder(sourcefoldername) r = range("a65536").end(xlup).row + 1 each fileitem in sourcefolder.files if fso.getextensionname(fileitem) = "txt" 'change txt want cells(r, 1).formula = fileitem.name cells(r, 2).formula = fileitem.path r = r + 1 ' next row number end if x = sourcefolder.path next fileitem if includesubfolders each subfolder in sourcefolder.subfolders listfilesinfolder subfolder.path, true next subfolder end if set fileitem = nothing set sourcefolder = nothing set fso = nothing end sub
Comments
Post a Comment