由于 onedrive 同步问题,恢复多个原始文件 powershell

Recover multiple original files powershell due to onedrive sync issues

我们遇到了影响数千个文件的同步问题。

同步工具在每个文件夹中创建了正确文件的备份。备份在扩展名之前的点之前添加了“-LocalPCName”。

我真的是powershell的初学者

我正在尝试编写一个 powershell 脚本来遍历每个文件夹并检查是否为每个文件创建了文件备份。

如果创建了备份,我想重命名文件以添加“-Wrongversion”并从正确的文件中删除“-LocalPCName”。

这基本上会将文件恢复到正确的版本并复制错误的版本。

我写了这段代码,但我在尝试使用 -replace

进行匹配时受阻
Get-ChildItem *.* |
  ForEach-Object {
    If ($_.name -match "\s-LocalPCName")
     {Get-ChildItem $_.Name -replace "\s-LocalPCName","" 
             }
   }

感谢您的帮助!

这是我的代码的较新版本,但我遇到错误并且我担心使用递归,当这将转到子文件夹时我是否遗漏了任何内容?在这段代码中,LocalPCName 是 Didi2015

Get-ChildItem *.*  -recurse|
ForEach-Object {
If ($_.name -match "\s-Didi2015")
{

    $OldName = $_.Name -replace "\s-Didi2015","" 
    $dir=$_.DirectoryName
    $ext = $_.Extension
    $file = $OldName.Substring(0, $OldName.LastIndexOf('.'))

        Write-Output $dir
        Write-Output $ext
        Write-Output $file


    $Newname = $file + " -Wrongversion" + $ext
    Write-Output $Newname
    Rename-Item -Path $OldName -NewName $Newname -WhatIf

    Rename-Item -Path $_ -NewName $OldName -WhatIf

   }



}

这是我的最终代码。它有效。

dir *-Didi2015*.* -Recurse|
ForEach-Object {
If ($_.name -match "-Didi2015")
    {

    $OldName = $_ -replace "-Didi2015","" 
    $dir=$_.DirectoryName
    $ext = $_.Extension
    $file = $OldName.Substring(0, $OldName.LastIndexOf('.'))

       # Write-Output $dir
       # Write-Output $ext
       # Write-Output $file
       # Write-Output $OldName
       # Write-Output $Newname
       # Write-Output $_.Name

    $Newname = $file + "-Wrongversion" + $ext
    Rename-Item -Path $OldName -NewName $Newname  -WhatIf 
    Rename-Item -Path $OldName -NewName $Newname  -Force 
    Rename-Item -Path $_ -NewName $OldName -WhatIf
    Rename-Item -Path $_ -NewName $OldName -Force

   }

}