文件终端命令是否包含 /* - 我怎样才能防止它注释掉进一步的工作?
Do file terminal command contains /* - How can I prevent this from commenting out further work?
所以,我知道 mv
更适合移动文件,所以在 Stata 中,我想将名称中包含特定年份的所有文件移动到它们自己的目录中。但是,当我在 !
终端命令中包含 /*
时,它会注释掉我在 do 文件中的其余工作。有什么办法可以解决这个问题吗?
这里是有问题的部分:
forvalues i = 2010/2013 {
!mkdir ~/Documents/Thesis/Data/EIA_AMI/Test/`i'
!mv ~/Documents/Thesis/Data/EIA_AMI/Test/*`i'* ~/Documents/Thesis/Data/EIA_AMI/Test/`i'/
}
如果用引号引起来不起作用,请尝试使用 char()
函数的 ASCII 代码。
我机器上的一个工作示例是:
clear all
set more off
forvalues i = 2010/2010 {
!mkdir ~/Desktop/test/`i'
!mv /home/roberto/Desktop/test/`=char(42)'`i'`=char(42)'.txt /home/roberto/Desktop/test/`i'/
}
和
结果是:
一个例子运行:
. local i = 2010
. // initial state
. !ls ~/Desktop/test
test2010test.txt
. // move
. !mkdir ~/Desktop/test/`i'
. !mv ~/Desktop/test/`=char(42)'`i'`=char(42)'.txt ~/Desktop/test/`i'/
. // final state
. !ls ~/Desktop/test
2010
. !ls ~/Desktop/test/2010
test2010test.txt
您可能会发现一些运气,将文件路径存储在本地 引号 中,然后在 mv
调用中评估该本地:
. do move.do
. ls
move.do
testDir/
testFile1.txt
testFile2.md
. local cmd "./*.txt testDir/"
. !mv `cmd'
. ls
move.do
testDir/
testFile2.md
. ls testDir/
testFile1.txt
有趣的是,如果您不通过本地,/*
会被解释为评论。我相信对 Stata 有更好了解的人可以解释为什么,但我不能。
. !mv "./*.md testDir/"
mv: missing destination file operand after `./*.md testDir/'
Try `mv --help' for more information.
. ls
move.do
testDir/
testFile2.md
. ls testDir/
testFile1.txt
end of do-file
另一种方法是使用 filelist
(来自 SSC)创建文件数据集,以在 Stata 中管理和维护所有文件。下面的示例假设在 Stata 的当前目录中有一个名为 "from" 的目录,其中包含一堆名称中带有年份的文件。
. filelist, dir(from) norecur
Number of files found = 6
. list
+--------------------------------------+
| dirname filename fsize |
|--------------------------------------|
1. | from test2010test 3.txt 8,540 |
2. | from test2010test.txt 17 |
3. | from test2010test2.txt 9 |
4. | from test2011test 3.txt 5 |
5. | from test2011test.txt 3 |
|--------------------------------------|
6. | from test2011test2.txt 3 |
+--------------------------------------+
下面的代码将在当前目录和以年份命名的子目录中创建一个名为 "years" 的新目录。通过仅使用相对目录和 Stata 命令,您可以使代码更具可移植性。当然,下面可以修改为使用shell命令来处理每个文件。
* filelist if from SSC
filelist, dir(from) norecur
* extract the year from the file name
gen year = regexs(1) if regexm(filename,"([0-9][0-9][0-9][0-9])")
assert !mi(year)
* automatically discover years
levelsof year, clean
local years `r(levels)'
* create destination directories if they don't alread exist
cap mkdir "years"
foreach y of local years {
cap mkdir "years/`y'"
}
* use Stata commands to copy the files one by one.
local obs = _N
forvalues i = 1/`obs' {
local fname = filename[`i']
local dname = dirname[`i']
local from = "`dname'/`fname'"
local year = year[`i']
local to = "years/`year'/`fname'"
copy "`from'" "`to'", replace
rm "`from'"
}
经过运行上面的例子,我得到
. filelist , dir(years)
Number of files found = 6
. list
+-----------------------------------------+
| dirname filename fsize |
|-----------------------------------------|
1. | years/2010 test2010test 3.txt 8,540 |
2. | years/2010 test2010test.txt 17 |
3. | years/2010 test2010test2.txt 9 |
4. | years/2011 test2011test 3.txt 5 |
5. | years/2011 test2011test.txt 3 |
|-----------------------------------------|
6. | years/2011 test2011test2.txt 3 |
+-----------------------------------------+
所以,我知道 mv
更适合移动文件,所以在 Stata 中,我想将名称中包含特定年份的所有文件移动到它们自己的目录中。但是,当我在 !
终端命令中包含 /*
时,它会注释掉我在 do 文件中的其余工作。有什么办法可以解决这个问题吗?
这里是有问题的部分:
forvalues i = 2010/2013 {
!mkdir ~/Documents/Thesis/Data/EIA_AMI/Test/`i'
!mv ~/Documents/Thesis/Data/EIA_AMI/Test/*`i'* ~/Documents/Thesis/Data/EIA_AMI/Test/`i'/
}
如果用引号引起来不起作用,请尝试使用 char()
函数的 ASCII 代码。
我机器上的一个工作示例是:
clear all
set more off
forvalues i = 2010/2010 {
!mkdir ~/Desktop/test/`i'
!mv /home/roberto/Desktop/test/`=char(42)'`i'`=char(42)'.txt /home/roberto/Desktop/test/`i'/
}
和
结果是:
一个例子运行:
. local i = 2010
. // initial state
. !ls ~/Desktop/test
test2010test.txt
. // move
. !mkdir ~/Desktop/test/`i'
. !mv ~/Desktop/test/`=char(42)'`i'`=char(42)'.txt ~/Desktop/test/`i'/
. // final state
. !ls ~/Desktop/test
2010
. !ls ~/Desktop/test/2010
test2010test.txt
您可能会发现一些运气,将文件路径存储在本地 引号 中,然后在 mv
调用中评估该本地:
. do move.do
. ls
move.do
testDir/
testFile1.txt
testFile2.md
. local cmd "./*.txt testDir/"
. !mv `cmd'
. ls
move.do
testDir/
testFile2.md
. ls testDir/
testFile1.txt
有趣的是,如果您不通过本地,/*
会被解释为评论。我相信对 Stata 有更好了解的人可以解释为什么,但我不能。
. !mv "./*.md testDir/"
mv: missing destination file operand after `./*.md testDir/'
Try `mv --help' for more information.
. ls
move.do
testDir/
testFile2.md
. ls testDir/
testFile1.txt
end of do-file
另一种方法是使用 filelist
(来自 SSC)创建文件数据集,以在 Stata 中管理和维护所有文件。下面的示例假设在 Stata 的当前目录中有一个名为 "from" 的目录,其中包含一堆名称中带有年份的文件。
. filelist, dir(from) norecur
Number of files found = 6
. list
+--------------------------------------+
| dirname filename fsize |
|--------------------------------------|
1. | from test2010test 3.txt 8,540 |
2. | from test2010test.txt 17 |
3. | from test2010test2.txt 9 |
4. | from test2011test 3.txt 5 |
5. | from test2011test.txt 3 |
|--------------------------------------|
6. | from test2011test2.txt 3 |
+--------------------------------------+
下面的代码将在当前目录和以年份命名的子目录中创建一个名为 "years" 的新目录。通过仅使用相对目录和 Stata 命令,您可以使代码更具可移植性。当然,下面可以修改为使用shell命令来处理每个文件。
* filelist if from SSC
filelist, dir(from) norecur
* extract the year from the file name
gen year = regexs(1) if regexm(filename,"([0-9][0-9][0-9][0-9])")
assert !mi(year)
* automatically discover years
levelsof year, clean
local years `r(levels)'
* create destination directories if they don't alread exist
cap mkdir "years"
foreach y of local years {
cap mkdir "years/`y'"
}
* use Stata commands to copy the files one by one.
local obs = _N
forvalues i = 1/`obs' {
local fname = filename[`i']
local dname = dirname[`i']
local from = "`dname'/`fname'"
local year = year[`i']
local to = "years/`year'/`fname'"
copy "`from'" "`to'", replace
rm "`from'"
}
经过运行上面的例子,我得到
. filelist , dir(years)
Number of files found = 6
. list
+-----------------------------------------+
| dirname filename fsize |
|-----------------------------------------|
1. | years/2010 test2010test 3.txt 8,540 |
2. | years/2010 test2010test.txt 17 |
3. | years/2010 test2010test2.txt 9 |
4. | years/2011 test2011test 3.txt 5 |
5. | years/2011 test2011test.txt 3 |
|-----------------------------------------|
6. | years/2011 test2011test2.txt 3 |
+-----------------------------------------+