How to acquire complete list of subdirs (including subdirs of subdirs)?

蹲街弑〆低调 提交于 2019-12-02 08:00:41

Although not a solution to whatever problem you're actually presenting, an easier way might be to use filelist, from SSC (ssc install filelist).

An example might be:

. // list all files
. filelist, directory("D:\Datos\RFERRER\Desktop\example")
Number of files found = 5

. 
. // strange way of tagging directories ending in "\house"
. // change at will
. gen tag = substr(reverse(dirname),1,6) == "esuoh/"

. 
. order tag

. list

     +----------------------------------------------------------------------------------------------+
     | tag   dirname                                                     filename             fsize |
     |----------------------------------------------------------------------------------------------|
  1. |   0   D:\Datos\RFERRER\Desktop\example/proj_1                     newfile.txt              0 |
  2. |   1   D:\Datos\RFERRER\Desktop\example/proj_2/house               somefile.txt             0 |
  3. |   0   D:\Datos\RFERRER\Desktop\example/proj_3/subproj_3_2         newfile2.txt             0 |
  4. |   1   D:\Datos\RFERRER\Desktop\example/proj_3/subproj_3_2/house   anothernewfile.txt       0 |
  5. |   1   D:\Datos\RFERRER\Desktop\example/proj_3/subproj_3_2/house   someotherfile.txt        0 |
     +----------------------------------------------------------------------------------------------+

Afterwards, use keep or drop, conditional on variable tag.

Graphically, the directory looks like:

(I'm on Stata 13. Check help string functions for other ways to tag.)

Your revised problem may yield to

local folder: dir . dirs "*"
foreach i of local folder {
     di "`i'"
     local house : dir  "G:\Data_backup\Soufang_data/`i'\house" files "*.xlsx"

     foreach j of local house {
        di "`j'"
     }
}

but clearly we can't see your file structure or file names.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!