Help me to understand why it's not working

Hi Guys,

first of all a warm welcome to all of you cause I’m new here :slight_smile:

I wonder why my Script is not giving me the expected output. Pre-Conditions of my script --> There is a CSV-file in the same-directory as the script itself which is filled with series I watch, which are saved on my homeserver. Format is the following …

csv_seriesid;csv_seriesname;last_on_tvdb;csv_status;tvdb_query
337907;9-1-1 Notruf L.A;S03E18;Continuing;https://thetvdb.com/search?query=337907
328569;Atlanta Medical;S03E20;Continuing;https://thetvdb.com/search?query=328569
164981;Blue Bloods - Crime Scene New York;S10E19;Continuing;https://thetvdb.com/search?query=164981

My Script is the following which should give me an output which series I do have on my home-server and which series is the last one-listed on TVDB so that I know If I’m behind with my downloads or not.

    $seriespath = "\\hestia\series"
    $seriesfolder = Get-ChildItem $seriespath
    $csv = import-csv .\series.csv -Delimiter ";" -Encoding UTF8
    $lastfilefromseries = @()
    $seriesarray = @()

    foreach ($folder in $seriesfolder) {
        $season = Get-ChildItem $folder.fullname | Where-Object { $_.Name -ne "missing_series.ps1" }
        foreach ($seasonfolder in $season) {
            $files = Get-ChildItem $seasonfolder.fullname | Where-Object { $_.Extension -notmatch "(.nfo|.csv|.ps1)" }
            $lastfile = $files | Select-Object -Last 1
            $lastfilefromseries += $lastfile
        }
    }
    $array = ($lastfilefromseries | Select-Object Name).name

    foreach ($item in $array) {
        $item_series = (($item -split "- (S\d\dE\d\d) - ")[0])
        $item_episodenr = ($item -split "- (S\d\dE\d\d) - ")[1]
        $item_episodename = ($item -split "- (S\d\dE\d\d) - ")[2]
   
        $seriesObject = [PSCustomObject]@{
            Series      = $item_series
            episodenr   = $item_episodenr
            lastontvdb  = ((select-string -path .\series.csv -Pattern $item_series | select line) -split ";")[2]
            episodename = $item_episodename

        }
        $seriesarray += $seriesObject
    }
    $seriesarray | Sort-Object -Property Series -Unique | Format-Table -Autosize

The only thing which is not working is the value “lastontvdb” in my custom-object (it’s simply empty in the output). Maybe you can help me out with that?

Thx in advance!
Florian

Hi Florian,

And welcome, i’am new too on this community.

For your problem, why did you don’t use your variable $csv ?
You’ve got all informations here, with the cmdlet Import-CSV.

$csv


csv_seriesid   : 337907
csv_seriesname : 9-1-1 Notruf L.A
last_on_tvdb   : S03E18
csv_status     : Continuing
tvdb_query     : https://thetvdb.com/search?query=337907

csv_seriesid   : 328569
csv_seriesname : Atlanta Medical
last_on_tvdb   : S03E20
csv_status     : Continuing
tvdb_query     : https://thetvdb.com/search?query=328569

csv_seriesid   : 164981
csv_seriesname : Blue Bloods - Crime Scene New York
last_on_tvdb   : S10E19
csv_status     : Continuing
tvdb_query     : https://thetvdb.com/search?query=164981

Regards,
Jocelyn

 

[quote quote=263807]Hi Florian,

And welcome, i’am new too on this community.

For your problem, why did you don’t use your variable $csv ?

You’ve got all informations here, with the cmdlet Import-CSV.

<textarea class=“urvanov-syntax-highlighter-plain print-no” style=“tab-size: 4; font-size: 14px !important; line-height: 18px !important; z-index: 0; opacity: 0;” readonly=“readonly” data-settings=“dblclick”>$csv

csv_seriesid : 337907
csv_seriesname : 9-1-1 Notruf L.A
last_on_tvdb : S03E18
csv_status : Continuing
tvdb_query : https://thetvdb.com/search?query=337907

csv_seriesid : 328569
csv_seriesname : Atlanta Medical
last_on_tvdb : S03E20
csv_status : Continuing
tvdb_query : https://thetvdb.com/search?query=328569

