vbscript - Iterate through the folder and find a particular file -
i have written code iterate through folders , exit function when finds ".c" file. ideally should return path of ".c" file. returning empty string.
tval = 1 function findstlcode(objfso,ffolder,foldername) set objfso = createobject("scripting.filesystemobject") set folder = objfso.getfolder(ffolder) set colfiles = folder.files each objfile in colfiles strfilename = objfile.name if strcomp(strfilename,foldername & ".c",vbtextcompare) = 0 findstlcode = ffolder & "\" & foldername & ".c" tval = tval + 1 exit function end if next each subfolder in folder.subfolders if tval = 1 xx = findstlcode(objfso, subfolder.path, foldername) end if next end function
change loop contents below, change xx findstlcode , should work
strfilename = objfile.name if right(strfilename, 2) = ".c" findstlcode = ffolder & "\" & strfilename tval = tval + 1 exit function end if
complete code below
tval = 1 function findstlcode(objfso,ffolder) set objfso = createobject("scripting.filesystemobject") set folder = objfso.getfolder(ffolder) set colfiles = folder.files each objfile in colfiles strfilename = objfile.name if right(strfilename, 2) = ".c" findstlcode = ffolder & "\" & strfilename tval = tval + 1 exit function end if next each subfolder in folder.subfolders if tval = 1 findstlcode = findstlcode(objfso, subfolder.path) end if next end function
calling function
set objfso = createobject("scripting.filesystemobject") wscript.echo findstlcode(objfso, "c:\")
Comments
Post a Comment