4

I don't know if this would be best done in PowerShell, but basically I have a lot of movies with names that are incorrect. The folder name for each movie is correct however.

Within a folder, I want to go through each folder and rename an .mp4 file to the same name as the folder.

Each folder has only an .mp4 file in it and a .jpg file, but I want to rename just the .mp4 file (although renaming both really wouldn't be bad either.)

Is there a simple way to do this in PowerShell?

4

3 回答 3

2

可读版本:

Get-ChildItem -Attributes Directory D:\Videos | ForEach-Object {
    Get-ChildItem -Path $_ *.mp4 | Rename-Item -NewName "$_.mp4"
}

第一个Get-ChildItem获取其中的所有目录对象D:\VideosForEach-Object迭代每个目录,如$_下面的块所示。

在块内,再次用于通过选项从给定目录Get-ChildItem中获取文件。最后,用于重命名视频文件而不将其从当前目录中移动。mp4-PathRename-Item

于 2013-06-18T22:15:30.907 回答
2

像这样的东西应该工作:

# run from your D:\Movies (or whatever) folder

# Go through all subfolders of the folder we're currently in, and find all of the .MP4 
# files.  For each .MP4 file we find...
ls -Recurse -Filter *.mp4 | %{ 
    # Get the full path to the MP4 file; use it to find the name of the parent folder.
    # $_ represents the .MP4 file that we're currently working on.
    # Split-Path with the -Parent switch will give us the full path to the parent 
    # folder.  Cast that path to a System.IO.DirectoryInfo object, and get the 
    # Name property, which is just the name of the folder.  
    # There are other (maybe better) ways to do this, this is just the way I chose.
    $name = ([IO.DirectoryInfo](Split-Path $_.FullName -Parent)).Name

    # Tell the user what we're doing...
    Write-Host "Renaming $_ to $($name).mp4..."

    # Rename the file.
    # We have to provide the full path to the file we're renaming, so we use
    # $_.FullName to get it.  The new name of the file is the same as that of the
    # parent folder, which we stored in $name.
    # We also remember to add the .MP4 file extension back to the name.
    Rename-Item -Path $_.FullName -NewName "$($name).mp4"
}
于 2013-06-18T21:47:01.893 回答
2

这是一个跨版本示例:

Get-ChildItem D:\temp\*\*.mp4 | Rename-Item -NewName {$_.Directory.Name +'.mp4'}
于 2013-06-19T06:51:14.433 回答