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...
Mar 10, 2023
try this code and let know if this will solve your problem
$file_name = "event-20220630T1420405650000.pqd"
$date_str = $file_name.Substring(6, 😎
$date_obj = [datetime]::ParseExact($date_str, "yyyyMMdd", $null)
$file_path = "C:\path\to\event-20220630T1420405650000.pqd"
$creation_time = (Get-ItemProperty -Path $file_path).CreationTime
$creation_date = $creation_time.Date
if ($date_obj -gt $creation_date) {
Write-Output "Date in file name is in the future."
} else {
Write-Output "Date in file name is not in the future."
}
$file_name = "event-20220630T1420405650000.pqd"
$date_str = $file_name.Substring(6, 😎
$date_obj = [datetime]::ParseExact($date_str, "yyyyMMdd", $null)
$file_path = "C:\path\to\event-20220630T1420405650000.pqd"
$creation_time = (Get-ItemProperty -Path $file_path).CreationTime
$creation_date = $creation_time.Date
if ($date_obj -gt $creation_date) {
Write-Output "Date in file name is in the future."
} else {
Write-Output "Date in file name is not in the future."
}
Fred_Elmendorf
Mar 10, 2023Brass Contributor
Thank you for your quick reply. I've been trying to incorporate your code into an existing script that I'm using to recurse a directory structure and extract specific information including the latest file name, size, and creation time, along with the parent folder name and the total file count in the folder and direct the output to a .csv file. That original script is functioning properly. My goal is to incorporate your code in such a way that as the folders are recursed, any file name date pattern that is in the future compared to the creation date of that specific file will be flagged as "Date in file name is in the future". So far, things are working with one exception. The $creation_date is the creation date of the parent folder, not the file. Here's the ugly code I have put together so far, and a sample of the output. Any additional assistance will be greatly appreciated.
$dirs = Get-ChildItem "\\fileshare\level1\level2\level3\parentdirectory" -Directory
$csvLog = "\\fileshare\toplevel\myprofile\myfolders\Documents\PowerShellOutput\Ion Future Events.csv"
foreach ($dir in $dirs) {
$recentFile = $null
$folder = $dir.Name
$directory = $dir.FullName
echo "Directory" $directory
$filesCount = (Get-ChildItem $directory -Filter "event*.pqd" -Recurse).Count
$recentFile = Get-ChildItem $directory -Filter "event*.pqd" -Recurse | Sort-Object LastWriteTime -Descending| Select-Object -First 1
$recentFileName = $recentFile.Name
$recentFileLength = $recentFile.Length
$recentFileWriteTime = $recentFile.LastWriteTime
if ($recentFile) {
$file_name = $recentFileName
$date_str = $file_name.Substring(6, 😎
echo "Date_str" $date_str
$date_obj = [datetime]::ParseExact($date_str, "yyyyMMdd", $null)
echo "Date Obj" $date_obj
$file_path = "\\fileshare\level1\level2\level3\parentdirectory"
$creation_time = (Get-ItemProperty -Path $file_path).CreationTime
echo "Creation Time" $creation_time
$creation_date = $creation_time.Date
echo "Creation Date" $Creation_date
if ($date_obj -gt $creation_date) {
Write-Output "Date in file name is in the future."
} else {
Write-Output "Date in file name is not in the future."
}
$object = New-Object -TypeName psobject
$object | Add-Member -MemberType NoteProperty -Name "Site" -Value $folder
$object | Add-Member -MemberType NoteProperty -Name "File Name" -Value $recentFileName
$object | Add-Member -MemberType NoteProperty -Name "File Size" -Value $recentFileLength
$object | Add-Member -MemberType NoteProperty -Name "Date Time" -Value $recentFileWriteTime
$object | Add-Member -MemberType NoteProperty -Name "File Count" -Value $filesCount
$object | Export-Csv $csvLog -Encoding ASCII -Append -NoTypeInformation
}
}
Directory
\\fileshare\level1\level2\level3\parentdirectory
Date_str
20230310
Date Obj
Friday, March 10, 2023 12:00:00 AM
Creation Time
Wednesday, January 5, 2022 2:13:48 PM
Creation Date
Wednesday, January 5, 2022 12:00:00 AM
Date in file name is in the future.
$dirs = Get-ChildItem "\\fileshare\level1\level2\level3\parentdirectory" -Directory
$csvLog = "\\fileshare\toplevel\myprofile\myfolders\Documents\PowerShellOutput\Ion Future Events.csv"
foreach ($dir in $dirs) {
$recentFile = $null
$folder = $dir.Name
$directory = $dir.FullName
echo "Directory" $directory
$filesCount = (Get-ChildItem $directory -Filter "event*.pqd" -Recurse).Count
$recentFile = Get-ChildItem $directory -Filter "event*.pqd" -Recurse | Sort-Object LastWriteTime -Descending| Select-Object -First 1
$recentFileName = $recentFile.Name
$recentFileLength = $recentFile.Length
$recentFileWriteTime = $recentFile.LastWriteTime
if ($recentFile) {
$file_name = $recentFileName
$date_str = $file_name.Substring(6, 😎
echo "Date_str" $date_str
$date_obj = [datetime]::ParseExact($date_str, "yyyyMMdd", $null)
echo "Date Obj" $date_obj
$file_path = "\\fileshare\level1\level2\level3\parentdirectory"
$creation_time = (Get-ItemProperty -Path $file_path).CreationTime
echo "Creation Time" $creation_time
$creation_date = $creation_time.Date
echo "Creation Date" $Creation_date
if ($date_obj -gt $creation_date) {
Write-Output "Date in file name is in the future."
} else {
Write-Output "Date in file name is not in the future."
}
$object = New-Object -TypeName psobject
$object | Add-Member -MemberType NoteProperty -Name "Site" -Value $folder
$object | Add-Member -MemberType NoteProperty -Name "File Name" -Value $recentFileName
$object | Add-Member -MemberType NoteProperty -Name "File Size" -Value $recentFileLength
$object | Add-Member -MemberType NoteProperty -Name "Date Time" -Value $recentFileWriteTime
$object | Add-Member -MemberType NoteProperty -Name "File Count" -Value $filesCount
$object | Export-Csv $csvLog -Encoding ASCII -Append -NoTypeInformation
}
}
Directory
\\fileshare\level1\level2\level3\parentdirectory
Date_str
20230310
Date Obj
Friday, March 10, 2023 12:00:00 AM
Creation Time
Wednesday, January 5, 2022 2:13:48 PM
Creation Date
Wednesday, January 5, 2022 12:00:00 AM
Date in file name is in the future.