Loop through folder using array to find lastest version (count) with VBA? -
i have attached code, however, find files present in folder.
want have incremental counter files. thing version start else 0 or 1, e.g. 3.
amesto non suppliers test w20-3 an
want next string 4.
i using this, work if 1 first, etc. stuck.
' version check while len(dir(strpath2 & "amesto non suppliers test w" & week & "-" & version & "*.cif")) <> 0 version = version + 1 strpath = getdirectorypath & "amesto non suppliers test w" & week & "-" & version & " " & username & ".cif" loop sub loadversion() dim myfile string dim counter long 'create dynamic array variable, , declare initial size dim directorylistarray() string redim directorylistarray(1000) 'loop through files in directory using dir$ function myfile = dir$("c:\users\niclas.madsen\desktop\ap\wave3\cif\*.*") while myfile <> "" directorylistarray(counter) = myfile myfile = dir$ counter = counter + 1 loop ' here?! if myfile = vbnullstring else end if 'reset size of array without losing values using redim preserve redim preserve directorylistarray(counter - 1) counter = 0 ubound(directorylistarray) 'debug.print writes results immediate window (press ctrl + g view it)' debug.print directorylistarray(counter) next counter end sub
to highest version on filename in directory, insert following functions:
function checkhighestversion(path string, cutlettersatwordbeginning integer) integer dim file variant dim tobecut string dim verlength integer dim highestversion integer highestversion = 0 file = dir(path) while (file <> "") tobecut = file tobecut = mid(tobecut, cutlettersatwordbeginning + 1) verlength = findverlength(tobecut) if verlength = -1 checkhighestversion = 0 exit function end if tobecut = left(tobecut, verlength) if val(tobecut) > highestversion highestversion = val(tobecut) end if file = dir wend checkhighestversion = highestversion end function function findverlength(filename string) integer dim integer = 1 len(filename) if not isnumeric(mid(filename, i, 1)) if = 1 msgbox "couldn't obtain highest version of files: " & _ "the first letter of version not numeric. letter " & mid(filename, i, 1) & _ ". please use correct amount of letters cut @ beginning of file name." findverlength = -1 exit function end if findverlength = - 1 exit function end if next findverlength = end function
call checkhighestversion
in sub. path directory (e.g. c:\test\ ), second parameter number of letters don't need @ beginning of word. if counted correctly, value should 30+(length of week, week 25 2, week 7 1) in case. function returns highest version contained in folder.
Comments
Post a Comment