Powershell ns 其中对象BaseName-匹配“^(?.*?\?S(?\d{1,2})\?E(?\d{2})”| ForEach对象{ “正在将文件[{0}]移动到[{1}]”-f$\全名,$Destination#向用户显示有关要移动的文件的信息 #如果必须,请打

Powershell ns 其中对象BaseName-匹配“^(?.*?\?S(?\d{1,2})\?E(?\d{2})”| ForEach对象{ “正在将文件[{0}]移动到[{1}]”-f$\全名,$Destination#向用户显示有关要移动的文件的信息 #如果必须,请打,powershell,directory,rename,Powershell,Directory,Rename,ns 其中对象BaseName-匹配“^(?.*?\?S(?\d{1,2})\?E(?\d{2})”| ForEach对象{ “正在将文件[{0}]移动到[{1}]”-f$\全名,$Destination#向用户显示有关要移动的文件的信息 #如果必须,请打开确认弹出窗口 #移动文件 $124;移动项目-目标$Destination-强制#将文件移动到目标 } 我在第一部分遇到以下错误:PS C:\!foldertest>C:\!foldertest\Make\u subfolders\u fro

ns 其中对象BaseName-匹配“^(?.*?\?S(?\d{1,2})\?E(?\d{2})”| ForEach对象{ “正在将文件[{0}]移动到[{1}]”-f$\全名,$Destination#向用户显示有关要移动的文件的信息 #如果必须,请打开确认弹出窗口 #移动文件 $124;移动项目-目标$Destination-强制#将文件移动到目标 }
我在第一部分遇到以下错误:
PS C:\!foldertest>C:\!foldertest\Make\u subfolders\u from\u SXX\u part\u of \u filename\u KEEP\u YEAR.ps1异常调用“Replace”为“2”参数:“字符串长度不能为零。参数名称:oldValue”位于C:\!foldertest\Make子文件夹\u from\u SXX\u part\u of \u filename\u KEEP\u YEAR.ps1:8 char:1+$series=($dstItem.Name-split'seasure')[0]。替换($YEAR.)。修剪…+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~FullyQualifiedErrorId:ArgumentException
@muggi,表示变量
$year
,应该类似于
(2012)
为空。很可能,您使用的文件夹名称没有(年份)部分,或者年份不是4位数,而您的所有示例都有。如果在现实生活中,情况并非如此,那么请给出真实的例子,以便我们知道如何处理。你的问题字面上是这样的:但我希望它将年份部分保留在我实际编写的Show文件夹中“(必须考虑到,(xxxx)部分并非总是在原始名称中,因此代码必须查找:括号开始,数字和括号结束=(xxxx)”但是,是的,我应该更清楚地解释,年份部分并不总是在foldername中,并且给出了更详细的数据样本,sorry@muggi抱歉,我错过了。我已经更新了代码以检查年份部分。感谢您的更新:-)它似乎只创建和更新了0文件夹,并且不移动任何.mkv文件。我试着运行你给出的两部分代码,在一个实例中同时运行两部分代码。(不知道是否对代码的行为有任何影响)。我在我的原始帖子中添加了一个带有小压缩文件的链接,其中包含文件夹和文件名示例。此外,powershell代码也在zip中,因此您可以在我计划运行它的地方找到它!foldertest>C:\!foldertest\Make\u subfolders\u from\u SXX\u part\u of \u filename\u KEEP\u YEAR.ps1异常调用“Replace”为“2”参数:“字符串长度不能为零。参数名称:oldValue”位于C:\!foldertest\Make子文件夹\u from\u SXX\u part\u of \u filename\u KEEP\u YEAR.ps1:8 char:1+$series=($dstItem.Name-split'seasure')[0]。替换($YEAR.)。修剪…+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~FullyQualifiedErrorId:ArgumentException@muggi,表示变量
$year
,应该类似于
(2012)
为空。很可能,您使用的文件夹名称没有(年份)部分,或者年份不是4位数,而您的所有示例都有。如果在现实生活中,情况并非如此,那么请给出真实的例子,以便我们知道如何处理。你的问题字面上是这样的:但我希望它将年份部分保留在我实际编写的Show文件夹中“(必须考虑到,(xxxx)部分并非总是在原始名称中,因此代码必须查找:括号开始,数字和括号结束=(xxxx)”但是,是的,我应该更清楚地解释,年份部分并不总是在foldername中,并且给出了更详细的数据样本,sorry@muggi抱歉,我错过了。我已经更新了代码以检查年份部分。感谢您的更新:-)它似乎只创建和更新了0文件夹,并且不移动任何.mkv文件。我试着运行你给出的两部分代码,在一个实例中同时运行两部分代码。(不知道是否对代码的行为有任何影响)。我在我的原始帖子中添加了一个带有小压缩文件的链接,其中包含文件夹和文件名示例。此外,powershell代码也在zip中,因此可以在我计划从哪里运行它。
###############
# 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
#>
$DstBase = 'D:\Test\Arrow (2012) Season 8 S08 (1080p BluRay x265 HEVC 10bit AAC 5.1 Vyndros)'

# get the source folder as DirectoryInfo object
$dstItem = Get-Item -Path $DstBase
# extract the year, season and series from the source folder name
$year    = ([regex]'(\(\d{4}\))').Match($dstItem.Name).Groups[1].Value
$season  = ([regex]'Season\s+(\d+)').Match($dstItem.Name).Groups[1].Value

# test if the folder name had a (year) part of not
if ([string]::IsNullOrWhiteSpace($year)) {
    $series = ($dstItem.Name -split 'Season')[0].Trim()
    $folder = '{0}\Season {1:00}' -f $series, [int]$season
}
else {
    $series = ($dstItem.Name -split 'Season')[0].Replace($year, '').Trim()
    $folder = '{0} {1}\Season {2:00}' -f $series, $year, [int]$season
}
# and combine that to become a full target path
$Destination = Join-Path -Path $dstItem.Parent.FullName -ChildPath $folder

# create the folder if it does not exist
if (!(Test-Path -Path $Destination -PathType Container)) {
    $null = New-Item -Path $Destination -ItemType Directory
}
Get-ChildItem -Path $DstBase -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 {
      "Moving file [{0}] to [{1}]" -f $_.FullName, $Destination # show information to user about files to Move 

      # do your confirmation popups if you must

      # move the file
      $_ | Move-Item -Destination $Destination -Force    #move files to destination
    }