In-script command to copy all do files in the current directory to another folder in Stata

筅森魡賤 提交于 2019-12-23 04:11:09

问题


The following is the copy command.

copy "main.do" "main2.do", replace

However, I want to copy all do files in the current directory, and paste to another directory. I hope this is done without me needing to specify the file names of each do file. How is this possible?

From link https://www.stata.com/statalist/archive/2006-08/msg00620.html

I tried

cd "C:\Users\Owner\Google Drive\test"
local files : dir "`c(pwd)'" files "*.do"
foreach file in `files' {
    copy `file' "`file'_copied"
}

so I could at least copy within the current folder. But I still don't know how to copy all files to another folder.

======================

Second solution.

Can I just get the filename of the current do-file, and then copy this do file into another folder?

This http://www.statalist.org/forums/forum/general-stata-discussion/general/1297295-obtain-name-of-current-do-file

talks about something, but "c(tmpdir)" only obtains directory name.

==========================

I did more research and found this https://ideas.repec.org/c/boc/bocode/s457628.html


回答1:


Working backwards: the current do-file is not a clearly defined concept. Do-files can be nested, so Stata could start with a.do which calls b.do which calls c.do .... So, what's the current do-file? All of them.

As you nearly say, you can get a list of files of a certain kind in the current directory by (e.g.)

local filelist : dir . files "*.do" 

Copying to bar just requires you to mention bar, although it seems you want to change the filename too:

foreach f of local filelist { 
    local F : subinstr local f ".do" "2.do", all  
    copy `f'  bar/`F' 
} 

Using a forward slash is prudent even under Windows. See the discussion here and its references.

Useful functions here are documented online and correspondingly within Stata:

 help extended fcn 


来源:https://stackoverflow.com/questions/44102264/in-script-command-to-copy-all-do-files-in-the-current-directory-to-another-folde

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