Powershell脚本将文件夹中所有文件的名称更改为文件夹名称

时间:2018-01-23 13:32:28

标签: powershell

我有一个类似下面的文件夹系统:

Folder A
  Folder.A.S01.E01.MP4
  Folder.A.S01.E02.MP4
  Folder.A.S02.E01.TEST.THIS.MP4
  Folder.A.S02.E01.TEST.THIS.MP4

有没有办法递归文件夹并重命名这样的文件:

Folder A
  Folder A S01E01.MP4
  Folder A S01E02.MP4
  Folder A S02E01.MP4
  Folder A S02E01.MP4

我理解的只是删除了。在文件夹中的所有文件的名称。

我尝试了以下内容,但它删除了。文件扩展名也是“中断”文件。

cd 'C:\Users\ME\Desktop\HERE'
dir -recurse | where {-Not $_.PsIscontainer -AND $_.name -match "."} | 
foreach {
$New=$_.name.Replace("."," ")
Rename-Item -path $_.Fullname -newname $New -passthru
}

提前致谢,

2 个答案:

答案 0 :(得分:5)

FileInfo具有属性BaseNameExtension。前者只包含没有路径的文件名,后者显然是扩展名。将两者结合起来,

$New=$_.BaseName.Replace("."," ")+$_.Extension
Rename-Item -path $_.Fullname -newname $New -passthru

话虽如此,考虑使用下划线而不是空格。使用名为

的文件通常会更容易一些
Folder_A_S02_E01_TEST_THIS.MP4

而不是

Folder A S02 E01 TEST THIS.MP4

答案 1 :(得分:2)

{
    "Envelope": {
        "Header": {
            "WorkContext": {
                "java": {
                    "string": [
                        "weblogic.app.MerchantQueryWebService",
                        "weblogic.workarea.StringWorkContext",
                        "2.0",
                        ""
                    ],
                    "int": "214",
                    "_class": "java.beans.XMLDecoder"
                },
                "_xmlns:wsu": "http://schemas.xmlsoap.org/ws/2002/07/utility",
                "_xmlns:work": "http://bea.com/2004/06/soap/workarea/",
                "__prefix": "work"
            },
            "_xmlns:work": "http://bea.com/2004/06/soap/workarea/",
            "__prefix": "env"
        },
        "Body": {
            "RequestTransactionByTimeIntervalResponse": {
                "RequestTransactionByTimeIntervalResult": {
                    "Status": {
                        "_xmlns:java": "java:com.obopay.ws.merchantquery.zain",
                        "__prefix": "java",
                        "__text": "0"
                    },
                    "TotalTransactions": {
                        "_xmlns:java": "java:com.obopay.ws.merchantquery.zain",
                        "__prefix": "java",
                        "__text": "1"
                    },
                    "TotalAmount": {
                        "_xmlns:java": "java:com.obopay.ws.merchantquery.zain",
                        "__prefix": "java",
                        "__text": "50"
                    },
                    "Transactions": {
                        "_xmlns:java": "java:com.obopay.ws.merchantquery.zain",
                        "__prefix": "java",
                        "__text": "[46139805#254734977477#50#Test]"
                    },
                    "Message": {
                        "_xmlns:java": "java:com.obopay.ws.merchantquery.zain",
                        "__prefix": "java",
                        "__text": "Success"
                    },
                    "__prefix": "m"
                },
                "_xmlns:m": "http://www.zain.com/",
                "__prefix": "m"
            },
            "__prefix": "env"
        },
        "_xmlns:env": "http://schemas.xmlsoap.org/soap/envelope/",
        "__prefix": "env"
    }
}

我将它用于一些类似的处理工作,它将用于非常奇怪的命名文件并且更加健壮,它抓取根文件夹名称并将使用它而不是“猜测”从中删除句点。

Get-ChildItem -Recurse -File | ForEach {
    $Match = [Regex]::Matches($_.BaseName,".*S(\d+).*E(\d+).*")
    $Season = ([int]$Match.Groups[1].Value).ToString("00")
    $Episode = ([int]$Match.Groups[2].Value).ToString("00")
    $RootName = $_.Directory.BaseName

    $NewName = "$($RootName) S$($Season) E$($Episode)$($_.Extension)"
    Rename-Item -LiteralPath $_.FullName -NewName $NewName -WhatIf
}

正如它可以做的那种高级重命名的例子,我真的建议在C:\++\n\Folder1\aaaS01.E02.eee.mp4 -> C:\++\n\Folder1\Folder1 S01 E02.mp4 C:\++\n\Folder1\Folder1S01E03SOMETHING.mp4 -> C:\++\n\Folder1\Folder1 S01 E03.mp4 C:\++\n\Folder1\S01 and it is also E04.mp4 -> C:\++\n\Folder1\Folder1 S01 E04.mp4 C:\++\n\Folder1\S1E5.mp4 -> C:\++\n\Folder1\Folder1 S01 E05.mp4 C:\++\n\Folder1\something S01E01 something.mp4 -> C:\++\n\Folder1\Folder1 S01 E01.mp4 调用中添加-WhatIf并首先运行它以确保没有问题,如果该文件包含多个Rename-ItemS01或类似的内容,它会混淆并无法正确重命名,但您的示例名称都不会出现此问题。

如果您想尝试重命名按字母顺序排列的文件,上面的正则表达式解决方案将不匹配,请尝试以下操作:

E01