Hello everyone,

I have a problem with my code and I don´t know how to solve it. The issue is that I have several folders and within each folder a file(s) that can be in different formats, actually either ".txt" or ".trs", for files in forlders 2004, 2009-2017 and from 2005-2008 respectively: for example:
Main_path\
MCVL2004
MCVL2004_affiliation1.txt
MCVL2004_affiliation2.txt
MCVL2005
MCVL2005_affiliation1.trs
MCVL2005_affiliation2.trs
.
.
.
MCVL2009
MCVL2009_affiliation1.txt
MCVL2009_affiliation2.txt

However, I am utterly puzzled by the fact that when running the code as is below, it would only read and save files with extension ".trs", but when I comment the second chunk and uncomment the third it would only read and save ".txt" files.
Of course I would like to read and save all at once and not have to manually change the code. I have also tried to set a "else if" for folders from 2005 to 2008 but it didn´t work either. So, I would appreciate if somebody could point out what I am doing wrong and propose an alternative to make it run all straight through.

Kind Regards


Code:
cd "${rawdata_path}"

***** ***** ***** ***** ***** DATA EXTRACTION ***** ***** ***** ***** ***** *****
// BE CAREFUL!!! there are two else, comment one

local folder: dir . dirs "*"
foreach i of local folder {
di "`i'"
    if         "`i'" == "MCVL2004" {
        local filename     : dir "${rawdata_path}/`i'" files "*.txt"
        foreach f in `filename'{
            di "`f'"
            cd "${rawdata_path}/`i'"
            import delimited "`f'", delimiter(space) bindquote(nobind) case(preserve) asfloat clear
            local name "`f'"
            local name = subinstr("`name'", ".txt", "", 1)
            save "${datasets_path}/`i'/`name'.dta", replace
        }
    }
    else {
        local filename : dir "${rawdata_path}/`i'" files "*.trs"
        foreach f in `filename'{
            di "`f'"
            cd "${rawdata_path}/`i'"
            import delimited "`f'", delimiter(";", asstring) bindquote(nobind) case(preserve) asfloat clear
            local name "`f'"
            local name = subinstr("`name'", ".trs", "", 1)
            save "${datasets_path}/`i'/`name'.dta", replace
        }
    }
    /*
    else {
        local filename     : dir "${rawdata_path}/`i'" files "*.txt"
        foreach f in `filename'{
            di "`f'"
            cd "${rawdata_path}/`i'"
            import delimited "`f'", delimiter(";", asstring) bindquote(nobind) case(preserve) asfloat clear
            local name "`f'"
            local name = subinstr("`name'", ".txt", "", 1)
            save "${datasets_path}/`i'/`name'.dta", replace
        }
    }
    */
}