Split name and Date part using Powershell

Welcome Forums General PowerShell Q&A Split name and Date part using Powershell

Viewing 8 reply threads
  • Author
    Posts
    • #245499
      Inactive
      Topics: 2
      Replies: 7
      Points: 0
      Rank: Member

      Hi Team,

      Could you please help me here. I have a requirement to split filename and sate in two variables and if the  date part is yyyy_m_d_time or yyyy_mm_d_time or yyyy_m_dd_time or yyyy_mm_dd_time it should be converted to yyyymmdd format.

      Filename: DailyReportCompletedAJKGFPD_2020_7_21_2252.xlsx

      I tried different code snippets with $regex function which i am not successful to get the required output.

      Thanks,

      Krish

    • #245523
      Participant
      Topics: 8
      Replies: 568
      Points: 2,171
      Helping Hand
      Rank: Community Hero

      Maybe you’ll find some help/ideas in this.

      Output

    • #245529
      Inactive
      Topics: 2
      Replies: 7
      Points: 0
      Rank: Member

      Hi Dough,

      Thanks for the reply. I do see the output for test portion and date portion remains same from different excel files given as input. It looks something is overriding in the loop.

       

      Thanks,

      Krish

    • #245532
      Participant
      Topics: 8
      Replies: 568
      Points: 2,171
      Helping Hand
      Rank: Community Hero

      Please forgive me, I copied the wrong code. Here is what I meant to.

      Output

    • #245607
      Inactive
      Topics: 2
      Replies: 7
      Points: 0
      Rank: Member

      Awesome. Thank u so much Dough. Appreciate your help on this.

    • #245640
      Inactive
      Topics: 2
      Replies: 7
      Points: 0
      Rank: Member

      Sorry to trouble again. Can we replace the file names with network location from the above code like $orgPath = ‘C:\TEMP\Excel\’ to check the history loads

    • #245643
      Participant
      Topics: 8
      Replies: 568
      Points: 2,171
      Helping Hand
      Rank: Community Hero

      You say network location but then list c:\temp\excel. Perhaps you can explain more what you have and what you’re trying to accomplish.

    • #245646
      Inactive
      Topics: 2
      Replies: 7
      Points: 0
      Rank: Member

      One more thing. what if the date separator changes from ‘_’ to ‘-‘ can we handle it from the above code. Looking for reusability and handle mutiple format scenarios. In the network drive.

    • #245739
      Inactive
      Topics: 2
      Replies: 7
      Points: 0
      Rank: Member

      I just added ‘-‘ in your code. It worked. Thanks again.

       

      $str = @(

      “DailyReportCompletedJKGFPD_2020-7-21-2253.xlsx

      “DailyReportCompletedHSMAPD_2020-8-21_2252.xlsx”

      )

       

      switch -Regex ($str){

      ‘^(.+)_(\d{4})[_-](\d{1,2})[_-](\d{1,2})[_-](\d{4}).+’ {

      $text = $matches[1]

      $y    = $matches[2]

      $m,$d = 3..4 | foreach {if($matches[$_].length -eq 1){“0” + $matches[$_]}else{$matches[$_]}}

      $t    = $matches[5]

      “Original File: $_”

      “Text portion : $text”

      “Date portion : $y$m$d$t”

      }

      }

Viewing 8 reply threads
  • You must be logged in to reply to this topic.