Paulo
Paulo

Reputation: 361

Powershell Get data by group

I need some help on this:

I´ve this code:

Get-ChildItem C:\Scripts\Video_Time\Output\ -File | 
Group-Object { $_.Name -replace '_.*$' } | 
ForEach-Object { $_.Group.Fullname } | 
ForEach-Object { 
    echo "file '$($_)'" >> C:\Scripts\Video_Time\input\new.txt
}

This code gives me all files in a directory, grouped by name and writes the results to a txt file.

file 'C:\Scripts\Video_Time\Output\el arte_12.mp4'
file 'C:\Scripts\Video_Time\Output\el arte_17.mp4'
file 'C:\Scripts\Video_Time\Output\el arte_2.mp4'
file 'C:\Scripts\Video_Time\Output\la música_15.mp4'
file 'C:\Scripts\Video_Time\Output\la música_4.mp4'
file 'C:\Scripts\Video_Time\Output\la música_9.mp4'
file 'C:\Scripts\Video_Time\Output\la vida de antes_10.mp4'
file 'C:\Scripts\Video_Time\Output\la vida de antes_16.mp4'
file 'C:\Scripts\Video_Time\Output\la vida de antes_6.mp4'
file 'C:\Scripts\Video_Time\Output\los animales_14.mp4'
file 'C:\Scripts\Video_Time\Output\los animales_3.mp4'
file 'C:\Scripts\Video_Time\Output\los juegos_1.mp4'
file 'C:\Scripts\Video_Time\Output\los juegos_11.mp4'
file 'C:\Scripts\Video_Time\Output\los juegos_5.mp4'

I need the result only by same file name at the time, that means each group at the time:

file 'C:\Scripts\Video_Time\Output\el arte_12.mp4'
file 'C:\Scripts\Video_Time\Output\el arte_17.mp4'
file 'C:\Scripts\Video_Time\Output\el arte_2.mp4'

file 'C:\Scripts\Video_Time\Output\la música_15.mp4'
file 'C:\Scripts\Video_Time\Output\la música_4.mp4'
file 'C:\Scripts\Video_Time\Output\la música_9.mp4'

file 'C:\Scripts\Video_Time\Output\la vida de antes_10.mp4'
file 'C:\Scripts\Video_Time\Output\la vida de antes_16.mp4'
file 'C:\Scripts\Video_Time\Output\la vida de antes_6.mp4'

file 'C:\Scripts\Video_Time\Output\los animales_14.mp4'
file 'C:\Scripts\Video_Time\Output\los animales_3.mp4'


file 'C:\Scripts\Video_Time\Output\los juegos_1.mp4'
file 'C:\Scripts\Video_Time\Output\los juegos_11.mp4'
file 'C:\Scripts\Video_Time\Output\los juegos_5.mp4'

The idea is override the same txt for each group, execute the rest of code and go back and get other group.

Upvotes: 0

Views: 63

Answers (1)

mklement0
mklement0

Reputation: 438113

The following writes the full paths of the files in each group to a file named for the group's grouping criterion, such as el arte.txt for the first group:

Get-ChildItem C:\Scripts\Video_Time\Output -File | 
  Group-Object { $_.Name -replace '_.*$' } | 
    ForEach-Object {
      $groupName = $_.Name
      $_.Group.FullName | ForEach-Object { "file '$_'" } > "$groupName.txt"
    }

Upvotes: 2

Related Questions