如何使用PowerShell将本地文件和路径复制到网络共享?

问题描述

我正在使用PowerShell,通过管道从本地驱动器中传输文件列表,以便复制项可以将它们复制到网络文件夹中。以下语句确实找到了我需要的文件,并将其复制。但是,所有路径信息都将丢失,并且它们最终都位于同一文件夹中。我希望每个文件都以相对路径的方式保存在目标位置,并且文件的路径来自本地磁盘。

例如,应将“ c:\ users \ user_name \ Documents \ file_1.txt”复制到“ \\ server \ share \ my_folder \ users \ user_name \ Documents \ file_1.txt”

我的搜索结果无济于事。可以修改此语句来实现这一点吗?

Get-ChildItem -Path c:\users\user_name -r | ? {!($_.psiscontainer) -AND $_.lastwritetime -gt (get-date).date} | %{copy-Item -Path $_.fullname -destination "\\server\share\my_folder" -Recurse -Force -Container}

编辑:

似乎copy-Item应该能够做到这一点。以下命令有效:

copy-Item -Path c:\users\user_name\Documents -Destination "\\server\share\my_folder" -recurse -Force

此命令将复制c:\ users \ user_name \ Documents下的所有文件,子文件夹和子文件夹中的文件。然后,它在网络共享上重新创建相对目录结构。似乎第一个命令的$ _。fullname参数没有以类似的方式处理。我当然不是以预期的方式将文件列表传递给copy-Item。还有其他建议吗?

解决方法

我们应该做一些事情。首先,如果只需要文件,则可以删除!$ _。psiscontainer检查,然后将-file添加到Get-Childitem命令中。接下来,如果您打算复制早于Get-Date的文件,则比较需要为-lt。至于在目标位置创建目录结构,则需要自己创建。您可以通过从路径上剥离c:并使用*-Path cmdlet这样来实现。

$source      = 'c:\users\user_name\Documents'
$destination = '\\server\share\my_folder'

Get-ChildItem -Path $source -File -Recurse |
    Where-Object {$_.lastwritetime -lt (get-date).date} |
        ForEach-Object {
            $destdir = Join-Path $destination (Split-Path $_.FullName -Parent).Substring(2)
            
            If(-not(Test-Path $destdir))
            {
                Write-Host Making directory $destdir -ForegroundColor Cyan
                $null = New-Item -Path $destdir -ItemType Directory
            }
            
            Write-Host Copying file $_.name to $destdir -ForegroundColor Green
            Copy-Item $_.FullName -Destination $destdir
        }

但是,如Bill_Stewart所说的robocopy或其他工具可能更合适。 Robocopy肯定会快得多,因此,如果您需要使用它来处理大量数据,我会朝着这个方向努力。

,

如注释中所述,使用Set-Location(请参阅:Get relative path of files in sub-folders from the current directory):

Set-Location c:\users\user_name
Get-ChildItem -Recurse |
  Where-Object { !($_.psiscontainer) -AND $_.lastwritetime -gt (get-date).date } |
    Foreach-Object {
        $Destination = Join-Path \\server\share\my_folder ("$_" | Resolve-Path -Relative)
        New-Item -ItemType File -Path $destination -Force
        Copy-Item -Path "$_" -Destination $Destination -Force
    }
,

好的,这确实是个挑战,但是我终于找到了一种机制。感谢@iRob为我指出一个潜在的解决方案。我无法获得他的代码来在目标位置创建相对路径,但是能够扩展他的概念。我最终添加了一个New-Item命令(通过Powershell 2 copy-item which creates a folder if doesn't exist找到)和一个Split-Path(在这里Copy a list of files to a directory找到)。 Split-Path从联接路径中切出文件名,然后通过New-Item命令创建该路径。最后,Copy-Item可以复制文件。因此,从这个意义上说,@ Doug Maurer也是正确的,Copy-Item本身不会这样做。要成功运行Copy-Item命令,还需要做一些额外的准备。

Set-Location c:\users\user_name; Get-ChildItem -Recurse | ? { !($_.psiscontainer) -AND $_.lastwritetime -gt (get-date).date } | % {Copy-Item $_.fullname -destination (New-Item -type directory -force ((Join-Path '\\server\share\my_folder' ($_ | Resolve-Path -Relative)) | Split-Path -Parent)) -Force}

恕我直言,在MS-DOS 6.0(xcopy / D:08-20-2020 / S c:\ users \ user_name *。* \ server \ share \ my_folder)中琐碎的事情在PowerShell中应该不会那么困难。似乎应该有一个更简单的方法。现在,我想起来了,可能应该坚持使用xcopy而不是深入研究这个兔子洞。 PowerShell看起来是如此闪亮。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...