如何使用 PowerShell 将本地文件和路径复制到网络共享?
How to copy local files AND PATHS to network share using PowerShell?
我正在尝试使用 PowerShell 从我的本地驱动器传输文件列表,以便 Copy-Items 可以将它们复制到网络文件夹。下面的语句确实找到了我需要的文件,并复制了它们。但是,所有路径信息都丢失了,它们最终都在同一个文件夹中。我希望每个文件都以相对路径的方式保存在目的地,其文件路径来自本地磁盘。
因此,例如“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
(see: 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) and a Split-Path (found here 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 看起来很闪亮。
我正在尝试使用 PowerShell 从我的本地驱动器传输文件列表,以便 Copy-Items 可以将它们复制到网络文件夹。下面的语句确实找到了我需要的文件,并复制了它们。但是,所有路径信息都丢失了,它们最终都在同一个文件夹中。我希望每个文件都以相对路径的方式保存在目的地,其文件路径来自本地磁盘。
因此,例如“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
(see: 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) and a Split-Path (found here 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 看起来很闪亮。