Get part of a file name

Welcome Forums General PowerShell Q&A Get part of a file name

This topic contains 4 replies, has 3 voices, and was last updated by

 
Participant
1 week, 2 days ago.

  • Author
    Posts
  • #156729

    Participant
    Topics: 13
    Replies: 25
    Points: 29
    Rank: Member

    Hello!

    I am working on a script that moves matching files to a share directory.

    The tricky part is that the files have slightly different names and a time tamp in the file name at different postions based on the file name. I want to use the time stamp as the patch factor so I need to extract that time stamp from the first file the then compare to the second file to make sure they match before moving them to be processed.

    File name structure is simpler to this.

    Bob_Order_mmddyyy_hhmmss,txt
    Bob_Account_mmddyyyy_hhmmssnn.txt

    My goal is to batch the 2 files to the mmddyy_hhmm

    As always thanks for the assistance!

  • #156731

    Moderator
    Topics: 8
    Replies: 817
    Points: 2,424
    Helping Hand
    Rank: Community Hero

    With regex...

    $r = 'Bob_Order_01031984_040322.txt'
    $r -match '_(........)_(....)'
    $Matches[1,2] -join '_'
    
  • #156732

    Participant
    Topics: 1
    Replies: 1333
    Points: 1,690
    Helping Hand
    Rank: Community Hero
    $ListOfFilenames = @(
        'Bob_Order_01012019_143000.txt'
        'Bob_Account_02022019_101530nn.txt'
        )
    
    foreach ($Filename in $ListOfFilenames) {
        $Filename -match '(\d{8}_\d{6})' | Out-Null
        [PSCustomObject]@{
            Filename = $Filename
            Timestamp = [DATETIME]::ParseExact($Matches[1],'MMddyyyy_HHmmss',$null)
        }
    }
  • #156954

    Participant
    Topics: 13
    Replies: 25
    Points: 29
    Rank: Member

    Thanks for the feed back.

    I think the approach I want to take is to pull the time stamp from the first file then look to see if I have the second file with the same time stamp.

    The complicated issue is I have to pull these files down from an FTP site then move them to a processing share. But I have to make sure I have a matching pare then move the first file to be processed. I can then only move the second file after the first file has completed processing and is removed from the destination folder. I have an idea how I will code all the automation I just need to pull that time stamp to a variable.

     

    Thanks!!!

  • #157178

    Participant
    Topics: 1
    Replies: 1333
    Points: 1,690
    Helping Hand
    Rank: Community Hero

    You should ask a specific question about a particular piece of code you wrote. And you should post this piece of code here (formatted as code please). At the moment you're just explaining the task you think you have an idea of how to accomplish it. 😉

You must be logged in to reply to this topic.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort