I want to copy all files from a folder to another one in Stata.
I have used the following code:
local dlist: dir "$dir" dirs "*"
foreach d of local dlist {
local file: dir "$dir\"`d'"" files "*.dta"
foreach f of local file{
copy `f' "$dir/PROGRAMMATION/INITIALES"
}
}
However, Stata returns:
invalid syntax
$dir
is the actual directory where this do-file is located.
The following works for me:
global dir /Users/monkey/Downloads
local dlist: dir "$dir" dir "*"
foreach d of local dlist {
local file: dir "$dir/`d'" files "*.dta"
foreach f of local file {
copy "$dir/`d'/`f'" "/Users/monkey/testdir/`f'"
}
}
Note that the code will copy
all Stata dataset files contained in every sub-directory of /Users/monkey/Downloads
to the directory /Users/monkey/testdir/
.
If you just want to copy
all Stata dataset files from /Users/monkey/Downloads
to /Users/monkey/testdir/
one loop will suffice:
global dir /Users/monkey/Downloads
local file: dir "$dir" files "*.dta"
foreach f of local file{
copy "$dir`f'" "/Users/monkey/testdir/`f'"
}