【问题标题】:Recover multiple original files powershell due to onedrive sync issues由于 onedrive 同步问题,恢复多个原始文件 powershell
【发布时间】:2016-05-19 03:08:40
【问题描述】:

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

同步工具在每个文件夹中创建了正确文件的备份。备份在扩展名之前的点之前添加了“-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

   }



}

【问题讨论】:

  • 我也尝试使用此代码,但没有任何反应:Get-ChildItem -Filter “*-LocalPCName*” -Recurse | move-Item -Destination ($_.name -replace "\s-LocalPCName","") -force
  • 你遇到了什么错误?

标签: powershell onedrive


【解决方案1】:

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

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

   }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-09
    • 1970-01-01
    • 2015-03-01
    • 1970-01-01
    • 2021-03-25
    • 1970-01-01
    • 2018-08-31
    相关资源
    最近更新 更多