【发布时间】:2020-09-14 12:07:59
【问题描述】:
我得到了下面的 powershell 代码,它将文件夹移动和重命名为电视节目文件夹和子季节文件夹,但我希望它保留第一个父年部分(2012 年):
一个例子:
编辑 2:我上传了一个文件夹示例,其中包含和不包含年份,以及带有 .mkv 扩展名的空文件。 powershell 脚本也位于我通常会执行它的位置。 希望我不要通过链接和使用此 gofile 站点来违反任何规则。 (以前没用过)。 只需将其提取到 C: 或其他位置,但如果它不是 C:,则必须更改硬编码的目标变量:
文件夹 --> 绿箭侠 (2012) 第 8 季 S08(1080p BluRay x265 HEVC 10bit AAC 5.1 Vyndros)\
包含文件 --> Arrow.S08E01.Starling.City.1080p.10bit.BluRay.AAC5.1.HEVC-Vyndros.mkv Arrow.S08E02.Welcome.to.Hong.Kong.1080p.10bit.BluRay.AAC5.1.HEVC-Vyndros.mkv Arrow.S08E03.Leap.of.Faith.1080p.10bit.BluRay.AAC5.1.HEVC-Vyndros.mkv Arrow.S08E04.Present.Tense.1080p.10bit.BluRay.AAC5.1.HEVC-Vyndros.mkv 箭头。 S08E05.Prochnost.1080p.10bit.BluRay.AAC5.1.HEVC-Vyndros.mkv Arrow.S08E06.Reset.1080p.10bit.BluRay.AAC5.1.HEVC-Vyndros.mkv Arrow.S08E07.Purgatory.1080p.10bit。 BluRay.AAC5.1.HEVC-Vyndros.mkv Arrow.S08E08.Crisis.on.Infinite.Earths.Part.Four.1080p.10bit.BluRay.AAC5.1.HEVC-Vyndros.mkv Arrow.S08E09.Green.Arrow。 &.The.Canaries.1080p.10bit.BluRay.AAC5.1.HEVC-Vyndros.mkv Arrow.S08E10.Fadeout.1080p.10bit.BluRay.AAC5.1.HEVC-Vyndros.mkv
创建文件夹并将文件移动到 Season 08 文件夹:
箭\第08季\
但我希望它保留 Show 文件夹中的年份部分:
绿箭侠 (2012)\第 08 季\
可以更改代码来做到这一点吗? (必须考虑到(xxxx)部分并不总是在原始名称中,因此代码必须查找:括号开始和数字和括号结束=(xxxx)
文件名不应更改
编辑开始以获得进一步解释: 我将默认从文件夹的根目录运行 powershell 脚本,该脚本应检查显示文件夹。请注意,会有几个不同的节目文件夹,每个文件夹可能包含一个或多个季节 .mkv 文件:
示例:
c:\test\powershell_script.ps1
c:\test\Arrow (2012) 第 8 季 S08(1080p BluRay x265 HEVC 10bit AAC 5.1 Vyndros\Arrow.S08E01.Starling.City.1080p.10bit.BluRay.AAC5.1.HEVC-Vyndros.mkv .
.
.
/
c:\test\Arrow (2012) 第 8 季 S08(1080p BluRay x265 HEVC 10bit AAC 5.1 Vyndros\Arrow.S08E10.Starling.City.1080p.10bit.BluRay.AAC5.1.HEVC-Vyndros.mkv
c:\test\Altered Carbon (2018) 第 2 季 S02 (1080p DS4K NF WEBRip x265 HEVC 10bit DDP 5.1 Vyndros)\Altered.Carbon.S02E01.Phantom.Lady.1080p.10bit.NF.WEBRip.DDP5.1 .HEVC-Vyndros.mkv .
.
.
/
c:\test\Altered Carbon (2018) 第 2 季 S02 (1080p DS4K NF WEBRip x265 HEVC 10bit DDP 5.1 Vyndros)\Altered.Carbon.S02E08.Phantom.Lady.1080p.10bit.NF.WEBRip.DDP5.1 .HEVC-Vyndros.mkv .
.
.其他几个带有文件的显示目录
编辑完成
###############
# make a folder for each different name before the SXX (season part)
# and make a subfolder named Season XX for each SXX part that differs from each other.
#
###############
# $Sourcefolder =
# $DstBase = "c:\!foldertest\" # Destination folder
$DstBase = Read-Host 'what is destination folder ?' # Destination folder
Get-ChildItem -Include *.mkv,*.mp4,*.srt,*.avi,*.txt,*.nfo -Recurse -File| # pick files with these extensions
Where-Object BaseName -match "^(?<Series>.*?)\.?S(?<Season>\d{1,2})\.?E(?<Episode>\d{2})"|
ForEach-Object {
$Destination = "{0}\{1}\Season {2:00}\" -f $DstBase,$Matches.Series.replace('.',' ').Trim(),[int]$Matches.Season
if (!(Test-Path $Destination)){MD $Destination -Force | Out-Null}
"Moving file [{0}] to [{1}]" -f $_.FullName,$Destination # show information to user about files to Move
# popup window asking user for YES / NO confirmation
$message = 'User input'
$question = 'Are you sure you want to proceed?'
$choices = '&Yes', '&No'
$decision = $Host.UI.PromptForChoice($message, $question, $choices, 1)
if ($decision -eq 0) {
Write-Host 'confirmed'
} else {
Write-Host 'cancelled'
}
#
Write-Host 'fortsat'
$_ | Move-Item -Destination $Destination -Force #move files to destination
}
<# Sample tree after running the script:
> tree /F
└───series
└───Breaking Bad
├───Season 01
│ Breaking.Bad.S01E01.DVDRip.XviD-ORPHEUS.avi
│ Breaking.Bad.S01E01.DVDRip.XviD-ORPHEUS.spa.srt
│
├───Season 04
│ Breaking.Bad.S04E01.Box.Cutter.720p.hdtv.x264-orenji.mkv
│ Breaking.Bad.S04E01.Box.Cutter.720p.hdtv.x264-orenji.spa.srt
│
└───Season 05
Breaking.Bad.S05E15.720p.HDTV.x264-EVOLVE.mkv
Breaking.Bad.S05E15.720p.HDTV.x264-EVOLVE.spa.srt
#>
【问题讨论】:
标签: powershell directory rename