简体   繁体   English

移动 windows 上的多个特定文件夹

[英]Moving multiple specific folders on windows

I have a list of multiple specific directories that I would like to move to another folder on Windows. The list is stored in a txt.file which contains the paths of these directories:我有一个包含多个特定目录的列表,我想将它们移动到 Windows 上的另一个文件夹。该列表存储在包含这些目录路径的 txt.file 中:

"C:/Path/to/my/folder1/"
"C:/Path/to/my/folder4/"
"C:/Path/to/my/folder9/"

I would like to move all these folders including there subfolders to a new directory:我想将所有这些文件夹(包括子文件夹)移动到新目录:

C:/Path/to/new/directory/

Is there a smart way to do this by powershell, python or R on windows? powershell、python 或 windows 上的 R 是否有聪明的方法来做到这一点?

Edit (Sorry, didn't include my first attempts initially):编辑(抱歉,最初不包括我的第一次尝试):

I had previously tried using the " file.copy(list_of_paths, target_path )" command in R, but it always returned "FALSE FALSE".我之前曾尝试在 R 中使用“ file.copy(list_of_paths, target_path )”命令,但它总是返回“FALSE FALSE”。 If I understood correctly, it seems that R on windows has difficulties handling folders rather than files (I'm used to unix and haven't come across that issue before).如果我理解正确的话,windows 上的 R 似乎在处理文件夹而不是文件时遇到困难(我习惯了 unix,之前没有遇到过这个问题)。

The other thing I tried was using the Copy-Item command on Powershell ( Get-ChildItem -Path 'C:\Path\of\files' -Recurse| Copy-Item -Destination 'C:\Path\to\target' ), but I'm not sure how to load a list of paths in Powershell.我尝试的另一件事是在 Powershell 上使用 Copy-Item 命令( Get-ChildItem -Path 'C:\Path\of\files' -Recurse| Copy-Item -Destination 'C:\Path\to\target' ),但我不确定如何加载 Powershell 中的路径列表。

Thanks so much!非常感谢!

这是使用 R 的解决方案。假设您的文件路径存储在列表fpath

file.copy(fpath, "C:/Path/to/new/directory/")

Alternate Solution(s)替代解决方案

The previous answer provides a solution in R for copying recursively the folders from source to target.上一个答案在R中提供了一个解决方案,用于将文件夹从源递归复制到目标。

Per your question, you need to move the folders and maintain their relative sub-folder structure from the root path.根据您的问题,您需要移动文件夹并从根路径维护它们的相对子文件夹结构。


I know that's not a big deal because you can simply copy recursively over the folders and delete originals, however, in general moving is much faster than copying and it's what you asked for so here's what I've got.我知道这没什么大不了的,因为您可以简单地在文件夹上递归复制并删除原件,但是,一般来说,移动比复制快得多,这就是您所要求的,所以这就是我所拥有的。


I propose the following solutions:我提出以下解决方案:

PowerShell Solution PowerShell 解决方案

The simplest solution is to simply run:最简单的解决方案是简单地运行:

# showing different ways of specifying paths
$fromDirs = @( 
    ".\folder1"
    "C:\Path\to\my\folder2\"
    "$env:USERPROFILE\Documents\TestDir"
)

$destDir = "$HOME\Desktop\"

ForEach ($dir in $fromDirs) {
    Move-Item -Path $dir -Destination $destDir -Force
}

Another, more involved PowerShell solution is as follows:另一种,涉及较多的PowerShell解决方案如下:

$fromDirs = @(
    "C:\Path\to\my\folder1"
    "C:\Path\to\my\folder2\"
    "C:\Path\to\my\folder3\"
)

$toDir = "C:\Path\to\my\destination\"

$fromDirs | ForEach-Object {
    $fromDir = $_
    $Files = Get-ChildItem -Path $fromDir -Recurse -File
    $Files | ForEach-Object {
        $File = $_
        $RelativePath = $File.FullName.Replace($fromDir, '')
        $Destination = Join-Path -Path $toDir -ChildPath $RelativePath
        $DestinationDir = Split-Path -Path $Destination -Parent
        if (-not (Test-Path -Path $DestinationDir)) {
            New-Item -Path $DestinationDir -ItemType Directory -Force
        }
        Move-Item -Path $File.FullName -Destination $Destination -Force
    }
}

this more advanced solution deals with the relativistic hierarchy issues when moving recursive paths.这个更高级的解决方案处理移动递归路径时的相对论层次问题。

Relative Folder Hierarchy相对文件夹层次结构

In order to maintain relative path consistency (ie when moving the folders and subfolders, you will need to create the relative path structure first before moving/copying the files over), a little more advanced solution is necessary than simply running Move-Item -Path $fromDir -Destination $toDir -Force since the Move-Item cmdlet does not support recursion (and it shouldn't for various reasons).为了保持相对路径的一致性(即移动文件夹和子文件夹时,您需要先创建相对路径结构,然后再移动/复制文件),需要比简单运行Move-Item -Path $fromDir -Destination $toDir -Force更高级的解决方案Move-Item -Path $fromDir -Destination $toDir -Force因为Move-Item cmdlet 不支持递归(出于各种原因不应该支持)。

R Solution R 解决方案

In R, I would go with fs::dir_copy() over base R (a practice I usually avoid) due to its file system management practices in Windows and its dir_copy() function being more robust than base R file.copy() in this scenario.在 R 中,我将使用fs::dir_copy()的 go 覆盖基数 R(我通常会避免这种做法),因为它在 Windows 中的文件系统管理实践及其dir_copy() file.copy()比基数 8814366825 中的文件系统管理实践更强大这种情况。

require(fs)
fs::dir_copy(c("folder1", "folder2"), "DestinationFolder")

But to address the topic of moving instead of copying the best solution in R is to use base R's file.rename() function.但是要解决移动而不是复制的主题,R 中的最佳解决方案是使用 base R 的file.rename() function。

# this moves a directory from one location to another:
file.rename(folder_old_path, path_new)

for multiple directories with subdirectories:对于具有子目录的多个目录:

to <- "todir"
froms <- c("dir1", "dir2")
tos <- paste0(to, "/", froms)

file.rename(froms, tos)

will result in "dir1" and "dir2" moving to "todir/dir1/ " and "todir/dir2/ ".将导致“dir1”和“dir2”移动到“todir/dir1/ ”和“todir/dir2/ ”。

Note that if "todir" doesn't exist you will need to check that first via if (dir.exists(to)) {... }请注意,如果“todir”不存在,您需要先通过if (dir.exists(to)) {... }


Copying vs. Moving复制与移动

Just like on UNIX, copying is used to copy from one place to another while moving is used to move a file or folder.和UNIX一样,复制是从一个地方复制到另一个地方,移动是移动一个文件或文件夹。 Moving will not have a flag for recursion (ie no -r flag) because it will automatically move all sub-folders and files to the specified destination's path.移动不会有递归标志(即没有-r标志),因为它会自动将所有子文件夹和文件移动到指定目标的路径。 Copying, however, allows you to specify the recursion option to copy directories recursively.但是,复制允许您指定递归选项以递归地复制目录。 Lastly, be wary of overwriting pre-existing files in the destination path.最后,要小心覆盖目标路径中预先存在的文件。

Also, if you are on Windows you should use the correct path separators ( \ instead of / ; or to be safe just go with double \\ ).此外,如果您在 Windows 上,则应使用正确的路径分隔符( \而不是/ ;或者为了安全起见,只需使用双\\的 go )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM