0

I have 2 excel files like below with timestamp (YYYYMMdd_HHmmss) in the directory. There are 2 excel files in the directory. I will extract timestamp from filename in the directory and then compare two dates and finally assign variable to each.

I want to assign to $Start variable as report-capacity-server01.contoso.com-20191010_171044.xlsx.

I want to assign to $End variable as report-capacity-server01.contoso.com-20191108_130454.xlsx.

Arbelac
  • 1,698
  • 6
  • 37
  • 90
  • And what is your question? Do you want to know how to get the timestamp? – Alex_P Nov 23 '19 at 12:00
  • First of all There are 2 excel files in the directory. I will extract timestamp from filename in the directory and then compare two dates and finally assign variable to each. – Arbelac Nov 23 '19 at 12:23
  • In this case, please edit your question with this description for clarification. – Alex_P Nov 23 '19 at 12:31
  • It's a bit unclear to me what the files in the directory are named.. Are the names just the timestamps like `20191010_171044.xlsx` or is that timestamp part of the names? Can you give an example? – Theo Nov 23 '19 at 13:48
  • No names just the timestamps like 20191010_171044.xlsx. e.g report-capacity-server01.contoso.com-20191108_130454.xlsx like this. I will extract timestamp from filename – Arbelac Nov 23 '19 at 14:07

2 Answers2

1

If you want the full file names sorted in chronological order by embedded timestamp (with possibly varying name prefixes), pass a timestamp-extraction expression as a sort criterion.

$Start, $End = (Get-ChildItem *.xlsx |
                 Sort-Object { ($_.Name -split '[-.]')[-2] }).FullName
  • Get-ChildItem *.xlsx gets file-info objects for all files with extension .xlsx in the current directory ([System.IO.FileInfo] instances).

  • Sort-Object receives a script block ({ ... }) as a criterion that extracts the timestamp from each input file's name ($_.Name) by splitting it by - and . characters and extracting the second-to-last ([-2]) resulting token.

  • (...).FullName uses member-access enumeration to extract the full names (paths) of the sorted file-info objects.

  • $Start, $End = ... is a destructuring assignment that assigns the 2 (chronologically sorted) full file names to individual variables. Note that if there were more than 2 files, $End would receive all remaining files, as an array.

As an aside:

Given the format of your timestamp strings, you can use them as-is for comparison, because lexical comparison will be equivalent to chronological comparison.

If you wanted to convert the timestamp strings to proper .NET [datetime] instances, do the following:

$timeStamps = Get-ChildItem *.xlsx | ForEach-Object { 
  [datetime]::ParseExact(($_.Name -split '[-.]')[-2], 'yyyyMMdd_HHmmss', $null) 
}
mklement0
  • 382,024
  • 64
  • 607
  • 775
0

In order to extract the timestamp from your files, you can use -split "-".

This will give you an array of strings divided after each delimiter.

$files = Get-ChildItem -Path ./Path-to-files
$timestamp = $files.BaseName -split "-"

The $timestamp variable contains now an array of strings from the two files. With Index operations you can select the timestamp and do your comparison. Instead of splitting all files in your $files variable you can also of course loop through each file and assign the timestamp directly to variable.

Alex_P
  • 2,580
  • 3
  • 22
  • 37