Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
7.4k views
in Technique[技术] by (71.8m points)

powershell - Autofolder downloader

I'm tying to create a script for moving folders with content in them, after they have been downloaded. In other words when I download a movie or Tv series, the script to use a specific mask for detecting if it is either a movie or TV show and then to move them to the according folders for each of the categories. I have been trying to do that on Powershell. The problem is that is not stable. in other words i make it to work and then the next time I need it, it throws some kind of error. Here is what I've got. It is on 2 separate scripts, because this was the only solution, that I can think of for separating TV shows from movies.

This is the first one for TV shows detection:

    $rootFolder = "C:UtorrentReady" ##Mention the root folder name here###

$folderItems = (Get-ChildItem $rootFolder) 

$destination = "F:Seriali"

$subfolderslist = (Get-ChildItem $rootFolder -recurse | Where-Object {$_.PSIsContainer -eq $True -and $_.Name -like "*season*"} | Sort-Object)

foreach ($curfolder in $subfolderslist)
{

    #Write-Host 'Folder Name '$curfolder  yyyy-mm-dd
    $subFolderItems = (Get-ChildItem $curfolder.FullName) 

    foreach($item in $subFolderItems)
    {

      $currentfile=$rootFolder+""+$curfolder+""+$item
      #Write-Host 'file path '$mfilepath
    }
        
    $split = Split-Path -Path $currentfile -Parent 

    Copy-Item -Path $split -Destination $destination -Container -Recurse

    Remove-Item –path $split –recurse -force

}

$subfolderslist = (Get-ChildItem $rootFolder -recurse | Where-Object {$_.PSIsContainer -eq $True -and $_.Name -like "*S0*"} | Sort-Object)

foreach ($curfolder in $subfolderslist)
{

    #Write-Host 'Folder Name '$curfolder  yyyy-mm-dd
    $subFolderItems = (Get-ChildItem $curfolder.FullName) 

    foreach($item in $subFolderItems)
    {

      $currentfile=$rootFolder+""+$curfolder+""+$item
      #Write-Host 'file path '$mfilepath
    }
        
    $split = Split-Path -Path $currentfile -Parent

    Copy-Item -Path $split -Destination $destination -Container -Recurse

    Remove-Item –path $split –recurse -force

}

$subfolderslist = (Get-ChildItem $rootFolder -recurse | Where-Object {$_.PSIsContainer -eq $True -and $_.Name -like "*S1*"} | Sort-Object)

foreach ($curfolder in $subfolderslist)
{

    #Write-Host 'Folder Name '$curfolder  yyyy-mm-dd
    $subFolderItems = (Get-ChildItem $curfolder.FullName) 

    foreach($item in $subFolderItems)
    {

      $currentfile=$rootFolder+""+$curfolder+""+$item
      #Write-Host 'file path '$mfilepath
    }
        
    $split = Split-Path -Path $currentfile -Parent

    Copy-Item -Path $split -Destination $destination -Container -Recurse

    Remove-Item –path $split –recurse -force

}

$ScriptPath = Split-Path $MyInvocation.InvocationName
& "$ScriptPathest4.ps1"

And this is the second one for movies:

$rootFolder = "C:UtorrentReady" ##Mention the root folder name here###

$folderItems = (Get-ChildItem $rootFolder)

$destination = "E:Movies" 

$subfolderslist = (Get-ChildItem $rootFolder -recurse | Sort-Object)


foreach ($curfolder in $subfolderslist)
{

    #Write-Host 'Folder Name '$curfolder  yyyy-mm-dd
    $subFolderItems = (Get-ChildItem $curfolder.FullName) 

    foreach($item in $subFolderItems)
    {

      $currentfile=$rootFolder+""+$curfolder+""+$item
      #Write-Host 'file path '$mfilepath
    }
        
    $split = Split-Path -Path $currentfile -Parent
    Write-Host $split 

    Copy-Item -Path $split -Destination $destination -Container -Recurse

    Remove-Item –path $split –recurse -force

}

$subfolderslist = (Get-ChildItem $rootFolder -recurse | Where-Object {$_.PSIsContainer -eq $True -and $_.Name -notlike "*S0*"} | Sort-Object)


foreach ($curfolder in $subfolderslist)
{

    #Write-Host 'Folder Name '$curfolder  yyyy-mm-dd
    $subFolderItems = (Get-ChildItem $curfolder.FullName) 

    foreach($item in $subFolderItems)
    {

      $currentfile=$rootFolder+""+$curfolder+""+$item
      #Write-Host 'file path '$mfilepath
    }
        
    $split = Split-Path -Path $currentfile -Parent
    Write-Host $split 

    Copy-Item -Path $split -Destination $destination -Container -Recurse

    Remove-Item –path $split –recurse -force

}

$subfolderslist = (Get-ChildItem $rootFolder -recurse | Where-Object {$_.PSIsContainer -eq $True -and $_.Name -notlike "*S1*"} | Sort-Object)


foreach ($curfolder in $subfolderslist)
{

    #Write-Host 'Folder Name '$curfolder  yyyy-mm-dd
    $subFolderItems = (Get-ChildItem $curfolder.FullName) 

    foreach($item in $subFolderItems)
    {

      $currentfile=$rootFolder+""+$curfolder+""+$item
      #Write-Host 'file path '$mfilepath
    }
        
    $split = Split-Path -Path $currentfile -Parent
    Write-Host $split 

    Copy-Item -Path $split -Destination $destination -Container -Recurse

    Remove-Item –path $split –recurse -force

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...