Home > Net >  Copy latest files based on datestamp in filename from various directories
Copy latest files based on datestamp in filename from various directories

Time:08-17

I am trying to write a Powershell script for a small problem I am having.

I have various log files in various directories. These log files are related to various other computers, and a new batch of these files are added every so often. These files have timestamps within the filename.

For example:

index\1\computer1_system_01122022.evtx 
index\2\computer1_system_01122021.evtx
index\3\computer2_system_01122022.evtx
index\4\computer2_system_01122021.evtx

Note: date is ddMMyyyy

I am trying to write a script that can go into the 'Index' directory, look for all files containing "system", then only selecting the latest file based on the datestamp in the filename.

So for the above example, only copy the following to another directory (i.e. index\latest)

index\latest\computer1_system_01122022.evtx
index\latest\computer2_system_01122022.evtx

I hope I have described the problem clearly enough. I have tried various things, but can't figure this one out.

Any help would be much appreciated.

What I have tried so far: I have not come up with any script for this so far, I am fairly new to powershell. I have to used .indexof to separate the datestamp, and then sort and select the newer one. But this results in the script selecting only the newest file, and discarding the files for other computers. I can't think of a way to make powershell only compare the datestamp of the two 'computer1' files and selecting the newer ones, and then two 'computer2' find and selecting the newer one, and so on.

So to clarify, the script should compare 'computer1' with all other files that contain 'computer1' and select the newer one based on the datestamp in the filename. Then, compare 'computer2' and select the newer one, then 'computer3' and so on. Each of these selected files should be copied to a new directory.

Note: the latest datestamp on computer1 may be different from the latest datestamp on computer2. I.e. newest computer1 file may be named computer1_01122022.evtx and computer2 may be computer2_02022022.evtx. comparison of computer1 should only be with other files that contain computer1, and computer2 files should only be compared with other computer2 files.

This is part is a larger script that is being used to extract log data from files. I have written a script to extract the data, but cannot figure out what to do to get the latest log files out from the folders as described above.

CodePudding user response:

Combine Group-Object with Sort-Object, using calculated properties:

# Simulate Get-ChildItem output.
[System.IO.FileInfo[]] $files = 
  'index\1\computer1_system_01122022.evtx ',
  'index\2\computer1_system_01122021.evtx',
  'index\3\computer2_system_01122022.evtx',
  'index\4\computer2_system_01122021.evtx'

$files |
  Group-Object { ($_.BaseName -split '_')[0] } | # group by computer name
  ForEach-Object { # for each group of computer-specific files
    # sort by timestamp embedded in the file name and output the most recent
    $_.Group | 
      Sort-Object -Descending { 
       $dateStr = ($_.BaseName -split '_')[-1]
       [datetime]::ParseExact($dateStr, 'ddMMyyyy', [cultureinfo] '') 
      } |
      Select-Object -First 1
  }
  • Related