1

I have several txt files distributed in several sub-folders. This is what a file looks like:

Data file. version 01.10.

1
8
*
DAT\Trep\Typ10
DAT\Trep\Typ12
DAT\Trep\Typ13

what I would like to do is to extract only the part after the last "\" in order to get something like this:

Typ10 FileName.txt Path
Typ12 FileName.txt Path
Typ13 FileName.txt Path
...

I tried the following

Get-ChildItem -Path 'D:\MyData\*.txt' -Recurse | ForEach-Object {Write-Output $_; $data=Get-Content $_}
$data = $data -replace '.*\\'
$data

it works well for a single file but not with several (-recurse). Being a powershell beginner I can't figure out how to improve my script.

I also tried to add this to get the result shown above in my post, but that doesn't work either.

Select-Object -Property @{Name = 'Result list'; Expression = { $data }}, Filename, Path

Thanks in advance for your kind help

Wehrmicel
  • 19
  • 4

1 Answers1

0

Use Select-String:

Get-ChildItem -Path D:\MyData\*.txt -Recurse -File |
  Select-String '^.+\\(.+)' | 
  ForEach-Object { 
    [pscustomobect] @{ 
      Result = $_.Matches.Groups[1].Value
      FileName = $_.FileName 
      Path = $_.Path
    } 
  }

As for your desire to exclude certain folders during recursive traversal:

  • Unfortunately, Get-ChildItem -Exclude only excludes the matching folders themselves, not also their content. There are two relevant feature requests to potentially overcome this limitation in the future:

  • For now, a different approach with post-filtering based on Where-Object is required, using folder names Folder1 and Folder2 as examples:

Get-ChildItem -Path D:\MyData\*.txt -Recurse |
  Where-Object FullName -NotLike *\Folder1\* |
  Where-Object FullName -NotLike *\Folder2\* |
  Select-String '^.+\\(.+)' | 
  ForEach-Object { 
    [pscustomobect] @{ 
      Result = $_.Matches.Groups[1].Value
      FileName = $_.FileName 
      Path = $_.Path
    } 
  }

For a more flexible, cross-platform approach based on regex matching (which is invariably more complex), see the bottom section of this answer.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • Thanks this is perfect. I added the following : Out-File D:\test.csv I noticed that the path only return D: and not D:\MyData\Folder1\SubfolderB for exemple. Am I missing something ? – Wehrmicel Feb 01 '23 at 17:07
  • @Wehrmicel, I don't see this symptom, neither in Windows PowerShell (5.1) nor in PowerShell (Core) 7.3.2. What version are you running? `.Path` should definitely contain the full file path; not doing so would be a bug. – mklement0 Feb 01 '23 at 17:46
  • Thanks for your feedback. I tried with Export-CSV -NoTypeInformation D:\MyData\test.xls and it displays correctly now. Here is myh last question: Is there a way to exclude two folders ? I tried with -exclude Folder1, Folder2 without luck thanks again ! – Wehrmicel Feb 02 '23 at 07:45