如何循环遍历子文件夹并重命名为父文件夹-文件编号

如何循环遍历子文件夹并重命名为父文件夹-文件编号

这是我的文件夹:

Dexter\Season 1\season 1 ep 1  
Dexter\Season 1\season 1 ep 1  
Dexter\Season 2\season 1 ep 1  
Dexter\Season 2\season 1 ep 1  
Dexter\Season 3\season 1 ep 1  
Dexter\Season 3\season 1 ep 1  

我目前在Dexter\

我要做的是循环遍历 Dexter 中的所有子文件夹并相应地重命名所有文件:

Dexter\Season 1\season 1 ep 1 -> Dexter\Season 1\1x1

我该如何做这件事?

答案1

既然我反正要在家里浪费我的夏天,我不妨做点有用的事,并为你编写脚本。假设你使用的是 Windows,以下 PowerShell 脚本将适合你的场景(只需保存为 .ps1 文件并修改顶部的路径)。

$TVShowFolderPath = "X:\Series\Dexter";

$TVShowSeasons=$(get-childitem "$TVShowFolderPath");

foreach( $s in $TVShowSeasons)
{
    if( ($s.PSisContainer) -and ($s -imatch "^(Season )(\d{2}|\d{1})$") )
    {
        $season = $matches[2];
        write-host "Season $season";

        $episodes=$(get-childitem $s.FullName);

        foreach( $ep in $episodes)
        {
            if( $ep -imatch "^(season) (\d{2}|\d{1}) (ep) (\d{2}|\d{1})(.*)$")
            {
                $newName = "$($season)x$($matches[4])$($matches[5])";
                write-host "`tEpisode: `"$($ep.Name)`" --> `"$newName`"";
                Rename-Item $ep.FullName $newName;
            }
        }
    }
}

如果您想先查看它是否吐出正确的名称,只需注释掉以“Rename-Item”开头的行。

相关内容