6
\$\begingroup\$

I have a file directory which contains folders for every month, and within those folders are several text files. In this context I would like to copy all of the text files from my January folder into a separate target folder. Then copy all of the text files within the February folder to the target folder, and so on for each month. This needs to happen sequentially and I'm not sure if I'm doing the iteration properly. I need the code to be able to run on several machines, and installing libraries on each is not possible.

Am I on the right track with the following? I'm not sure if this is the right approach to take or how to flesh this out.

function listdirectory(directory)
    local i, t, popen = 0, {}, io.popen
    for filename in popen('dir "'..directory..'" /b /ad'):lines() do
        i = i + 1
        t[i] = filename
    end
    return t
end

os.execute(xcopy folder_month target_folder)
\$\endgroup\$
4
  • \$\begingroup\$ There is an extra end at the end. It it a function definition? \$\endgroup\$ – hjpotter92 Nov 7 '13 at 1:26
  • \$\begingroup\$ Sorry I forgot to include the function name. That is correct, I've updated the post to reflect that \$\endgroup\$ – MRCLE Nov 7 '13 at 1:29
  • \$\begingroup\$ Why do you need to do it file by file? A simple cp -r path/to/January new/path would work. \$\endgroup\$ – hjpotter92 Nov 7 '13 at 11:46
  • \$\begingroup\$ This script is a smaller part of a large process. When the text files are moved into the target folder there is a separate script reading them. When that completes this script will be called again and the next months files will be needed \$\endgroup\$ – MRCLE Nov 7 '13 at 13:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.