Forum Discussion

Fred_Elmendorf's avatar
Fred_Elmendorf
Brass Contributor
Mar 10, 2023

Dynamically created file name using present system time.

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

  • LainRobertson's avatar
    LainRobertson
    Mar 16, 2023

    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

    • Fred_Elmendorf's avatar
      Fred_Elmendorf
      Brass Contributor
      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, 😎
      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
      }
      }
      }
      • LainRobertson's avatar
        LainRobertson
        Silver Contributor

        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