Need help converting Office_2018-04-02T00-04-02 to 2018-04-02 00:04:02 in powershell

  • Hi All,
    I kinda need help converting the value from Office_2018-04-02T00-04-02 to 2018-04-02 00:04:02.
    Once i get the help here I'll put your helpful line my foreach loop which will do the same for multiple places as I have multiple places where I need to so the same conversion.

  • Mac1986 - Tuesday, April 3, 2018 12:01 PM

    Hi All,
    I kinda need help converting the value from Office_2018-04-02T00-04-02 to 2018-04-02 00:04:02.
    Once i get the help here I'll put your helpful line my foreach loop which will do the same for multiple places as I have multiple places where I need to so the same conversion.

    I did below dumb code to get what I wanted
    $FileName = "Office_2018-04-02T00-09-02.csv"
    $time = ($FileName -split '_')[1]
    $time = $time.Replace(".csv","")
    Remove-Item V:\SQLFeed\SplitOutput.txt -force
    $time -split 'T' >> V:\SQLFeed\SplitOutput.txt
    $Join = Get-Content V:\SQLFeed\SplitOutput.txt -ReadCount 3 | ForEach{$_ -Join " "}
    $DatePart = $Join.Substring(0, $Join.IndexOf(" "))
    $TimePart = $Join | % {$_.substring($_.length-8) -replace ("-",":")}
    $DatePart + " " + $TimePart

    All this to convert Office_2018-04-02T00-09-02.csv to 2018-04-02 00:09:02. I'm sure there might a easier way to achieve this but I'm not at that expert level yet 🙁

  • Mac1986 - Tuesday, April 3, 2018 2:16 PM

    Mac1986 - Tuesday, April 3, 2018 12:01 PM

    Hi All,
    I kinda need help converting the value from Office_2018-04-02T00-04-02 to 2018-04-02 00:04:02.
    Once i get the help here I'll put your helpful line my foreach loop which will do the same for multiple places as I have multiple places where I need to so the same conversion.

    I did below dumb code to get what I wanted
    $FileName = "Office_2018-04-02T00-09-02.csv"
    $time = ($FileName -split '_')[1]
    $time = $time.Replace(".csv","")
    Remove-Item V:\SQLFeed\SplitOutput.txt -force
    $time -split 'T' >> V:\SQLFeed\SplitOutput.txt
    $Join = Get-Content V:\SQLFeed\SplitOutput.txt -ReadCount 3 | ForEach{$_ -Join " "}
    $DatePart = $Join.Substring(0, $Join.IndexOf(" "))
    $TimePart = $Join | % {$_.substring($_.length-8) -replace ("-",":")}
    $DatePart + " " + $TimePart

    All this to convert Office_2018-04-02T00-09-02.csv to 2018-04-02 00:09:02. I'm sure there might a easier way to achieve this but I'm not at that expert level yet 🙁

    I used this as a chance to play with regex more.
    This ends up with a smaller footprint, but I don't necessarily know that it's more understandable (it's not for me :D)
    $FileName = "Office_2018-04-02T00-09-02.csv"
    ([regex]::replace(([regex]::matches($FileName,'(?<=_)(.*)(?=.csv)').value), '(?<=T).*',{param($old) [string]$old -replace '-',':'})) -replace 'T',' '

    This one is more lines, but makes more sense to me...
    $FileName = "Office_2018-04-02T00-09-02.csv"
    $time = (($FileName -split '_')[1].Replace(".csv","") -split 'T')
    ($time[0] + " " + ($time[1] -replace ("-",":")))

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply