我正在尝试批次重命名操作系统上同一文件夹中的许多文件。
我有一个有两个变量的数据集:oldname和newname。
dataset中有更多的文件名,然后需要重命名实际的文件,因此我希望构建与数据集中的观察相匹配的文件,重命名匹配的文件,然后将其移动到新的位置。
例如,我在一个位置上有三个文件:
filepath/to/my/files/file1.jpg
filepath/to/my/files/file2.jpg
filepath/to/my/files/file3.jpg包含字符串变量的数据集:
Dataset
newname oldname
a pink files/file1.jpg
b blue files/file4.jpg
c purple files/file6.jpg
d green files/file3.jpg
e red files/file2.jpg下面是所需的程序输出:
filepath/for/matches/pink.jpg
filepath/for/matches/red.jpg
filepath/for/matches/green.jpg我是否需要使用!操作符来实现我想要的?
编辑:
到目前为止,我已经找到了一种移动火柴的方法,但不是重新命名它们的方法:
global dir "/filepath"
global old "$dir/to/my/"
global new "$dir/for/matches"
ssc install mvfiles
preserve
keep if oldname!=.
foreach i in oldname{
mvfiles, infolder($old) outfolder($new) match(substr(`i',6,9))
}
restore发布于 2018-07-19 21:12:00
不一定。只需使用copy命令就可以实现您想要的结果。
下列措施应能发挥作用:
clear
input str1 letter str10 newname str15 oldname
"a" "pink" "files/file1"
"b" "blue" "files/file4"
"c" "purple" "files/file6"
"d" "green" "files/file3"
"e" "red" "files/file2"
end
local inpath "filepath/to/my/files/"
local outpath "different/filepath/for/matches/"
local files : dir "`inpath'" files "*.jpg"
local obs = _N
foreach fil of local files {
forvalues i = 1 / `obs' {
local oldname = oldname[`i']
if substr("`fil'", 1, strpos("`fil'", ".") - 1) == substr("`oldname'", 7, .) {
local newname = newname[`i']
copy "`inpath'`fil'" " `outpath'`newname'.jpg"
}
}
}只需将本地宏inpath和outpath替换为所需的路径即可。
请注意,如果您也希望在复制文件后删除该文件,那么只需在copy命令之后添加以下内容:
erase "`inpath'`fil'"https://stackoverflow.com/questions/51429209
复制相似问题