我正在寻找一种在远程计算机上重命名日志文件的方法,一旦它达到80MB。例如,假设这是路径“ C:\ Program Files \ Test Folder \ TCP.Log”,一旦大于80MB,则应将其重命名为TCP1.log。重命名后,系统会自动生成一个名称为“ TCP.log”的新日志文件(重命名或删除后,系统会自动生成该文件)。一旦TCP.log再次达到80MB,powershell应该将其重命名为TCP2.Log,我该怎么做?
我已经尝试过该脚本,它将重命名为TCP1.Log,但是一旦再次运行它,它将说该文件已经存在,因此不会将其重命名为TCP2.LOG。
理想情况下,我想创建一个计划任务,如果远程计算机上的TCP.log文件大于80MB,它将运行并重命名。
答案 0 :(得分:1)
我认为实用程序功能可以在以下情况下提供帮助:
function Rename-Unique {
# Renames a file. If a file with that name already exists,
# the function will create a unique filename by appending
# a sequence number to the name before the extension.
[CmdletBinding()]
Param(
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[string]$Path
)
# Throw a bit nicer error than with [ValidateScript({Test-Path -Path $_ -PathType Leaf})]
if (!(Test-Path -Path $Path -PathType Leaf)){
Throw [System.IO.FileNotFoundException] "Rename-Unique: The file '$Path' could not be found."
}
# split the filename into a basename and an extension variable
$baseName = [System.IO.Path]::GetFileNameWithoutExtension($Path)
$extension = [System.IO.Path]::GetExtension($Path) # this includes the dot
$folder = Split-Path -Path $Path -Parent
# get an array of all filenames (name only) of the files with a similar name already present in the folder
$allFiles = @(Get-ChildItem $folder -Filter "$baseName*$extension" -File | Select-Object -ExpandProperty Name)
# for PowerShell version < 3.0 use this
# $allFiles = @(Get-ChildItem $folder -Filter "$baseName*$extension" | Where-Object { !($_.PSIsContainer) } | Select-Object -ExpandProperty Name)
# construct the new filename / strip the path from the file name
$newName = $baseName + $extension # or use $newName = Split-Path $newName -Leaf
if ($allFiles.Count) {
$count = 1
while ($allFiles -contains $newName) {
$newName = "{0}{1}{2}" -f $baseName, $count++, $extension
}
}
Write-Verbose "Renaming '$Path' to '$newName'"
Rename-Item -Path $Path -NewName $newName -Force
}
在您的代码中,您可以像这样使用它:
$logfile = 'C:\Program Files\Test Folder\TCP.Log' # this is your current log file
$maxLogSize = 80MB # the maximum size in bytes you want
# check if the log file exists
if (Test-Path -Path $logfile -PathType Leaf) {
# check if the logfile is at its maximum size
if ((Get-Item -Path $logfile).Length -ge $maxLogSize) {
# rename the current log file using the next sequence number
Rename-Unique -Path $logfile -Verbose
# do we need to create the new log file?
# New-Item -Path $logfile -ItemType 'File' -Force | Out-Null
}
}
答案 1 :(得分:0)
Ditto同意Lee_Daily所说的。
关于您的代码。您实际上并没有做任何事情来获得增量,因此,它将为您提供相同的数字,因此会出错。
只需获取文件名的计数,然后将计数加1即可重命名。如果您说基本文件名始终相同,那么您甚至根本不需要该循环文件。
示例:
# Check for similarly named files
Get-ChildItem -Path 'E:\temp\Reports\TCP*'
# Results
Directory: E:\temp\Reports
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 12/29/2018 1:10 PM 38 TCP.log
-a---- 12/29/2018 1:10 PM 38 TCP1.log
-a---- 3/14/2019 12:27 PM 45 TCP2.log
-a---- 12/29/2018 2:31 PM 34 TCP3.log
# Rename the file that does not have a number with the next highest number of the count of the similarly named file.
Rename-Item -Path E:\Temp\Reports\TCP.log -NewName "TCP$((Get-ChildItem -Path E:\temp\Reports\tcp*).Count).log" -WhatIf
# Results
What if: Performing the operation "Rename File" on target "Item: E:\Temp\Reports\TCP.log Destination: E:\Temp\Reports\TCP4.log".