作为自动备份某些开发日志文件的一部分,我正在使用Powershell脚本,并希望重命名我刚刚创建的7zip文件,以匹配第一个文件名称的最后一部分和最后一个文件的最后一部分存档中的文件。
例如;我有三个文件,file1-03082013.log,file2-04082013.log和file3-05082013.log,这些文件是使用自动Powershell和7zip脚本压缩来创建log.zip的。现在我想将log.zip重命名为log-03082013 - 05082013(匹配第一个文件的最后一部分和我刚刚创建的存档中最后一个文件的最后一部分)。
这是我创建的完整脚本。 (我非常喜欢Powershell中的脚本编写,所以对于如何改进现有脚本的任何评论都非常受欢迎)希望你们能以任何方式提供帮助!提前谢谢!
#Variables
$Source = "C:\Path\"
$Destination = "C:\ZIP"
$Temp = "C:\Templog\"
$Previous = "C:\Templog\"
$programma = "C:\Program Files\7-Zip\7z.exe"
#Copy files to TempFolder
Function CopyFile
{
Copy-Item -Recurse -Filter "*.svclog" -path $Source -Destination $Temp
}
CopyFile
#Delete Old Log Files on server
Function DelOldFile
{
if (Test-Path $Source)
{
$Days = "7"
$Now = Get-Date
$LastWrite = $Now.AddDays(-$days)
$Files = get-childitem $Source -include *.svclog -recurse |Where {$_.LastWriteTime -le "$LastWrite"}
foreach ($File in $Files)
{write-host "Deleting file $File" -foregroundcolor "Red"; Remove-Item $File | out-null}
}
Else
{Write-Host "The folder $Source doesn't exist! Check the folder path!" -foregroundcolor "red"}
}
DelOldFile
#Create .zip archive from files and folders in Temp folder and copy to destination folder using 7zip.
Function ZipFile
{
Start-Process $programma -ArgumentList "a $Destination\Log.zip $Temp" -Wait -PassThru
}
ZipFile
#Delete Temp Folder
Function GetPrevious
{
if (Test-Path $Previous){
Remove-Item $Previous -Recurse -Force
}
}
GetPrevious
答案 0 :(得分:1)
使用Davids帮助我最终想出了以下完整的脚本。 希望它也能帮助你们应对未来的挑战。
如果您对此完成的脚本有任何意见或疑问,请与我们联系。
#=============================================
#
# Recursive Log Search, Archive & Del Script
# Ver. 1.0.0 06/08/2013
# Roelof Wijnholds
#
#=============================================
#Enter parentfolder
$sPath = "Path\to\parent\folder"
#Folder where the archive will be created (if it doesn't exist, folder will be created)
$tPath = "archive"
#Path to 7zip executable
$7z = "C:\Program Files\7-Zip\7z.exe"
checkForWebservices $sPath 5
#Check for bin & log folder & *.extension file
function checkValidWebservice($path)
{
If((Test-Path $path\bin) -and (Test-Path $path\log) -and (Test-Path $path\*.svc)){
write-host "Valid webservice structure found: " $path
return $true
}else{
return $false
}
}
function checkForWebservices($path, $max, $level = 0)
{
$path = (Resolve-Path $path).ProviderPath
foreach ($item in @(Get-ChildItem $path))
{
if ($item.Attributes -eq "Directory")
{
if(checkValidWebservice $path)
{
#Create name for compressed file
$fileName = CreateCompressFileName $path
if($fileName -ne $null)
{
#Compress files in folder
CompressAndRemoveLogFiles $fileName $path
Write-Host "Compressing and removing files"
}
return
}else{
checkForWebservices $item.PSPath $max ($level + 1)
}
#Break if recursive goes to DEEP
if ($level -eq $max)
{
Write-Host "Max depth" $path\$item - $level
return
}
}
}
}
function CreateCompressFileName($path)
{
#Get startingdate from file
$fileNameFrom = Get-ChildItem $path\log | Where {$_.Extension -eq ".svclog"} | Sort-Object name | Select-Object -first 1
if($fileNameFrom -ne $null)
{
#Get first file
if($fileBaseName -eq $null)
{
#File is supposed to be servicename_20130508.svclog
$fileBaseName = $fileNameFrom.BaseName.SubString(0,$fileNameFrom.BaseName.length - 9)
}
$fileNameFrom = $fileNameFrom.BaseName.SubString($fileNameFrom.BaseName.length - 8,8)
#Get last file
$fileNameTo = Get-ChildItem $path\log | Where {$_.LastWriteTime -lt ((Get-Date).Date) -and $_.Extension -eq ".svclog" } | Sort-Object name -descending | Select-Object -first 1
if($fileNameTo -ne $null)
{
$fileNameTo = $fileNameTo.BaseName.SubString($fileNameTo.BaseName.length - 8,8)
#Compile the name
return $fileBaseName+"_"+$fileNameFrom+"_"+$fileNameTo
}
}
return $null
}
function CompressAndRemoveLogFiles($fileName, $path)
{
$cFiles = Get-ChildItem $path\log | Where {$_.LastWriteTime -lt ((Get-Date).Date) -and $_.Extension -eq ".svclog" }
Foreach ($item in $cFiles)
{
#Add file to archive
$endPath = $path.TrimStart($sPath)
$target = "{0}\{1}\{2}\{3}" -f $sPath,$tPath,$endPath,$fileName
$result = & $7z a -mx5 -mmt $target $item.FullName
#Cleanup files
Remove-Item $path\log\$item
}
}
答案 1 :(得分:0)
这应该可以帮助您根据您的要求获取zip文件的名称,只需将日志文件的位置传递给它:
function Get-ZipFileName
{
param
($logPath)
# Get a list of files, remove the extension and split on the hyphen, #
# then take the second part of the array
$filenames = gci -Path $logPath -Filter "*.log" | % { $($_.Basename -split "-")[1] }
# Get the first item in the now sorted list
$first = $filenames | Sort-Object | Select-Object -First 1
# Get the first item in the sorted list, now descending to get the last
$last = $filenames | Sort-Object -descending | Select-Object -First 1
# return the filename
"log-{0} - {1}" -f $first, $last
}
我建议你看看使用稍微不同的日期格式,排序会更好,yyyymmdd会比ddmmyyyy更好。