Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Appending Files with Similar Name but From Different Folder

    I have two folders with exactly similar file names inside them. I would like for these files to be appended with each other.

    For example, interview__33 from Folder v2 should be appended with interview__33 from Folder v3.

    I used this codes:

    global v3 "C:\Users\lm2\Desktop\Raw Files\20190816"
    global v2 "C:\Users\lm2\Desktop\Raw Files\v2"
    global temp ""C:\Users\lm2\Desktop\Raw Files\20190816\temp"

    cd "$v2"
    local dta: dir . files "*.dta"
    foreach f of local dta {
    use "`f'", clear
    append using "$v3\`f'"
    save "$temp\`f'", replace
    }

    The error is: r(601);
    File C:\Users\lm2\Desktop\Raw Files\20190816 not found

  • #2
    Beware the backstabbing backslash:
    https://www.stata-journal.com/sjpdf....iclenum=pr0042

    Can be fixed with cd-ing into the right folder at each point, roughly:

    Code:
    cd "$v2"
    local dta: dir . files "*.dta"
    foreach f of local dta {
    cd "$v2"
    use "`f'", clear
    cd "$v3"
    append using "`f'"
    cd "$temp"
    save "`f'", replace
    }

    Comment


    • #3
      Adding to Jorrit's advice, do note the next-to-final paragraph of the linked article, which discusses the particular issue of file paths on Windows systems, and Stata's ability to use forward slashes in file paths on Windows systems.

      In your example code from post #1, replacing the append and save commands with
      Code:
      append using "$v3/`f'"
      save "$temp/`f'", replace
      will avoid the problem.

      Comment

      Working...
      X