Forum Discussion
Fred_Elmendorf
Mar 10, 2023Brass Contributor
Parsing a file name and comparing a string to file creation date
I'm still a novice in PowerShell, but I have continuing problems to solve. I have automatically created file names of this form: event-20220630T1420405650000.pqd Where event- is constant, the n...
Fred_Elmendorf
Mar 13, 2023Brass Contributor
This is getting really close to what I need, but there are a couple remaining issues.
1. I only need output for the files where the datetime object is greater than the creation date, so I removed some of the NoteProperty definitions and moved the export inside the if statement. I also tried to create a new NoteProperty -Name "Has a date greater than creation date" -$creation_date, but as you can see from the sample output it did not get added to the output file.
2. The second issue is that with the results so far, I have discovered that there are many valid files where the creation date is one day greater than the date in the file name, but it should never be more than one day greater. How can I change the comparison to effectively be if($date_obj -gt $creation_date+1)?
Here's my modified code and sample output.
Modified code:
$dirs = Get-ChildItem "\\fileshare\level1\level2\level3\parentdirectory" -Directory
$csvLog = "\\fileshare\toplevel\myprofile\myfolders\Documents\PowerShellOutput\Ion Future Events.csv"
foreach ($dir in $dirs) {
$folder = $dir.Name
$directory = $dir.FullName
# echo "Directory" $directory
$files = Get-ChildItem $directory -Filter "event*.pqd" -Recurse
$filesCount = $files.Count
foreach ($file in $files) {
$date_str = $file.Name.Substring(6, 😎
# echo "Date_str" $date_str
$date_obj = [datetime]::ParseExact($date_str, "yyyyMMdd", $null)
# echo "Date Obj" $date_obj
$creation_date = $file.CreationTime.Date
# echo "Creation Date" $creation_date
if ($date_obj -gt $creation_date) {
$object = New-Object -TypeName psobject
# $object | Add-Member -MemberType NoteProperty -Name Write-Output "File $($file.Name) in folder $folder has date in the future."
$object | Add-Member -MemberType NoteProperty -Name "Site" -Value $folder
$object | Add-Member -MemberType NoteProperty -Name "File Name" -Value $file.Name
$onject | Add-Member -MemberType NoteProperty -Name "Has a date greater than creation date" -$creation_date
$object | Export-Csv $csvLog -Encoding ASCII -Append -NoTypeInformation
} else {
}
}
}
Sample output:
"Site","File Name"
"SiteA","event-20221110T0324065380000.pqd"
"SiteA","event-20221110T0323011310000.pqd"
"SiteB","event-20220904T0217282030000.pqd"
"Site1","event-20220904T0217268210000.pqd"
"Site2","event-20220705T0056043380000.pqd"
"Site2","event-20220707T0031091680000.pqd"
"Site3","event-20220707T0031094020000.pqd"
1. I only need output for the files where the datetime object is greater than the creation date, so I removed some of the NoteProperty definitions and moved the export inside the if statement. I also tried to create a new NoteProperty -Name "Has a date greater than creation date" -$creation_date, but as you can see from the sample output it did not get added to the output file.
2. The second issue is that with the results so far, I have discovered that there are many valid files where the creation date is one day greater than the date in the file name, but it should never be more than one day greater. How can I change the comparison to effectively be if($date_obj -gt $creation_date+1)?
Here's my modified code and sample output.
Modified code:
$dirs = Get-ChildItem "\\fileshare\level1\level2\level3\parentdirectory" -Directory
$csvLog = "\\fileshare\toplevel\myprofile\myfolders\Documents\PowerShellOutput\Ion Future Events.csv"
foreach ($dir in $dirs) {
$folder = $dir.Name
$directory = $dir.FullName
# echo "Directory" $directory
$files = Get-ChildItem $directory -Filter "event*.pqd" -Recurse
$filesCount = $files.Count
foreach ($file in $files) {
$date_str = $file.Name.Substring(6, 😎
# echo "Date_str" $date_str
$date_obj = [datetime]::ParseExact($date_str, "yyyyMMdd", $null)
# echo "Date Obj" $date_obj
$creation_date = $file.CreationTime.Date
# echo "Creation Date" $creation_date
if ($date_obj -gt $creation_date) {
$object = New-Object -TypeName psobject
# $object | Add-Member -MemberType NoteProperty -Name Write-Output "File $($file.Name) in folder $folder has date in the future."
$object | Add-Member -MemberType NoteProperty -Name "Site" -Value $folder
$object | Add-Member -MemberType NoteProperty -Name "File Name" -Value $file.Name
$onject | Add-Member -MemberType NoteProperty -Name "Has a date greater than creation date" -$creation_date
$object | Export-Csv $csvLog -Encoding ASCII -Append -NoTypeInformation
} else {
}
}
}
Sample output:
"Site","File Name"
"SiteA","event-20221110T0324065380000.pqd"
"SiteA","event-20221110T0323011310000.pqd"
"SiteB","event-20220904T0217282030000.pqd"
"Site1","event-20220904T0217268210000.pqd"
"Site2","event-20220705T0056043380000.pqd"
"Site2","event-20220707T0031091680000.pqd"
"Site3","event-20220707T0031094020000.pqd"
LainRobertson
Mar 16, 2023Silver Contributor
Here's a version with a little bit of flexibility insofar as:
- It doesn't care what the prefix is, so long as the filename format adheres to:
somePrefix-timestamp.pqd - It'll silently exclude any files where the timestamp component isn't parsable.
It's also more efficient over larger file repositories as it's not storing the file system structure in a local variable prior to doing the work.
I went with using the full path to the file in the output but you can change that to your liking.
$LogFile = ".\SomeLogFile.log";
$Root = "D:\Data";
$FilenameTimestamp = [datetime]::MinValue;
Remove-Item -Path $LogFile -ErrorAction:SilentlyContinue;
Get-ChildItem -Directory -Path $Root |
ForEach-Object {
$Directory = $_;
Get-ChildItem -File -Recurse -Path ($_.FullName) -Filter "event-*.pqd" |
ForEach-Object {
if ((($NameParts = $_.Name.Split(@("-", "."))).Count -eq 3) -and ([datetime]::TryParseExact($NameParts[1].Substring(0, 8), "yyyyMMdd", $null, [System.Globalization.DateTimeStyles]::None, [ref] $FilenameTimestamp)) -and ($FilenameTimestamp.Date -gt $_.CreationTime.Date))
{
[PSCustomObject] @{
Site = $Directory.Name;
Path = $_.FullName;
}
}
}
} | Export-Csv -Path $LogFile -NoTypeInformation;
Sample output from the log file:
"Site","Path"
"Temp","D:\Data\Temp\event-20230630T1420405650000.pqd"
Cheers,
Lain