如何使用powershell压缩在特定日期创建的文件?
我有一个脚本来压缩在特定日期创建的所有文件如何使用powershell压缩在特定日期创建的文件?,powershell,powershell-4.0,Powershell,Powershell 4.0,我有一个脚本来压缩在特定日期创建的所有文件 Add-Type -AssemblyName System.IO.Compression.FileSystem $logfolder = "c:\users\riteshthakur\desktop\abc" $startdate = "20161205" $enddate = "20161205" [System.IO.Compression.ZipArchive] $arch = [System.IO.Compression.ZipFile]
Add-Type -AssemblyName System.IO.Compression.FileSystem
$logfolder = "c:\users\riteshthakur\desktop\abc"
$startdate = "20161205"
$enddate = "20161205"
[System.IO.Compression.ZipArchive] $arch = [System.IO.Compression.ZipFile]::Open('c:\users\riteshthakur\desktop\arch.zip', [System.IO.Compression.ZipArchiveMode]::Update)
Get-ChildItem - path $logFolder |
Where-Object {$_.CreationDate -gt $startDate -and $_.CreationDate -lt $endDate} |
foreach{[System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($arch,$_.FullName,$_.Name)}
# archive will be updated with files after you close it. normally, in C#, you would use "using ZipArchvie arch = new ZipFile" and object would be disposed upon exiting "using" block. here you have to dispose manually:
$arch.Dispose()
我正在运行此脚本,但抛出:“无法添加类型。找不到程序集‘System.IO.Compression.FileSystem’。”
请帮忙 ,这里有一个要压缩的函数:
# Purpose: Creates a .zip file of a file or folder.
# Params:
# -target: The file or folder you would like to zip.
#
# -zip_to: The location where the zip file will be created. If an old version
# exists, it will be deleted.
#
# -compression (optional): Sets the compression level for your zip file. Options:
# a. fast - Higher process speed, larger file size (default option).
# b. small - Slower process speed, smaller file size.
# c. none - Fastest process speed, largest file size.
#
# -add_timestamp (optional): Applies a timestamp to the .zip file name.
# By default, no timestamp is used.
#
# -confirm (optional): When provided, indicates that you would like to be
# prompted when the zip process is finished.
#
# |Info|
[CmdletBinding()]
Param (
[Parameter(Mandatory=$true,Position=0)]
[string]$target,
[Parameter(Mandatory=$true,Position=1)]
[string]$zip_to,
[Parameter(Mandatory=$false,Position=2)]
[ValidateSet("fast","small","none")]
[string]$compression,
[Parameter(Mandatory=$false,Position=3)]
[bool]$timestamp,
[Parameter(Mandatory=$false,Position=4)]
[bool]$confirm
)
#-----------------------------------------------------------------------------#
function DeleteFileOrFolder
{ Param([string]$PathToItem)
if (Test-Path $PathToItem)
{
Remove-Item ($PathToItem) -Force -Recurse;
}
}
function DetermineCompressionLevel{
[Reflection.Assembly]::LoadFile('C:\WINDOWS\System32\zipfldr.dll')
Add-Type -Assembly System.IO.Compression.FileSystem
$CompressionToUse = $null;
switch($compression)
{
"fast" {$CompressionToUse = [System.IO.Compression.CompressionLevel]::Fastest}
"small" {$CompressionToUse = [System.IO.Compression.CompressionLevel]::Optimal}
"none" {$CompressionToUse = [System.IO.Compression.CompressionLevel]::NoCompression}
default {$CompressionToUse = [System.IO.Compression.CompressionLevel]::Fastest}
}
return $CompressionToUse;
}
#-----------------------------------------------------------------------------#
Write-Output "Starting zip process...";
if ((Get-Item $target).PSIsContainer)
{
$zip_to = ($zip_to + "\" + (Split-Path $target -Leaf) + ".zip");
}
else{
#So, the CreateFromDirectory function below will only operate on a $target
#that's a Folder, which means some additional steps are needed to create a
#new folder and move the target file into it before attempting the zip process.
$FileName = [System.IO.Path]::GetFileNameWithoutExtension($target);
$NewFolderName = ($zip_to + "\" + $FileName)
DeleteFileOrFolder($NewFolderName);
md -Path $NewFolderName;
Copy-Item ($target) $NewFolderName;
$target = $NewFolderName;
$zip_to = $NewFolderName + ".zip";
}
DeleteFileOrFolder($zip_to);
if ($timestamp)
{
$TimeInfo = New-Object System.Globalization.DateTimeFormatInfo;
$CurrentTimestamp = Get-Date -Format $TimeInfo.SortableDateTimePattern;
$CurrentTimestamp = $CurrentTimestamp.Replace(":", "-");
$zip_to = $zip_to.Replace(".zip", ("-" + $CurrentTimestamp + ".zip"));
}
$Compression_Level = (DetermineCompressionLevel);
$IncludeBaseFolder = $false;
[Reflection.Assembly]::LoadWithPartialName( "System.IO.Compression.FileSystem" );
[System.IO.Compression.ZipFile]::CreateFromDirectory($target, $zip_to, $Compression_Level, $IncludeBaseFolder);
Write-Output "Zip process complete.";
if ($confirm)
{
write-Output "Press any key to quit ...";
$quit = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown");
}
按如下方式使用:
zip.ps1 -target "C:\Projects\test" -zip_to "C:\projects\test2" [-compression fast] [-timestamp] [-confirm]
,这里有一个要压缩的函数:
# Purpose: Creates a .zip file of a file or folder.
# Params:
# -target: The file or folder you would like to zip.
#
# -zip_to: The location where the zip file will be created. If an old version
# exists, it will be deleted.
#
# -compression (optional): Sets the compression level for your zip file. Options:
# a. fast - Higher process speed, larger file size (default option).
# b. small - Slower process speed, smaller file size.
# c. none - Fastest process speed, largest file size.
#
# -add_timestamp (optional): Applies a timestamp to the .zip file name.
# By default, no timestamp is used.
#
# -confirm (optional): When provided, indicates that you would like to be
# prompted when the zip process is finished.
#
# |Info|
[CmdletBinding()]
Param (
[Parameter(Mandatory=$true,Position=0)]
[string]$target,
[Parameter(Mandatory=$true,Position=1)]
[string]$zip_to,
[Parameter(Mandatory=$false,Position=2)]
[ValidateSet("fast","small","none")]
[string]$compression,
[Parameter(Mandatory=$false,Position=3)]
[bool]$timestamp,
[Parameter(Mandatory=$false,Position=4)]
[bool]$confirm
)
#-----------------------------------------------------------------------------#
function DeleteFileOrFolder
{ Param([string]$PathToItem)
if (Test-Path $PathToItem)
{
Remove-Item ($PathToItem) -Force -Recurse;
}
}
function DetermineCompressionLevel{
[Reflection.Assembly]::LoadFile('C:\WINDOWS\System32\zipfldr.dll')
Add-Type -Assembly System.IO.Compression.FileSystem
$CompressionToUse = $null;
switch($compression)
{
"fast" {$CompressionToUse = [System.IO.Compression.CompressionLevel]::Fastest}
"small" {$CompressionToUse = [System.IO.Compression.CompressionLevel]::Optimal}
"none" {$CompressionToUse = [System.IO.Compression.CompressionLevel]::NoCompression}
default {$CompressionToUse = [System.IO.Compression.CompressionLevel]::Fastest}
}
return $CompressionToUse;
}
#-----------------------------------------------------------------------------#
Write-Output "Starting zip process...";
if ((Get-Item $target).PSIsContainer)
{
$zip_to = ($zip_to + "\" + (Split-Path $target -Leaf) + ".zip");
}
else{
#So, the CreateFromDirectory function below will only operate on a $target
#that's a Folder, which means some additional steps are needed to create a
#new folder and move the target file into it before attempting the zip process.
$FileName = [System.IO.Path]::GetFileNameWithoutExtension($target);
$NewFolderName = ($zip_to + "\" + $FileName)
DeleteFileOrFolder($NewFolderName);
md -Path $NewFolderName;
Copy-Item ($target) $NewFolderName;
$target = $NewFolderName;
$zip_to = $NewFolderName + ".zip";
}
DeleteFileOrFolder($zip_to);
if ($timestamp)
{
$TimeInfo = New-Object System.Globalization.DateTimeFormatInfo;
$CurrentTimestamp = Get-Date -Format $TimeInfo.SortableDateTimePattern;
$CurrentTimestamp = $CurrentTimestamp.Replace(":", "-");
$zip_to = $zip_to.Replace(".zip", ("-" + $CurrentTimestamp + ".zip"));
}
$Compression_Level = (DetermineCompressionLevel);
$IncludeBaseFolder = $false;
[Reflection.Assembly]::LoadWithPartialName( "System.IO.Compression.FileSystem" );
[System.IO.Compression.ZipFile]::CreateFromDirectory($target, $zip_to, $Compression_Level, $IncludeBaseFolder);
Write-Output "Zip process complete.";
if ($confirm)
{
write-Output "Press any key to quit ...";
$quit = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown");
}
按如下方式使用:
zip.ps1 -target "C:\Projects\test" -zip_to "C:\projects\test2" [-compression fast] [-timestamp] [-confirm]
据介绍,该程序集直到.NETFramework4.5才可用。您安装了什么版本的.NET Framework?如果您有4.5或更高版本,则可能需要重新安装/修复它。根据,该程序集直到.NET Framework 4.5才可用。您安装了什么版本的.NET Framework?如果您有4.5或更高版本,您可能需要重新安装/修复它。Powershell中有
压缩存档
和扩展存档
(不确定它们出现在哪个版本)。它们在Powershell 5.0版中。我仍然在使用powershell 4.0,这肯定是一个.Net框架问题(请参阅其他答案)。如果我错了,请告诉我,我们可以撤回dup投票。Powershell中有压缩存档
和扩展存档
(不确定它们出现在哪个版本)。它们在Powershell 5.0版中。我仍然在使用powershell 4.0,这肯定是一个.Net框架问题(请参阅其他答案)。如果我错了,请告诉我,我们可以撤回dup投票。发布的代码试图添加与原始海报所说的生成错误的程序集相同的程序集。为什么这里不会发生同样的错误?@JeffZeitlin:我相信在我们得到错误的系统中,没有zipfldr.dll在这个路径下C:\WINDOWS\System32\zipfldr.dll
。你能检查一下那个文件在不在吗。默认情况下,它应该在那里,特别是当您使用任何winrar或7zipOK时,不知何故,第一次我错过了[Reflection.Assembly]::LoadFile('C:\WINDOWS\System32\zipfldr.dll')
,这可能会使函数作为一个整体运行。我不相信即使加载了该程序集,紧跟其后的Add类型也不会抛出与原始海报所询问的相同的错误。我不这么认为。我不应该。如果您有任何错误,请您发布错误或屏幕截图。我已经用了很长一段时间了,事实上,如果dll存在,我从来没有遇到过任何问题。我现在还不能进行测试,因为我已经安装了.NET 4.6.2,所以Add类型适合我。但是,Add类型中调用的程序集在.NET 4.5之前不存在,这可以解释原始海报的错误,这也是我询问OP的原因。发布的代码尝试添加原始海报所说的生成错误的相同程序集。为什么这里不会发生同样的错误?@JeffZeitlin:我相信在我们得到错误的系统中,没有zipfldr.dll在这个路径下C:\WINDOWS\System32\zipfldr.dll
。你能检查一下那个文件在不在吗。默认情况下,它应该在那里,特别是当您使用任何winrar或7zipOK时,不知何故,第一次我错过了[Reflection.Assembly]::LoadFile('C:\WINDOWS\System32\zipfldr.dll')
,这可能会使函数作为一个整体运行。我不相信即使加载了该程序集,紧跟其后的Add类型也不会抛出与原始海报所询问的相同的错误。我不这么认为。我不应该。如果您有任何错误,请您发布错误或屏幕截图。我已经用了很长一段时间了,事实上,如果dll存在,我从来没有遇到过任何问题。我现在还不能进行测试,因为我已经安装了.NET 4.6.2,所以Add类型适合我。但是,Add类型中调用的程序集在.NET4.5之前不存在,这可以解释原始海报的错误,这也是我向OP询问此问题的原因。