gpt4 book ai didi

PowerShell 将源文件夹中的文件复制到匹配目标失败并显示目标值

转载 作者:行者123 更新时间:2023-12-05 05:56:13 27 4
gpt4 key购买 nike

我正在尝试将一个文件夹中的文件复制到另一个文件夹。文件应位于其父文件夹下的相同文件夹结构中。例如,C:\Users\Boss\One\Two\Alpha\File1.txt应该去C:\Users\Boss\One\Two\Beta\File1.txt

它在我在 Copy-Item 中指定 Destination 值的最后一行失败了:

$sourcedir = "C:\Users\Boss\One\Two\Alpha"
$localdir = "C:\Users\Boss\One\Two\Beta"

# Assumes folders are established by other processes
$sourceContents = Get-ChildItem $sourcedir -Recurse | Where-Object { -not $_.PsIsContainer }
$localContents = Get-ChildItem $localdir | Where-Object { -not $_.PsIsContainer }

Compare-Object $sourceContents $localContents -Property Name,Length,LastWriteTime -PassThru |
Where-Object {$_.SideIndicator -eq "<="} |
ForEach-Object {
Write-Output $_.FullName, $_.Name;
Write-Output $localdir;
$a = Join-Path $localdir $_.FullName.SubString($sourcedir.Length);
Write-Output $a;
Write-Output $_;
# Write-Outputs are just to see what's going on
# Works as expected up until here
Copy-Item $_ -Destination "$a"
}

在这种情况下,我收到一条错误消息:“找不到路径 'C:\Users\Boss\File1.txt,因为它不存在。”

我已经尝试了很多事情并最终以上述尝试不同的方式来提供目标值。

当我完成此操作时,源需要成为网络共享。我很早就想过使用 Robocopy,但我需要再次将其迁移到使用 URL 作为源。

如有任何想法,我们将不胜感激。我使用 C# .NET 进行编程,但我一直觉得学习 PowerShell 是一项挑战。


使用 Theo 的示例,我将代码简化为以下内容。请注意,我将文件夹移到了用户之外的一个文件夹中,而不是在任何同步文件夹中。

$sourcedir = "C:\Unsynchronised\Alpha"
$localdir = "C:\Unsynchronised\Beta"
# I kept the "local" because I would like to use this for server-client some day

Get-ChildItem -Path $sourcedir -Recurse -File | ForEach-Object {
$localDirPath = Join-Path -Path $localdir -ChildPath $_.DirectoryName.Substring($sourcedir.Length)
$localDirFile = Join-Path -Path $localDirPath -ChildPath $_.Name

Write-Output $_.DirectoryName
Write-Output $localDirPath
Write-Output $localDirFile
$_ | Copy-Item -Destination %localDirPath
}

输出给出...

C:\Unsynchronised\Alpha
C:\Unsynchronised\Beta\
C:\Unsynchronised\Beta\TestFile.txt
C:\Unsynchronised\Alpha\Folder1
C:\Unsynchronised\Beta\Folder1
C:\Unsynchronised\Beta\Folder1\Subfile_1_1.txt

尽管在 Alpha 中有更新的同名文件,但 Beta 中的文件保持不变。

最佳答案

我不会在这上面使用 Compare-Object,而是在一个简单的循环中使用 if() 条件来测试文件是否已经存在于目标路径中,并且如果没有,从源路径复制它,在需要的地方创建子文件夹:

$sourcedir   = "C:\Users\Boss\One\Two\Alpha"
$destination = "C:\Users\Boss\One\Two\Beta"

# get a string array of files already in the destination path (FullNames only)
$existingFiles = (Get-ChildItem -Path $destination -Recurse -File).FullName

Get-ChildItem -Path $sourcedir -Recurse -File | ForEach-Object {
$targetPath = Join-Path -Path $destination -ChildPath $_.DirectoryName.Substring($sourcedir.Length)
$targetFile = Join-Path -Path $targetPath -ChildPath $_.Name
# test if the file is not already present in the destination
if ($existingFiles -notcontains $targetFile) {
# create the destination subfolder path if this does not yet exist
$null = New-Item -Path $targetPath -ItemType Directory -Force
$_ | Copy-Item -Destination $targetPath
}
}

由于您的问题并未表明您只想复制目标中尚未存在的文件,还想复制现有文件的较新版本,请参阅下面的编辑版本:

$sourcedir   = 'C:\Unsynchronised\Alpha'
$destination = 'C:\Unsynchronised\Beta'

Get-ChildItem -Path $sourcedir -Recurse -File | ForEach-Object {
$targetPath = Join-Path -Path $destination -ChildPath $_.DirectoryName.Substring($sourcedir.Length)
$targetFile = Join-Path -Path $targetPath -ChildPath $_.Name
# test if the file is not already present in the destination
$existingFile = Get-Item -Path $targetFile -ErrorAction SilentlyContinue
# if there is no such file or that file is older than the one we have in the source directory
if (!$existingFile -or $_.LastWriteTime -gt $existingFile.LastWriteTime) {
# create the destination subfolder path if this does not yet exist
$null = New-Item -Path $targetPath -ItemType Directory -Force
$_ | Copy-Item -Destination $targetPath -Force
}
}

知道这一点,为什么不使用 robocopy 呢?

有点像

robocopy.exe $sourcedir $destination /S /XO /COPYALL /R:0 /W:0

在哪里

/S       Do not copy empty directories
/XO Excludes older files (do not overwrite existing files that are newer in the destination)
/COPYALL Copy ALL file info
/R:0 Number of retries on failures (default 1 million)
/W:0 Wait time between retries in seconds (default is 30 seconds)

关于PowerShell 将源文件夹中的文件复制到匹配目标失败并显示目标值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69262766/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com