# mv
移动或重命名文件或文件夹
# 语法
mv(source, destination)
# 说明
mv(source, destination) 将 source 文件或文件夹移动到 destination。如果 source 和 destination 位于同一位置,则 mv 会将 source 重命名为 destination。要在移动文件或文件夹时对其重命名,需将 destination 设置为与 source 不同的名称,且只能为 source 指定一个文件或文件夹。
mv(source, destination; force),force 默认为false,force为true时,会先删除已存在的destination,然后将source移动到目标位置。
如果 source 是文件,则 destination 是文件。
如果 source 是文件夹,则 destination 必须是文件夹。
如果 source 是一个文件夹, 或指定了多个文件,而 destination 不存在,代码会报错,destination 必须是已经存在的文件或文件夹。示例
# 示例
将文件移动到当前文件夹
获取当前文件夹,将文件和文件夹移动到当前文件夹。
创建两个文件夹:第一个 myfiles,包含文件 myfile1.m,第二个 myotherfiles,包含文件 myfile2.m。将 myfile1.m 移动到当前文件夹。
using TyBase
mvdir = homedir() * "/movefile"
dir_path = pkgdir(TyBase) * "/examples/FileSystem/movefile"
mkpath(mvdir)
cp(dir_path * "/myfile1.m", mvdir * "/myfile1.m")
cp(dir_path * "/myfile2.m", mvdir * "/myfile2.m")
cd(dir_path)
myfilesdir = mvdir * "/myfiles"
mkpath(myfilesdir)
mv(mvdir * "/myfile1.m", myfilesdir * "/myfile1.m")
myotherfilesdir = mvdir * "/myotherfiles"
mkpath(myotherfilesdir)
mv(mvdir * "/myfile2.m", myotherfilesdir * "/myfile2.m")
将当前文件夹设置为 myfiles。将 myotherfiles 及其内容移动到当前文件夹。
cd(myfilesdir)
nowloc = pwd()
mv("../myotherfiles", nowloc * "/myotherfiles/")
cd(dir_path)
rm(mvdir; recursive=true)
将文件和文件夹移动到新文件夹
将名称以 my 开头的文件和子文件夹从当前文件夹移动到文件夹 newFolder(newFolder 之前并不存在)。
using TyBase
mvdir = homedir() * "/movefile"
oldpwd = pkgdir(TyBase) * "/examples/FileSystem/movefile"
mkpath(mvdir)
cp(oldpwd * "/myfile1.m", mvdir * "/myfile1.m")
cp(oldpwd * "/myfile2.m", mvdir * "/myfile2.m")
cd(oldpwd)
alls = filter(x -> occursin(r"^my", x), readdir(mvdir))
!isdir(mvdir * "newFolder") && mkdir(mvdir * "/newFolder")
try
for f in alls
mv(f, joinpath("newFolder", f))
end
catch
ErrorException("移动文件失败,请检查文件是否被占用!")
end
rm(mvdir; recursive=true)
重命名文件夹
创建文件夹 myoldfolder,然后将其重命名为 mynewfolder。
mvdir = homedir() * "/movefile"
mkpath(mvdir * "/myoldfolder")
mv(mvdir * "/myoldfolder", mvdir * "/mynewfolder")
rm(mvdir; recursive=true)
将文件移动到只读文件夹
将文件 myfile1.m 从当前文件夹移动到只读文件夹 restricted。
创建只读文件夹 restricted。
using TyBase
mvdir = homedir() * "/movefile"
mkpath(mvdir * "/restricted")
chmod(mvdir * "/restricted", 0o444)
移动文件 myfile1.m,以覆盖目标文件夹的只读状态。
dir_path = pkgdir(TyBase) * "/examples/FileSystem/movefile"
mkpath(mvdir)
cp(dir_path * "/myfile1.m", mvdir * "/myfile1.m")
status, message, messageId =
try
mv(mvdir * "/myfile1.m", mvdir * "/restricted/myfile1.m")
[1, "", ""]
catch e
[0, e.msg, e.code]
end
chmod(mvdir * "/restricted", 0o577)
rm(mvdir, recursive=true)
status = 0
message = "open(\"restricted/myfile1.m\", 769, 33206): permission denied (EACCES)"
messageId = -4092
# 输入参数
source - 要移动的文件或文件夹字符串标量
要移动的文件或文件夹,指定为字符串标量。
在移动本地文件或文件夹时,source 可以是绝对路径或相对路径。不过,要移动远程位置的文件和文件夹,source 必须包含指定为统一资源定位器 (URL) 的完整路径。
注意
如果 source 是字符串,请将所有输入都括在括号中。例如,mv("myoldfolder", "mynewfolder")。
destination - 文件或文件夹目标字符串标量
文件或文件夹目标,指定为字符串标量。
如果 destination 是本地位置,则可以将其指定为绝对路径或相对路径。如果 destination 是远程位置,它必须包含指定为 URL 的完整路径。
注意
如果 destination 是字符串,请将所有输入都括在括号中。例如,mv("myoldfolder", "mynewfolder")。