繁体   English   中英

从回收站复制文件

[英]copying files from recycle bin

在这里( 列出回收站中的文件 ),我发现了一条@ Smile4ever帖子,说如何获取回收站中文件的原始位置:

(New-Object -ComObject Shell.Application).NameSpace(0x0a).Items()
|select @{n="OriginalLocation";e={$_.ExtendedProperty("{9B174B33-40FF-11D2-A27E-00C04FC30871} 2")}},Name
| export-csv -delimiter "\" -path C:\Users\UserName\Desktop\recycleBinFiles.txt -NoTypeInformation

(gc C:\Users\UserName\Desktop\recycleBinFiles.txt | select -Skip 1)
| % {$_.Replace('"','')}
| set-content C:\Users\UserName\Desktop\recycleBinFiles.txt

我想将它们复制到某个位置(以防万一我被告知不要删除其中一些而清空回收站)。

在这里( https://superuser.com/questions/715673/batch-script-move-files-from-windows-recycle-bin )我发现了@ gm2帖子可以复制它们

$shell = New-Object -ComObject Shell.Application  
$recycleBin = $shell.Namespace(0xA) #Recycle Bin  
$recycleBin.Items() | %{Copy-Item $_.Path ("C:\Temp\{0}" -f $_.Name)}   

而且它们工作正常,但我还需要更多。

我对powershell一无所知,但我想做的是:对于回收站中的每个文件,在备份文件夹C:\\ Temp中创建其原始位置文件夹,并将文件复制到那里(所以我赢了不会出现更多具有相同名称的文件的问题)。

然后压缩该C:\\ Temp。

有没有办法做到这一点? 谢谢!

您应该可以这样做:

# Set a folder path INSIDE the C:\Temp folder to collect the files and folders
$outputPath = 'C:\Temp\RecycleBackup'
# afterwards, a zip file is created in 'C:\Temp' with filename 'RecycleBackup.zip'

$shell = New-Object -ComObject Shell.Application  
$recycleBin = $shell.Namespace(0xA)
$recycleBin.Items() | ForEach-Object {
    # see https://docs.microsoft.com/en-us/windows/win32/shell/shellfolderitem-extendedproperty
    $originalPath = $_.ExtendedProperty("{9B174B33-40FF-11D2-A27E-00C04FC30871} 2")
    # get the root disk from that original path
    $originalRoot = [System.IO.Path]::GetPathRoot($originalPath)

    # remove the root from the OriginalPath
    $newPath = $originalPath.Substring($originalRoot.Length)

    # change/remove the : and \ characters in the root for output
    if ($originalRoot -like '?:\*') {  
        # a local path.  X:\ --> X
        $newRoot = $originalRoot.Substring(0,1)
    }
    else {                             
        # UNC path.  \\server\share --> server_share
        $newRoot = $originalRoot.Trim("\") -replace '\\', '_'   
        #"\"# you can remove this dummy comment to restore syntax highlighting in SO
    }

    $newPath = Join-Path -Path $outputPath -ChildPath "$newRoot\$newPath"
    # if this new path does not exist yet, create it
    if (!(Test-Path -Path $newPath -PathType Container)) {
        New-Item -Path $newPath -ItemType Directory | Out-Null
    }

    # copy the file or folder with its original name to the new output path
    Copy-Item -Path $_.Path -Destination (Join-Path -Path $newPath -ChildPath $_.Name) -Force -Recurse
}

# clean up the Com object when done
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($shell) | Out-Null
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
$shell = $null

以下代码需要PowerShell版本5

# finally, create a zip file of this RecycleBackup folder and everything in it.
# append a '\*' to the $outputPath variable to enable recursing the folder
$zipPath = Join-Path -Path $outputPath -ChildPath '*'
$zipFile = '{0}.zip' -f $outputPath.TrimEnd("\")
#"\"# you can remove this dummy comment to restore syntax highlighting in SO

# remove the zip file if it already exists
if(Test-Path $zipFile -PathType Leaf) { Remove-item $zipFile -Force }
Compress-Archive -Path $zipPath -CompressionLevel Optimal -DestinationPath $zipFile -Force

在低于版本5的PowerShell中创建zip文件

如果您没有PowerShell 5或更高版本,则“ Compress-Archive不可用。
要从C:\\Temp\\RecycleBackup创建一个zip文件,您可以改为执行以下操作:

$zipFile = '{0}.zip' -f $outputPath.TrimEnd("\")
#"\"# you can remove this dummy comment to restore syntax highlighting in SO

# remove the zip file if it already exists
if(Test-Path $zipFile -PathType Leaf) { Remove-item $zipFile -Force }
Add-Type -AssemblyName 'System.IO.Compression.FileSystem'
[System.IO.Compression.ZipFile]::CreateFromDirectory($outputPath, $zipFile) 

当然,您也可以使用第三方软件,例如7Zip。 网上有很多示例,例如在这里如何在Powershell中使用它

根据您在创建zip后删除“ RecycleBackup”文件夹的最后请求

Remove-Item -Path $outputPath -Recurse -Force

希望能有所帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM