Powershell:创建包含 N 个子目录中最新文件中最新行的 CSV 列表
Powershell: Create CSV list containing newest line in newest file in N number subdirs
我对 Powershell 还很陌生,所以这对我来说是一个挑战:
Objective:遍历N个包含日志的子目录,在每个子目录中找到最新的文件,提取最后写入的行并将其添加到文件中。
这列出了包含日志的目录 - 这些目录的来来去去超出了我的控制范围。
Get-ChildItem Z:\Logfiles | Where{$_.LastWriteTime -gt (Get-Date).AddDays(-1)}
我发现这个片段可以从命名目录中的最后一个写入文件中提取最后一行,其中每个目录都包含来自服务器的日志,命名为文件夹名称(主要是 IP 地址):
gci Z:\Logfiles2.16.1.1 | sort LastWriteTime | select -last 1 | Get-Content | Select-Object -Last 1
我需要合并这些,以便我可以将这些最后几行附加到一个文件中,也许作为 CSV - 所有提示都非常感谢。
使用 Get-Childitems
的 -File
和 -Recurse
标志,这将为您提供根文件夹和子文件夹中的所有文件。将其通过管道传输到 Group-Object
并按目录分组。然后,您需要做的就是按日期对分组文件进行排序,select 第一个。像这样:
Get-ChildItem -File -Recurse | Group-Object Directory | % {
New-Object psobject -Property @{Folder = $_.Name; File = ($_.Group | Sort-Object LastWriteTime -Descending)[0]}
} | Export-Csv your_file_name.csv
你可以这样使用,我添加了注释以便更容易理解逻辑。
# List all folders on the Initial Path
$folders = Get-ChildItem -Directory -Path Z:\Logfiles
$export = [system.collections.generic.list[pscustomobject]]::new()
foreach($folder in $folders)
{
# Get the newest file in each folder
$file = Get-ChildItem $folder -File | Sort-Object LastWriteTime -Descending | Select -First 1
# Read the content of the file and get the last line
$content = (Get-Content $file)[-1]
# Here you can create a new object that will be used to export to Csv
# As an example, i'll create an object with the File Name,
# Last Write Time and Last Line of the Content (This will be the CSV columns)
$export.Add(
[PSCustomObject]@{
FileName = $file.Name
LastWriteTime = $file.LastWriteTime
LastLine = $content
})
}
# After the loop finishes you can export the results to a Csv
$export | Export-Csv -NoTypeInformation -Path "Path/To/Export/Here.csv"
我对 Powershell 还很陌生,所以这对我来说是一个挑战:
Objective:遍历N个包含日志的子目录,在每个子目录中找到最新的文件,提取最后写入的行并将其添加到文件中。
这列出了包含日志的目录 - 这些目录的来来去去超出了我的控制范围。
Get-ChildItem Z:\Logfiles | Where{$_.LastWriteTime -gt (Get-Date).AddDays(-1)}
我发现这个片段可以从命名目录中的最后一个写入文件中提取最后一行,其中每个目录都包含来自服务器的日志,命名为文件夹名称(主要是 IP 地址):
gci Z:\Logfiles2.16.1.1 | sort LastWriteTime | select -last 1 | Get-Content | Select-Object -Last 1
我需要合并这些,以便我可以将这些最后几行附加到一个文件中,也许作为 CSV - 所有提示都非常感谢。
使用 Get-Childitems
的 -File
和 -Recurse
标志,这将为您提供根文件夹和子文件夹中的所有文件。将其通过管道传输到 Group-Object
并按目录分组。然后,您需要做的就是按日期对分组文件进行排序,select 第一个。像这样:
Get-ChildItem -File -Recurse | Group-Object Directory | % {
New-Object psobject -Property @{Folder = $_.Name; File = ($_.Group | Sort-Object LastWriteTime -Descending)[0]}
} | Export-Csv your_file_name.csv
你可以这样使用,我添加了注释以便更容易理解逻辑。
# List all folders on the Initial Path
$folders = Get-ChildItem -Directory -Path Z:\Logfiles
$export = [system.collections.generic.list[pscustomobject]]::new()
foreach($folder in $folders)
{
# Get the newest file in each folder
$file = Get-ChildItem $folder -File | Sort-Object LastWriteTime -Descending | Select -First 1
# Read the content of the file and get the last line
$content = (Get-Content $file)[-1]
# Here you can create a new object that will be used to export to Csv
# As an example, i'll create an object with the File Name,
# Last Write Time and Last Line of the Content (This will be the CSV columns)
$export.Add(
[PSCustomObject]@{
FileName = $file.Name
LastWriteTime = $file.LastWriteTime
LastLine = $content
})
}
# After the loop finishes you can export the results to a Csv
$export | Export-Csv -NoTypeInformation -Path "Path/To/Export/Here.csv"