*! cdlist.ado v0.5.1 Scott Long 2017-08-09
* cdlist.ado v0.5.0 Scott Long 2017-07-30 // links to commands
* cdlist.ado v0.4.0 Scott Long 2017-07-30
// changewd: list cd* macros in PERSONAL directory
capture program drop cdlist
program define cdlist
    version 13.0
    syntax [ , Details DIRectories]

    * names of cd*.ado file in PERASONALs
    local cdsavefiles : dir "`c(sysdir_personal)'" files "cd*.ado"

    * length of command names
    local cmdwidth = 0
    foreach file in `cdsavefiles' {
        local filelen = strlen("`file'")
        if `filelen'>`cmdwidth' local cmdwidth = `filelen'
    }
    local cmdwidth = `cmdwidth' - 3 // for .ado

    * later restore this as the working directory
    local currentpwd `"`c(pwd)'"'

    * loop through cd commands
    display
    foreach file in `cdsavefiles' {
        local cmd : subinstr local file ".ado" "", all
        capture quietly `cmd'
        local wdmissing = 0 // cd return error code
        if _rc!=0 {
            local wdmissing = 1
        }
        if "`details'"=="details" {
            * OLD di as result "`cmd'"
            di `"{stata `cmd':`cmd'}"'
            cdinfo
            if `wdmissing' {
                di as error _col(15) "working directory not found"
            }
        }
        else {
            local note `"$S_cdnote"'
            if ("`note'"=="" | "`directories'"=="directories") ///
                local note `"$S_cdwd"'
            local nspaces = `cmdwidth'-strlen("`cmd'")
            di `"{space `nspaces'}{stata `cmd':`cmd'} - `note'"' _continue
            if `wdmissing' {
                local colloc = `cmdwidth' + 2
                di as error " (working directory not found)" ///
                as result ""
            }
            else di
        }
    } // file loop
    if "`details'"=="" {
        di _new as text "Run " ///
        as result "cdlist, details" ///
        as text " for list of " ///
        as result "S_cd*" ///
        as text " macros"
    }

    * restore starting working directory
    qui cd `"`currentpwd'"'

end
exit

 di `"{p 0 23}{space 13}{stata c wfwork:wfwork} {txt}d:\Dropbox\active\wf icpsr 2017\Distribute - lectures\work{p_end}"'

*! cdlist.ado v0.4.0 Scott Long 2017-07-30

* cdlist.ado v0.3.0 Scott Long 2017-07-30 add cdinfo
* cdlist.ado v0.2.0 Scott Long 2017-07-27
cdlist
cdlist, details