Announcement

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

  • Help_Line of code that searches all folders in a given directory

    Dear all,

    Basically, what I have is a small database. A folder that contains many folders inside (each with some dta files). The code that I have it performs some adjustment for each file and saves this file in the same folder, and it does that for each file in a folder.

    However, I only specified that a code works only with a particular folder and I need so that it goes to every folder automatically and does the procedure that already works.

    The line of code I used for single folder is:

    local files = ""
    local files : dir "${path}" files "*.dta"
    display `"`files'"'

    I suppose that to make it search every single folder in a given directory, I need to start with smh like

    foreach folder in folderlist {

    local files = ""
    local files : dir "${path}" files "*.dta"

    display `"`files'"'




    I would appreciate your help on restructuring a code in a way that it would work with all folders in a directory



    Last edited by Myron Merton; 28 Aug 2018, 04:59.

  • #2
    Something like the following may be a good starting point, assuming your directories go just one level deep.
    Code:
    local dirs : dir "${path}" dirs 
    display `"`dirs'"'
    foreach d of local dirs {
        local files : dir "${path}/`d'" files "*.dta"
        display `"`files'"'
        foreach f of local files {
            use "${path}/`d'/`f'", clear
            ...
        }
    }
    With that said, please take the time to review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post. While you are there, follow the link on the left to the extra advice and read the section on bumping.

    Statalist is a worldwide forum, and not all members are active when you are.

    Comment


    • #3
      Within one hour you bump your post, and send two direct messages asking for help with this thing?
      I know I helped you in the past, but if the reward for that is a bunch of spam, than I know better than to help again.
      You're on my ignore list from now on.

      Comment


      • #4
        When I gave the advice in post #2 I was unaware of Jorrit's experience. I'd add to the advice in post #2 that you also read the section on private messages that follows the section on bumping.

        Also, I see that you deleted your bump post that was originally post #2 in this topic, posted an hour after your original post, as Jorrit wrote. Having been called out on it, there was no point in deleting it, it doesn't undo the effects. Let me add, on the topic of bumping, that there are members who look first for topics that have only the single initial post to see if they can help where others have not. By bumping your post, it increases the count from 1 to 2, and I for one am more likely to assume it has gotten a response.

        And in your initial topic, I see now that you subsequently deleted the post where you responded to my question about your initial post. That was not appropriate.

        Do read the Statalist FAQ as well as the additional material. Learn what you can do so that others want to help you with your problems. Learn about CODE delimiters to make your presentation clearer.

        Comment


        • #5
          Note that filelist (from SSC) will recursively search all sub-directories and create a dataset with the path, filename, and file size for each file found. You can then loop over each file or use runby (from SSC) to process the list.

          Comment

          Working...
          X