英文:
Comparing original file and last two files using PowerShell
问题
我有另一个脚本,每隔4小时将数据输出到一个CSV文件,并将文件创建为vdsinfo_$(Get-Date -Format "yyMMdd_hh-mm-ss").csv
。
我想要能够比较原始文件以及最后两个创建的文件,以查看文件中是否有更改。
目前,以下是我对脚本的修改,但它还不够完善,真的很感谢能够帮助我使其生成一个写入Changes.csv
文件的输出。
我希望比较的文件的文件名总是在变化,因此脚本需要使用变量,唯一我认为可以让它工作的方法是使用get-date addhours-4
。
$File = 'E:\TEMP\Changes.csv'
# 获取文件项
$file1 = Get-ChildItem -Path 'E:\TEMP\reports\vdsinfo_original.csv'
# 获取特定日期
$date1 = (Get-Date).AddHours(-8)
# 比较文件修改日期与特定日期
$file2 = Get-ChildItem 'E:\TEMP\reports\' | Where-Object { $_.Name -match '^vdsinfo_\d{6}_\d{2}-\d{2}-\d{2}\.csv$' -and $_.LastWriteTime -gt $date1 }
# 比较文件
Compare-Object -ReferenceObject $file1 -DifferenceObject $file2 -Property Name -CaseSensitive | Format-Table InputObject, SideIndicator -AutoSize | Out-File $File -Width 200
使用当前脚本,它只对两个文件进行比较,我想要比较原始文件以及位于文件夹位置中的最后两个文件。
英文:
I have another script that outputs data into a csv file every 4 hours and creates the files as vdsinfo_$(Get-Date -Format "yyMMdd_hh-mm-ss").csv
.
I want to be able to compare the original file created and also the last two files created against each other to see if there has changes within the file.
At the moment, this is what I have for the script but it isn't quite there yet and would really appreciate some help to get this producing an output written to a Changes.csv
file.
I want to compare files that file names are always changing so the script needs to be using variables where possible and only way I can see this working is by the get-date addhours-4.
$File = 'E:\TEMP\Changes.csv'
# Get File item
$file1 = Get-ChildItem -Path E:\TEMP\reports\vdsinfo_original.csv
# Get the specific date
$date1 = (Get-Date).AddHours(-8)
# Compare file modified date with specific date
$file2 = $file1.LastWriteTime -gt $date1
Compare-Object -ReferenceObject $file1 -DifferenceObject $file2 -CaseSensitive | Format-table InputObject, SideIndicator -Autosize | out-file $file -Width 200
Else {
Write-Host "File missing " -ForegroundColor White -BackgroundColor Red
}
Any help would really help me, thanks in advance
With this current script it only does comparison against two files and I'd like to compare against the original and the last two files within the folder location.
答案1
得分: 0
我想能够比较原始文件与最近创建的两个文件,以查看文件中是否有更改。
这有点不清楚,但在以下内容中,我将假设您想生成2组更改:
- 原始文件与最新版本之间的所有更改的完整集合。
- 最近的更改集 - 在倒数第二个版本和最新版本之间。
唯一我能看到这种方法起作用的方式是通过添加-4
小时的日期:
这一点完全不必要。文件名包含可排序的日期/时间戳,所以按名称对文件进行排序,然后您可以简单地跳到已排序列表中的前一个文件并获取前4小时的数据:
# 定义输出目标
$outFilePaths = @{
All = 'E:\TEMP\AllChanges.csv'
Latest = 'E:\TEMP\LatestChanges.csv'
}
# 查找原始文件
$originalFile = Get-Item -Path E:\TEMP\reports\vdsinfo_original.csv
# 查找文件的所有后续版本,确保它们按名称排序,然后将它们存储在数组中
$files = @(Get-ChildItem -Path E:\TEMP\reports -Filter vdsinfo_[0-9]*.csv | Sort-Object Name)
# 确保我们至少找到了一个后续版本 - 否则没有东西可以进行比较!
if ($files.Count -gt 0) {
# 让我们从比较原始文件与最新版本开始
# $files[-1] 将计算为 $files 数组中的最后一个项目
$allChanges = Compare-Object -ReferenceObject ($originalFile | Get-Content) -DifferenceObject ($files[-1] | Get-Content)
# 输出到文件
$allChanges | Format-Table InputObject, SideIndicator -Autosize | Out-File $outFilePaths['All'] -Width 200
# 重复比较最近两个版本之间的差异
$latestChanges = Compare-Object -ReferenceObject ($files[-2] | Get-Content) -DifferenceObject ($files[-1] | Get-Content)
# 输出到文件
$latestChanges | Format-Table InputObject, SideIndicator -Autosize | Out-File $outFilePaths['Latest'] -Width 200
}
else {
Write-Host "更新的文件丢失" -ForegroundColor White -BackgroundColor Red
}
英文:
> I want to be able to compare the original file created and also the last two files created against each other to see if there has changes within the file.
This is a bit unclear, but in the following I'm going to assume you want to generate 2 sets of changes:
- The full set of all changes between the original file and the latest version
- The most recent set of changes - between the next-to-last and the latest version
> only way I can see this working is by the get-date addhours-4.
That's not necessary at all. The file name contains a sortable date/timestamp, so sort the files by name, then you can simply jump to the previous file in your sorted list and get the data from 4 hours prior:
# define output destinations
$outFilePaths = @{
All = 'E:\TEMP\AllChanges.csv'
Latest = 'E:\TEMP\LatestChanges.csv'
}
# find the original file
$originalFile = Get-Item -Path E:\TEMP\reports\vdsinfo_original.csv
# find all the subsequent versions of the file, make sure
# they're sorted by name, then store them in an array
$files = @(Get-ChildItem -Path E:\TEMP\reports -Filter vdsinfo_[0-9]*.csv |Sort-Object Name)
# make sure we've located at least one subsequent version - otherwise there's nothing to compare against!
if ($files.Count -gt 0) {
# let's start by comparing original with the latest version
#
# $files[-1] is going to evaluate to the _last_ item in the $files array
$allChanges = Compare-Object -ReferenceObject ($originalFile |Get-Content) -DifferenceObject ($files[-1] |Get-Content)
# output to a file
$allChanges |Format-table InputObject, SideIndicator -Autosize |Out-File $filePaths['All'] -Width 200
# repeat comparison between the two latest versions
$latestChanges = Compare-Object -ReferenceObject ($files[-2] |Get-Content) -DifferenceObject ($files[-1] |Get-Content)
# output to a file
$latestChanges |Format-table InputObject, SideIndicator -Autosize |Out-File $filePaths['Latest'] -Width 200
}
else {
Write-Host "Updated files are missing" -ForegroundColor White -BackgroundColor Red
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论