在 PowerShell 4.0 中使用文件名重命名文件夹 [英] Renaming folder with filename in PowerShell 4.0

查看:56
本文介绍了在 PowerShell 4.0 中使用文件名重命名文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多文件夹要使用 PowerShell 批量重命名.在每个文件夹中都有许多具有不同扩展名的不同文件.我想要做的是使用 *.nfo 文件名重命名文件夹.

I've got a lot of folders that I want to batch rename using PowerShell. In each folder there are a lot of different files with different extensions. What I want to do is to rename the folder with the *.nfo filename.

示例:

C:\文件夹测试\测试\这些文件位于此文件夹中:
JPG.jpg
NFO.nfo
TXT.txt
WAV.wav

C:\foldertest\TEST\ In this folder these files reside:
JPG.jpg
NFO.nfo
TXT.txt
WAV.wav

运行脚本后,我希望将文件夹重命名为 *.nfo:
C:\文件夹测试\NFO\
我需要一个可以同时处理 > 1 个文件夹的解决方案.

After running a script I want the folder to be renamed like the *.nfo:
C:\foldertest\NFO\
I need a solution that works on > 1 folder at a time.

这就是我所拥有的(不工作 o/c):
Get-ChildItem -Path "C:\foldertest\" |Where-Object{$_.PSisContainer} |ForEach-Object -Process {$new1Name = Get-ChildItem $_ -Filter "*.nfo" |选择对象 -ExpandProperty BaseNameRename-Item -Path $_.FullName -NewName ($_.Name -replace $new1Name}

This is what I have (not working o/c):
Get-ChildItem -Path "C:\foldertest\" | Where-Object{$_.PSisContainer} | ForEach-Object -Process { $new1Name = Get-ChildItem $_ -Filter "*.nfo" | Select-Object -ExpandProperty BaseName Rename-Item -Path $_.FullName -NewName ($_.Name -replace $new1Name }

更新:我仍然有问题.解决方案(见下文)起初似乎有效,但只是有时.假设有 30% 的文件夹.然后这个错误发生在其余部分:
重命名项目:无法重命名,因为C:\Users\STOFFES\Desktop\REN\blablablabla"中的项目不退出英石.在 C:\Users\STOFFES\Desktop\REN\REN.ps1:2 字符:3+ 重命名项目(拆分路径 $_ -Parent)($_.BaseName)+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ CategoryInfo : InvalidOperation: (:) [Rename-Item], PSInvalidOperationException+ FullQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand

UPDATE: I am still having problems. The solution (see below) seemed to work at first, but only sometimes. Let's say on 30% of the folders. Then this error happens on the rest:
Rename-Item : Cannot rename because item at 'C:\Users\STOFFES\Desktop\REN\blablablabla' does not exi st. At C:\Users\STOFFES\Desktop\REN\REN.ps1:2 char:3 + Rename-Item (Split-Path $_ -Parent) ($_.BaseName) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [Rename-Item], PSInvalidOperationException + FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand

即使大多数文件夹中都有 *.nfo 文件.

Even though there are *.nfo files in most of the folders.

推荐答案

Get-ChildItem -Path "E:\Test\Rename" -Include "*.nfo" -Recurse | 
ForEach-Object {
    $oldFolder = $_.DirectoryName

    # New Folder Name is .nfo Filename, excluding extension
    $newFolder = $_.Name.Substring(0, $_.Name.Length - 4)

    # Verify Not Already Same Name
    if ($_.BaseName.ToUpper() -ne $newFolder.ToUpper()) {
        Write-Host "Rename: $oldFolder To: $newFolder"

        Rename-Item -NewName $newFolder -Path $oldFolder
    }
}

您是否有多个级别的子文件夹?这很可能是您收到无法重命名,因为项目不存在"错误的原因,因为您已经重命名了父文件夹.如果是这种情况,您将需要更复杂的东西.

Do you have multiple levels of subfolders? That's most likely why you're getting 'Cannot rename because item does not exist' errors, because you've already renamed a parent folder. If that's the case, you'll need something more complex.

这篇关于在 PowerShell 4.0 中使用文件名重命名文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