找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 16526|回复: 1

[原创代码] powershell根据内容重命名bt torrent文件

[复制链接]
发表于 2020-1-4 23:09:39 | 显示全部楼层 |阅读模式
有时候下载torrent种子文件,名字无意义,所以可以用ps脚本处理一下,重命名为正确的名字,代码如下,需要设置种子文件所在文件夹
  1.    
  2.   $Path ="D:"
  3.   $Filter = '*.torrent'

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

  5. function processing_grep(  $file ){
  6.   
  7.       #write-host "file_name=" $file
  8.   
  9.     $tmpContent = Get-Content -Encoding utf8  $file
  10.       $movie_name_full=""

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


  14.         if($tmpContent[$i] -match "name\d+:(.*?):name") #   "name\d+:(.*?):name" 
  15.          {
  16.           #write-host ""
  17.                 $movie_name_full=$matches[1]
  18.                 #write-host "------------------------------------------------------------------------------"
  19.           #write-host $file : " $i " :
  20.           #write-host $file " : " $i " : " $tmpContent[$i] -ForegroundColor yellow -background black
  21.                 ###write-host "$spaces-- movie_name_regex = $matches[0] "
  22.                 write-host "$spaces-- movie_name_full  = $movie_name_full "
  23.                             if($ar.Contains($movie_name_full)) {
  24.                         ###write-host "$spaces-- 含有重复复名字:$movie_name_full "  -ForegroundColor red -background black
  25.                     ###"$file , movie_name = $movie_name_full" | Out-File -Append "repeat_torrent.txt"
  26.                     } else {
  27.                     ###write-host "$spaces-- 添加元素: $movie_name_full "    -ForegroundColor green
  28.                     ###$ar.Add($movie_name_full)
  29.                                         ###mv -path "$file" -dest "$movie_name_full.torrent"
  30.                     }
  31.                                
  32.                 #write-host "$spaces------------------------------------------------------------------------------"
  33.                 $found = 1
  34.                                 break
  35.          } elseif($tmpContent[$i] -match "name.utf.*?:(.*?)12:piece") {
  36.           #write-host ""
  37.                 $movie_name_full=$matches[1]
  38.                 #write-host "------------------------------------------------------------------------------"
  39.           #write-host $file : " $i " :
  40.           #write-host $file " : " $i " : " $tmpContent[$i] -ForegroundColor yellow -background black
  41.                 ###write-host "$spaces-- movie_name_regex = $matches[0] "
  42.                 write-host "$spaces-- movie_name_full  = $movie_name_full "
  43.                             if($ar.Contains($movie_name_full)) {
  44.                         ###write-host "$spaces-- 含有重复复名字:$movie_name_full "  -ForegroundColor red -background black
  45.                     ###"$file , movie_name = $movie_name_full" | Out-File -Append "repeat_torrent.txt"
  46.                     } else {
  47.                     ###write-host "$spaces-- 添加元素: $movie_name_full "    -ForegroundColor green
  48.                     ###$ar.Add($movie_name_full)
  49.                                         ###mv -path "$file" -dest "$movie_name_full.torrent"
  50.                     }
  51.                                
  52.                 #write-host "$spaces------------------------------------------------------------------------------"
  53.                 $found = 1
  54.                                 break

  55.          } elseif($tmpContent[$i] -match "name\d+:(.*?)piece:") {
  56.           #write-host ""
  57.                 $movie_name_full=$matches[1]
  58.                 #write-host "------------------------------------------------------------------------------"
  59.           #write-host $file : " $i " :
  60.           #write-host $file " : " $i " : " $tmpContent[$i] -ForegroundColor yellow -background black
  61.                 ###write-host "$spaces-- movie_name_regex = $matches[0] "
  62.                 write-host "$spaces-- movie_name_full  = $movie_name_full "
  63.                             if($ar.Contains($movie_name_full)) {
  64.                         ###write-host "$spaces-- 含有重复复名字:$movie_name_full "  -ForegroundColor red -background black
  65.                     ###"$file , movie_name = $movie_name_full" | Out-File -Append "repeat_torrent.txt"
  66.                     } else {
  67.                     ###write-host "$spaces-- 添加元素: $movie_name_full "    -ForegroundColor green
  68.                     ###$ar.Add($movie_name_full)
  69.                                         ###mv -path "$file" -dest "$movie_name_full.torrent"
  70.                     }
  71.                                
  72.                 #write-host "$spaces------------------------------------------------------------------------------"
  73.                 $found = 1
  74.                                 break

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

  83.      }#for
  84.   }
  85.   

  86. ,@(Get-ChildItem  -Attributes A -path $Path -Filter $Filter -Recurse | ?{$_.PsIsContainer -eq $false}| %{ processing_grep (escape_path_special_char_to____string_for_get_childitem ($_.FullName) )} )
复制代码

评分

参与人数 1技术 +1 收起 理由
老刘1号 + 1 感谢分享

查看全部评分

发表于 2020-8-15 17:33:38 | 显示全部楼层
有时候下载torrent种子上海快3https://1680380.com/view/kuai3_shft/kuai3_index.html文件,名字无意义,所以可以用ps脚本处理一下,重命名为正确的名字,幸运飞艇https://1680380.com/view/xingyft/pk10kai.html代码如下,需要设置种子文件所在文件夹
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-16 21:24 , Processed in 0.017265 second(s), 8 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表