Get part of a file name

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!

With regex…

$r = 'Bob_Order_01031984_040322.txt'
$r -match '_(........)_(....)'
$Matches[1,2] -join '_'
$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)
    }
}

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!!!

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. :wink: