批处理之家's Archiver

gflrlm 发表于 2020-1-4 23:09

powershell根据内容重命名bt torrent文件

有时候下载torrent种子文件,名字无意义,所以可以用ps脚本处理一下,重命名为正确的名字,代码如下,需要设置种子文件所在文件夹[code]   
  $Path ="D:\"
  $Filter = '*.torrent'

$ar = New-Object -TypeName System.Collections.ArrayList

function processing_grep(  $file ){
  
      #write-host "file_name=" $file
  
    $tmpContent = Get-Content -Encoding utf8  $file
      $movie_name_full=""

  
    for ($i=0; $i -le $tmpContent.length; $i++)
    {


        if($tmpContent[$i] -match "name\d+:(.*?):name") #   "name\d+:(.*?):name" 
         {
          #write-host ""
                $movie_name_full=$matches[1]
                #write-host "------------------------------------------------------------------------------"
          #write-host $file : " $i " :
          #write-host $file " : " $i " : " $tmpContent[$i] -ForegroundColor yellow -background black
                ###write-host "$spaces-- movie_name_regex = $matches[0] "
                write-host "$spaces-- movie_name_full  = $movie_name_full "
                            if($ar.Contains($movie_name_full)) {
                        ###write-host "$spaces-- 含有重复复名字:$movie_name_full "  -ForegroundColor red -background black
                    ###"$file , movie_name = $movie_name_full" | Out-File -Append "repeat_torrent.txt"
                    } else {
                    ###write-host "$spaces-- 添加元素: $movie_name_full "    -ForegroundColor green
                    ###$ar.Add($movie_name_full)
                                        ###mv -path "$file" -dest "$movie_name_full.torrent"
                    }
                               
                #write-host "$spaces------------------------------------------------------------------------------"
                $found = 1
                                break
         } elseif($tmpContent[$i] -match "name.utf.*?:(.*?)12:piece") {
          #write-host ""
                $movie_name_full=$matches[1]
                #write-host "------------------------------------------------------------------------------"
          #write-host $file : " $i " :
          #write-host $file " : " $i " : " $tmpContent[$i] -ForegroundColor yellow -background black
                ###write-host "$spaces-- movie_name_regex = $matches[0] "
                write-host "$spaces-- movie_name_full  = $movie_name_full "
                            if($ar.Contains($movie_name_full)) {
                        ###write-host "$spaces-- 含有重复复名字:$movie_name_full "  -ForegroundColor red -background black
                    ###"$file , movie_name = $movie_name_full" | Out-File -Append "repeat_torrent.txt"
                    } else {
                    ###write-host "$spaces-- 添加元素: $movie_name_full "    -ForegroundColor green
                    ###$ar.Add($movie_name_full)
                                        ###mv -path "$file" -dest "$movie_name_full.torrent"
                    }
                               
                #write-host "$spaces------------------------------------------------------------------------------"
                $found = 1
                                break

         } elseif($tmpContent[$i] -match "name\d+:(.*?)piece:") {
          #write-host ""
                $movie_name_full=$matches[1]
                #write-host "------------------------------------------------------------------------------"
          #write-host $file : " $i " :
          #write-host $file " : " $i " : " $tmpContent[$i] -ForegroundColor yellow -background black
                ###write-host "$spaces-- movie_name_regex = $matches[0] "
                write-host "$spaces-- movie_name_full  = $movie_name_full "
                            if($ar.Contains($movie_name_full)) {
                        ###write-host "$spaces-- 含有重复复名字:$movie_name_full "  -ForegroundColor red -background black
                    ###"$file , movie_name = $movie_name_full" | Out-File -Append "repeat_torrent.txt"
                    } else {
                    ###write-host "$spaces-- 添加元素: $movie_name_full "    -ForegroundColor green
                    ###$ar.Add($movie_name_full)
                                        ###mv -path "$file" -dest "$movie_name_full.torrent"
                    }
                               
                #write-host "$spaces------------------------------------------------------------------------------"
                $found = 1
                                break

             } else {
                continue
                write-host "222------------------------------------------------------------------------------"
          write-host $file : " 资源名称找不到,请检查正则表达式是否遗漏。 " :  -ForegroundColor red -background black
                            cmd /c "pause"
                                #exit
                         }
             #cmd /c "pause"

     }#for
  }
  

,@(Get-ChildItem  -Attributes A -path $Path -Filter $Filter -Recurse | ?{$_.PsIsContainer -eq $false}| %{ processing_grep (escape_path_special_char_to____string_for_get_childitem ($_.FullName) )} )[/code]

lililala6868 发表于 2020-8-15 17:33

**** 作者被禁止或删除 内容自动屏蔽 ****

页: [1]

Powered by Discuz! Archiver 7.2  © 2001-2009 Comsenz Inc.