如何使用 Powershell / Node.js 从回收站恢复特定或上次删除的文件?

问题描述 投票:0回答:1

我有一个可以运行 Node.js 和 Powershell 命令 (Electron) 的应用程序,我正在尝试找出从回收站恢复的最佳(或任何)方法:

  • 最后删除的项目
  • 或在特定时间段(例如最后5分钟)删除的项目
  • 或(首选)特定路径

我找到了这个答案,但我没有足够的 Powershell 经验来将其应用于我的案例。

到目前为止我只找到了一种列出回收站项目的方法:

function RestoreItems () {
  $recycleBin = (New-Object -ComObject Shell.Application).NameSpace(0x0a);
  $recycleBin.Items() | ForEach-Object {
    # unfinished code:
    $originalPath = $_.ExtendedProperty(...)
    Copy-Item $_.Path ($originalPath)
  }
}

RestoreItems -Paths ['C:\test\1', 'C:\test\2']

输出所有回收站项目以及我需要的所有属性(名称和日期)。

假设我有一个需要恢复的文件名数组,我该怎么做?

node.js powershell cmd
1个回答
0
投票

这会将最后删除的项目恢复到当前目录。

function RestoreLastDeletedItem {
    # Create the Shell.Application COM object to access the Recycle Bin
    $shell = New-Object -ComObject Shell.Application
    $recycleBin = $shell.Namespace("shell:::{645FF040-5081-101B-9F08-00AA002F954E}")

    # Get all the items in the Recycle Bin
    $items = $recycleBin.Items()

    # Check if there are any items in the Recycle Bin
    if ($items.Count -eq 0) {
        Write-Host "No items found in the Recycle Bin."
        return
    }

    # Sort items by the deletion time (DateModified)
    $lastDeletedItem = $items | Sort-Object { $_.ExtendedProperty('System.DateModified') } | Select-Object -Last 1

    # If we find the last deleted item
    if ($lastDeletedItem) {
        # Get the current directory
        $currentDir = Get-Location

        # Define the path to restore the item to the current directory
        $restorePath = Join-Path $currentDir $lastDeletedItem.Name

        # Use the InvokeVerb to restore the item from the Recycle Bin to the current directory
        Write-Host "Restoring item: $($lastDeletedItem.Name) to $restorePath"
        
        # Copy the item to the current directory
        Copy-Item -Path $lastDeletedItem.Path -Destination $restorePath -Force

        Write-Host "Item restored successfully to $restorePath."
    } else {
        Write-Host "No deleted items found to restore."
    }
}

# Call the function to restore the last deleted item
RestoreLastDeletedItem

注意:它也会在回收站中保留一份副本。但这是某事。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.