-1

我希望你能帮我解决我的小问题。有2个不同的文件夹,A和B。

在文件夹 A 中,有很多 DLL 的数据。在文件夹 B 中,也有很多 DLL。

例如:

文件夹 A:

ThreeServer.Host。v13.1 .Core.dll

你好,这个。v13.1 .Is.More.dll

文件夹 B:

ThreeServer.Host。v12.0 .Core.dll

你好,这个。v12.0 .Is.More.dll

文件夹 A 中的所有 DLL 名称与文件夹 B (v12.0) 中的 DLL 的“v13.1”名称不同。

现在我想用文件夹 B 中的 DLL 替换文件夹 A 中的所有 DLL。

一切都基于语言PowerShellISE/Powershell

有人知道这个或方法的解决方案吗?

4

2 回答 2

1

您将需要使用组合Get-ChildItem来获取文件列表,使用正则表达式来获取文件名的非版本部分,然后使用通配符来查看目标目录中是否存在匹配项。

Get-ChildItem -Path $DLLPath -Filter *.dll |
    Where-Object { $_.BaseName -Match '^(.*)(v\d+\.\d+)(.*)$' } |
    Where-Object { 
        # uses $matches array to check if corresponding file in destination
        $destFileName = '{0}*{1}.dll' -f $matches[1],$matches[3]
        $destinationPath = Join-Path $FolderB $destFileName

        # Add the destination file name mask to the pipeline object so we can use it later
        $_ | Add-Member NoteProperty -Name DestinationPath -Value $destinationPath

        # Check that a corresponding destination exists
        Test-Path -Path $_.DestinationPath -ItemType Leaf
    } | 
    Copy-Item -WhatIf -Verbose -Destination { 
        # Use Get-Item to get the actual file matching the wildcard above.
        # But only get the first one in case there are multiple matches.
        Get-Item $_.DestinationPath | Select-Object -First 1 -ExpandProperty FullName
    }  

有关正则表达式的更多信息,请参阅about_Regular_Expressions 。

于 2013-07-04T12:44:44.613 回答
0

试试这个代码。

$folderA = "C:\Work\Tasks\test\A"
$folderB = "C:\Work\Tasks\test\B"
$oldVersion="v12.0"
$newVersion="v13.1"
$oldFiles=Get-ChildItem $folderB  | ForEach-Object { $($_.Name) }
Get-ChildItem $folderA  | ForEach-Object `
{ 
  foreach($oldFile in $oldFiles )
  {
   if($($_.Name) -eq ($oldFile -replace $oldVersion,$newVersion))
   {
     Write-host "File Replced: $($_.Name)"
     Write-host "File Deleted: $oldFile"
     Move-Item $($_.FullName) $folderB 
     Remove-item "$folderB\$oldFile"
   }   
  }  
}
于 2013-07-04T14:01:42.420 回答