使用7zip和powershell的文件夹压缩问题

时间:2016-07-14 12:27:09

标签: shell powershell compression archive 7zip

我有一个powershell脚本,它会查找并压缩日志文件,然后创建一个新的.7zip文件,但是目前它在压缩包含日期的文件夹时遇到问题,例如application_logs_2016-07-14

1:如果有包含char的文件夹,脚本可以正常工作,但是如果包含的文件夹有一个日期,例如(application_logs_2016-07-14)没有存档。

2:我需要一个超过5天的zip日志文件,dump.log.341.log,dump.log.342.log和dump.log.343.log应该转换为dump.log。 341.zip,dump.log.342.zip,dump.log.343.zip。

这是当前的代码,如果任何powershell大师可以建议我会非常高兴。提前谢谢。

# See if 7 zip is installed
if (-not (test-path "$env:ProgramFiles\7-Zip\7z.exe")) {throw "$env:ProgramFiles\7-Zip\7z.exe needed"}
set-alias sz "$env:ProgramFiles\7-Zip\7z.exe"

# Define log location and target directory
$Logfile = "D:\stuff\Software\dump files\Archive.log" 
$newdirectory = "D:\stuff\compressedlogs" 

# Write to log file - start of archive
$now = Get-date -Format "yyyy-MM-dd HH:mm:ss"
Add-Content $Logfile -value "Archive started $now"

# Import what we want to back up from a list file
$List = Import-CSV "D:\stuff\Software\dump files\Compressionlist.txt"

ForEach ($Entry in $List){

$filepath = $($Entry.FilePath)
$Extension = $($Entry.Extension)
$Include = $($Entry.Include)
$Horizon = $($Entry.Horizon)
$Ext2Chg = $($Entry.Ext2Chg)

# Extract List of Files to process
$log = Get-ChildItem -Recurse -Path $filepath -Filter $Extension -Include $Include | Where-Object {$_.lastwriteTime -lt (((Get-Date).AddDays($Horizon)).date)}

# Archive each file found
ForEach ($file in $log) {

if ($file -ne $null) {

 $name = $file.name
$newdate = $file.CreationTime | Get-Date -f "yyyy-MM-dd"
$newname = $file.BaseName + "___" + $newdate + $file.Extension
$directory = $file.DirectoryName
$zipfile = $newname.replace($Ext2Chg,".7z")
sz a -t7z "$newdirectory\$zipfile" "$directory\$name"
$now = Get-date -Format "yyyy-MM-dd HH:mm:ss"
Add-Content $Logfile -value "File $directory\$name archived to folder\new filename $newdirectory\$newname at $now"
Remove-Item $file
}

}

}

# Write to log file - end of archive
$now = Get-date -Format "yyyy-MM-dd HH:mm:ss"
Add-Content $Logfile -value "Archive completed $now"

# End

该脚本查看txt文档以查找要归档的内容

COMPRESSION LIST

Filepath,Extension,Include,Horizon,Ext2Chg

E:\APPLICATION\DUMP_logs_*,*.log,APP.dumps-currentlog.messages*,-5,.log

=============================================== ================

示例文件夹结构

D:\ application \ server_log

(包含日志,例如server_log_2016-07-14_00-00-00_0001.log) 归档。

D:\ application \ application_log_2016-07-14

(包含日志,例如APP.dumps-currentlog.messages.log)这将存档。

希望有意义。

0 个答案:

没有答案