csv_seriesid : 164981
csv_seriesname : Blue Bloods - Crime Scene New York
last_on_tvdb : S10E19
csv_status : Continuing
tvdb_query : https://thetvdb.com/search?query=164981</textarea>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$csv
csv_seriesid : 337907
csv_seriesname : 9-1-1 Notruf L.A
last_on_tvdb : S03E18
csv_status : Continuing
tvdb_query : https://thetvdb.com/search?query=337907
csv_seriesid : 328569
csv_seriesname : Atlanta Medical
last_on_tvdb : S03E20
csv_status : Continuing
tvdb_query : https://thetvdb.com/search?query=328569
csv_seriesid : 164981
csv_seriesname : Blue Bloods - Crime Scene New York
last_on_tvdb : S10E19
csv_status : Continuing
tvdb_query : https://thetvdb.com/search?query=164981
Regards,

Jocelyn

[/quote]
Hey Jocelyn,

Also tried this of course - but also with no result. I tried with something like

($csv -match "9-1-1").last_on_tvdb

which works on console directly but not in the script - or let me say not as expected.

This is the output then ?!

Series                           episodenr lastontvdb episodename
------                           --------- ---------- -----------
9-1-1 Notruf L.A.                S03E10    S03E18     Wendepunkte.mkv
Atlanta Medical                  S02E23               Sinneswandel.mkv
Atlanta Medical                  S03E14               Der Floh.mkv
Chicago Fire                     S08E20               Nächste Schritte.mkv
Chicago Med                      S05E20               Alte Wunden.mkv
Chicago PD                       S07E20               Todesstille.mkv
Criminal Minds                   S15E10               Der Anfang vom Ende (2).mkv
FBI Special Crime Unit           S02E11               Todesengel.mkv
Grey's Anatomy                   S16E21               Gute Miene, böses Spiel.mkv
Hawaii Five-0                    S10E22               Aloha Oe.mkv
Lincoln Rhyme Der Knochenjäger   S01E10               Auge in Auge (2).mkv
Lucifer                          S05E08               Spoileralarm.mp4
Marvel's Agents of S.H.I.E.L.D.  S03E22    S07E13     Himmelfahrtskommando (2).mkv
Marvel's Agents of S.H.I.E.L.D.  S04E22    S07E13     Das Ende der Welt.mkv
Mysterious Mermaids              S02E16               Schöne neue Welt.mkv
Mysterious Mermaids              S03E04               Ryns Baby!.mkv
New Amsterdam                    S02E09               Die Insel.mkv
SEAL Team                        S03E20               Wofür man kämpft.mkv
The 100                          S04E13               Praimfaya.mkv
The 100                          S05E13               Damokles (2).mkv
The 100                          S06E13               Das Blut Sanctums.mkv
The Blacklist                    S05E22               Sutton Ross (Nr. 17).mkv
The Blacklist                    S06E22               Robert Diaz (Nr. 15).mkv
The Blacklist                    S07E19               Die Kazanjian-Brüder.mkv
The Flash (2014)                 S05E22               Das Vermächtnis.mkv
The Flash (2014)                 S06E19               Der Erfolg ist garantiert.mkv
The Good Doctor                  S02E18               Trampolin.mkv
The Good Doctor                  S03E18               Willkommen im Club.mkv
The Walking Dead                 S07E16               Heute beginnt der Rest des Lebens.mkv
The Walking Dead                 S08E16               Zorn.mkv
The Walking Dead                 S09E16               Der Sturm.mkv
The Walking Dead                 S10E15               Prinzessin.mkv

 

Thx

What is the strucuture of your variable $array ?
I think there is a mistake with your regex when you apply the split.
You may to use both your $csv varable and your $array variable to filter and then extract the given value.

my array looks like that -->

