0

I'm trying to extract partial filename data from a file and create a folder with its name in another directory then move the file to this location.

I'm also looking to check if this file already exists, if it does it will create one.

So far my code works up until the actual Move-Item itself which brings up a path[0] thing.

$TRANSFER = 'C:\Users\BRACEGIRDLE\Favorites\Desktop\TRANSFER'
$TVSHOWS = 'C:\Users\BRACEGIRDLE\Favorites\Desktop\TV_SHOW'

Get-ChildItem "$TRANSFER/*.txt" |
    ForEach{
        $target = $_.Name -split 'S[0-9][0-9].*'
        If( -not (test-path $TRANSFER\$target))
            {
            New-Item -ItemType Directory -Force -Path $TVSHOWS\$target
            }
            Copy-Item -Destination $TVSHOWS\$target
        }

does anyone have any idea what my problem would be.

Filenames are like such

12 Monkeys S02E10 - Fatherland.txt
Colony S02E01 - Eleven Thirteen.txt
Prison Break S05E05 - Contingency.txt

The script splits the file name down to the 'TV Show' and creates a folder into another directory with the show name but it wont move the file?

  • Maybe $_ | Copy-Item -Destination $TVSHOWS\$target? Also check $target.Gettype(). – JosefZ Jun 3 '18 at 17:08
  • When unsure what your commands will do append the -WhatIf or -Confirm parameter and see what the outcome is. – LotPings Jun 3 '18 at 17:10
  • Hi JosefZ, I tried this and didn’t appear to work.. LotPings, I did try this aswell, it doesn’t show anything other than asking for a path[0] on the copy item command – Connor Bracegirdle Jun 3 '18 at 20:19
  • Your copy-item is missing a source object, try Copy-Item -Path $_.Fullname -Destination $TVSHOWS\$target – whatever Jun 5 '18 at 8:58
  • That’s for the reply.. have just tried this and still not working, still getting this path[0] input coming up and nothing is moving – Connor Bracegirdle Jun 5 '18 at 13:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.