我正在尝试使用Powershell创建网络目录中特定文件夹中所有* .xml文件的存档(不包括子目录中可能已存在的任何.zip文件)。我需要循环遍历所有子目录,并且仅压缩存在的文件,并且只有* .xml文件应该包含在子目录中的zip中 - zip需要具有子目录和名称中的日期。我还希望在压缩后从子目录中删除任何xml文件。
以下是我目前的情况:
[Reflection.Assembly]::LoadWithPartialName( "System.IO.Compression" )
[Reflection.Assembly]::LoadWithPartialName( "System.IO.Compression.FileSystem" )
$directory = 'C:\some directory'
$folderstozip = Get-ChildItem $directory -Recurse -Filter *.xml -Exclude *.zip |
Select-Object -ExpandProperty DirectoryName -Unique
foreach ($folder in $folderstozip) {
$archive = $folder + ((Get-Date).ToString(' yyyy-MM-dd')) + '.zip'
If(Test-Path $archive) { Remove-Item $archive }
[System.IO.Compression.ZipFile]::CreateFromDirectory($folder, $archive, 'Optimal', $false)
}
这是在上面的目录中创建zip文件,而不是在xml文件所在的子目录中。
我也无法弄清楚如何正确使用Remove-Item
cmdlet。 Remove-Item $folderstozip -Recurse -Filter *.xml
删除整个目录,我只想删除xml文件。它还包括zip中子目录中存在的任何zip文件。
有人能帮助吗?任何帮助都感激不尽! 如果有任何不清楚的地方,如果您需要进一步的信息,请告诉我。
谢谢你, 亲切的问候, LR
答案 0 :(得分:0)
就Remove-Item
而言,如果您只想删除某个扩展名的文件,请尝试使用-Include
开关而不是-Filter
。此外,您的文件夹路径后面需要\*
表示您正在查看文件夹的内容而不是文件夹本身。我会改变你提到的那一行
Remove-Item $folderstozip\* -Recurse -Include *.xml
答案 1 :(得分:0)
在可行的选择之后移动拉链吗?
Move-Item -Path "$directory\$archive" -Destination $folder
或者,我认为问题在于这一行:
$archive = $folder + ((Get-Date).ToString(' yyyy-MM-dd')) + '.zip'
打印出来以确保它包含您的想法。本来以为它需要像:
$archive = $folder.FullName + "\" ((Get-Date).ToString(' yyyy-MM-dd')) + '.zip'
答案 2 :(得分:0)
Just to put it out there, this is what I ended up with.
It does exactly what I need, excludes already zipped files from the new zips, writes details of the successful zips to a log file and also reads a list of directories to search through and zip from a text file.
Hopefully someone will find this useful :) It may not be as streamlined as a Powershell expert would code it, but i'm pretty happy with it!
Thanks!
#---------------------------------------------------------------------------------------
# Script: Archiver.ps1
# Author:
# Date: 31/01/2017
# Comments: .NET Framework 4.5 required - WMF 5.0 required i.e. powershell 5.0
#---------------------------------------------------------------------------------------
[Reflection.Assembly]::LoadWithPartialName( "System.IO.Compression" )
[Reflection.Assembly]::LoadWithPartialName( "System.IO.Compression.FileSystem" )
$streamReader = New-Object System.IO.StreamReader -ArgumentList "C:\archiver\includedirectories.txt"
$streamWriter = New-Object System.IO.StreamWriter -ArgumentList "C:\archiver\archiverlogfile.txt", $true
while($line = $streamReader.ReadLine())
{
$directory = $line
$folderstozip = Get-ChildItem $directory -Recurse -Include *.xml, *.onx, *.xls, *.txt, *.xlsx, *.csv | Select-Object -ExpandProperty DirectoryName -Unique
foreach ($folder in $folderstozip)
{
$archive = $folder + ((Get-Date).ToString('_yyyy-MM-dd_HHmm')) + '.zip'
If(Test-path $archive) {Remove-item $archive}
$filestozip = Get-ChildItem $folder . -exclude *.zip
Compress-Archive $filestozip $archive -CompressionLevel Fastest
Move-Item -Path $archive -Destination $folder
$streamWriter.WriteLine(((Get-Date).ToString('yyyy-MM-dd_HH:mm - ')) + 'Zipped ' + $archive + ' to ' + $folder + ' successfully.')
Remove-Item $folder\* -Recurse -Include *.xml, *.onx, *.xls, *.txt, *.xlsx, *.csv
}
}
$streamReader.close()
$streamWriter.close()