Announcement

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

  • Moving entire folders to different directories

    Hello:
    I am trying to use the command -mvfiles- which seems to be a good way to move/copy files from one location to another in the local directory.
    Where I am trying to employ this is both in moving bulk files as well as many folders that contain files within.


    My two main questions where I'm having trouble:
    1. I am trying to use -mvfiles- command in a loop where the match(“str”) component is looped through a varlist, as I have several name matches out of a large folder of other folders that I need to move. This does not seem to be working with the loop, but only if I manually input the match string.
    2. Does this command work with moving folders, rather than files, I am not sure I was able to get this to work with folders/directory that contain other files, but aren’t files themselves.

    Here are my data sets, and below that the code I've tried:

    This is the target directory containing multiple folders (that each contain files themselves) that I need to match by name and move to another directory:
    Code:
    clear
    input str18 filename
    "100-myfile858.type"
    "101-myfile55.type" 
    "102-myfile893.type"
    "103-myfile371.type"
    "104-myfile800.type"
    "111-myfile610.type"
    "112-myfile340.type"
    "113-myfile187.type"
    "114-myfile834.type"
    "115-myfile323.type"
    "116-myfile234.type"
    "121-myfile752.type"
    "122-myfile723.type"
    "123-myfile943.type"
    "125-myfile705.type"
    "126-myfile373.type"
    "127-myfile766.type"
    "130-myfile201.type"
    "131-myfile332.type"
    "135-myfile150.type"
    "136-myfile679.type"
    "137-myfile286.type"
    "140-myfile931.type"
    "141-myfile597.type"
    "142-myfile124.type"
    "143-myfile434.type"
    "151-myfile13.type" 
    "155-myfile691.type"
    "156-myfile364.type"
    "158-myfile89.type" 
    "160-myfile140.type"
    "161-myfile245.type"
    "165-myfile347.type"
    "171-myfile434.type"
    "175-myfile46.type" 
    "178-myfile96.type" 
    "179-myfile20.type" 
    "180-myfile337.type"
    "181-myfile15.type" 
    "190-myfile243.type"
    "191-myfile881.type"
    "200-myfile43.type" 
    "201-myfile925.type"
    "205-myfile254.type"
    "209-myfile719.type"
    "210-myfile510.type"
    "211-myfile458.type"
    "221-myfile609.type"
    end
    This is the list that contains the header numbers that I'll match so that only those folders whose names match in the target directory are moved:
    Code:
    clear
    input int id str3 id_str
    111 "111"
    121 "121"
    131 "131"
    141 "141"
    151 "151"
    161 "161"
    171 "171"
    181 "181"
    191 "191"
    211 "211"
    221 "221"
    end
    And here is the loop code I attempted that does not work:
    Code:
    foreach id of varlist id_str {
    mvfiles, infolder("C:\Me\Storage\mainfoldersource") outfolder("C:\Me\Otherstorage\matchedfoldernames") match("`id'*")
    }
    Note, when I run the code using an individual matching id, it works, just not with the loop:
    Code:
    mvfiles, infolder("C:\Me\Storage\mainfoldersource") outfolder("C:\Me\Otherstorage\matchedfoldernames") match("111*")
    But I have so many I need to go through that I am really trying to make a loop that works. Or if anyone else has better methods of doing this sort of work.
    Thanks!

  • #2
    Your -foreach- code is trying to iterate over a list of variables, and a list of variables that contains only one variable: id_str, at that. What you mean to do is iterate over the values of the variable id_str.

    So you want
    Code:
    levelsof id_str, local(ids)
    foreach id of local ids {
        mvfiles, infolder("C:\Me\Storage\mainfoldersource") ///
        outfolder("C:\Me\Otherstorage\matchedfoldernames") match("`id'*")
    }
    Note: I am not familiar with the program mvfiles, so if there is some additional problem with the syntax of that command, I'm not in a position to help you with that. Since your command worked fine outside of the loop and the problem with the loop was clear to me, I jumped in. But I can't help you if there's more to it than this.

    Comment

    Working...
    X