SOLVED

Dynamically created file name using present system time.

Brass Contributor

Each time I run my script I need to create a new file name that includes the system date and time to reflect the script run time. The desired output file name would look something like this:

 

My Output File yyyyddmm.hhmmss.csv

 

Any assistance greatly appreciated!

Thanks,

Fred

4 Replies
This is a refinement to the "Parsing a file name" script that you have been assisting me with. I used a new post to keep the topics separated.

For this topic I simply need to insert the present date and time into the output filename as shown in this example.

Existing file name: Ion Future Events.csv
Desired file name example: Ion Future Events 20230313 112816 am.csv

Here's the script:
$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, 8)
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) {
Write-Output "File $($file.Name) in folder $folder has date in the future."
} else {
$object = New-Object -TypeName psobject
$object | Add-Member -MemberType NoteProperty -Name "Site" -Value $folder
$object | Add-Member -MemberType NoteProperty -Name "File Name" -Value $file.Name
$object | Add-Member -MemberType NoteProperty -Name "File Size" -Value $file.Length
$object | Add-Member -MemberType NoteProperty -Name "Date Time" -Value $file.LastWriteTime
$object | Add-Member -MemberType NoteProperty -Name "File Count" -Value $filesCount
$object | Export-Csv $csvLog -Encoding ASCII -Append -NoTypeInformation
}
}
}
best response confirmed by Fred_Elmendorf (Brass Contributor)
Solution

@Fred_Elmendorf 

 

Having put a fuller example in your other post, I'll simply say that to change the $LogFile name to include a timestamp would simply involve changing Line 1 from that example to something like the following:

 

$LogFile = ".\SomeLogFile-$([datetime]::Now.ToString("yyyyMMdd HHmmss")).log";

 

From that small change, I get the CSV output log filename of:

SomeLogFile-20230316 175653.log

 

Cheers,

Lain

Thank you, Lain
I had done some exploring through various reference docs and came up with a solution that works, but yours is much more concise. I really appreciate your assistance.

Cheers,
Fred
1 best response

Accepted Solutions
best response confirmed by Fred_Elmendorf (Brass Contributor)
Solution

@Fred_Elmendorf 

 

Having put a fuller example in your other post, I'll simply say that to change the $LogFile name to include a timestamp would simply involve changing Line 1 from that example to something like the following:

 

$LogFile = ".\SomeLogFile-$([datetime]::Now.ToString("yyyyMMdd HHmmss")).log";

 

From that small change, I get the CSV output log filename of:

SomeLogFile-20230316 175653.log

 

Cheers,

Lain

View solution in original post