PS Microsoft.PowerShell.Core\FileSystem::\\hestia\series> .\missing_series.ps1 -last
9-1-1 Notruf L.A. - S03E10 - Wendepunkte.mkv
Atlanta Medical - S02E23 - Sinneswandel.mkv
Atlanta Medical - S03E14 - Der Floh.mkv
Chicago Fire - S08E20 - Nächste Schritte.mkv
Chicago Med - S05E20 - Alte Wunden.mkv
Chicago PD - S07E20 - Todesstille.mkv
Criminal Minds - S15E10 - Der Anfang vom Ende (2).mkv
FBI Special Crime Unit - S02E11 - Todesengel.mkv
Grey's Anatomy - S16E21 - Gute Miene, böses Spiel.mkv
Hawaii Five-0 - S10E22 - Aloha Oe.mkv
Lincoln Rhyme Der Knochenjäger - S01E10 - Auge in Auge (2).mkv
Lucifer - S05E08 - Spoileralarm.mp4
Marvel's Agents of S.H.I.E.L.D. - S03E22 - Himmelfahrtskommando (2).mkv
Marvel's Agents of S.H.I.E.L.D. - S04E22 - Das Ende der Welt.mkv
Mysterious Mermaids - S02E16 - Schöne neue Welt.mkv
Mysterious Mermaids - S03E04 - Ryns Baby!.mkv
New Amsterdam - S02E09 - Die Insel.mkv
SEAL Team - S03E20 - Wofür man kämpft.mkv
The 100 - S04E13 - Praimfaya.mkv
The 100 - S05E13 - Damokles (2).mkv
The 100 - S06E13 - Das Blut Sanctums.mkv
The Blacklist - S05E22 - Sutton Ross (Nr. 17).mkv
The Blacklist - S06E22 - Robert Diaz (Nr. 15).mkv
The Blacklist - S07E19 - Die Kazanjian-Brüder.mkv
The Flash (2014) - S05E22 - Das Vermächtnis.mkv
The Flash (2014) - S06E19 - Der Erfolg ist garantiert.mkv
The Good Doctor - S02E18 - Trampolin.mkv
The Good Doctor - S03E18 - Willkommen im Club.mkv
The Walking Dead - S07E16 - Heute beginnt der Rest des Lebens.mkv
The Walking Dead - S08E16 - Zorn.mkv
The Walking Dead - S09E16 - Der Sturm.mkv
The Walking Dead - S10E15 - Prinzessin.mkv

That’s why I’m splitting with regex to get it “more structured”

Ok, Here is my approch:

foreach ($item in $array)
{
    $regex = "- (S\d\dE\d\d) - "
    $item_series = (($item -split $regex )[0])
    $item_episodenr = ($item -split $regex)[1]
    $item_episodename = ($item -split $regex)[2]

    $lastontvdb = ($csv | ? { $_.csv_seriesname -eq $item_series.trim() }).last_on_tvdb

    $seriesObject = [PSCustomObject]@{
        Series      = $item_series
        episodenr   = $item_episodenr
        lastontvdb  = $lastontvdb
        episodename = $item_episodename

    }
    $seriesarray += $seriesObject
}
$seriesarray | Sort-Object -Property Series -Unique | Format-Table -Autosize

I use the $csv variable to compare the csv_seriesname field to the first split of your array and then take the last_on_tvdb from your $csv variable.
You must take a attention of the integrity of your csv file the field csv_seriesname is not perfectly egal to the data feed on your array.

For example:

On CSV csv_seriesname --> 9-1-1 Notruf L.A
On your array $item_series --> 9-1-1 Notruf L.A.
There is a dot on the end of your data, so the statement can’t be true and you can’t get your value.

Hi,

Your approach is a good one. The difference in both data sources results in one simple fact.
There is no “.” allowed as last character in a directory at windows-filesystems - I got the data from the series-db from thetvdb. I’ll try to find a workaround on that on my own.

I would to thank you very very much on this!

it works this way -->

$pattern = $item_series.trim()
$lastontvdb = ($csv | Where-Object { $_.csv_seriesname -like "*$pattern*" }).last_on_tvdb
there is just NO match for the word "Knochenjäger" although I used UTF8 as encoding ... strange

The character ä to UTF8 return something not readable.

Knochenjäger - UTF8 -> Knochenjäger

Maybe applying a regex for theses special characters.

I found this https://community.idera.com/database-tools/powershell/powertips/b/tips/posts/replacing-special-chars-like-umlauts

[quote quote=263867]I found this https://community.idera.com/database-tools/powershell/powertips/b/tips/posts/replacing-special-chars-like-umlauts

[/quote]
Hey,

thx for your insights. I’ll try to get this working. But the main work is done (it’s not that tragical for this one “bone-collector” series :))

Cheers & Thx