简体   繁体   English

是否有一行代码可以使用Powershell更改所有文件夹和子文件夹的文件扩展名?

[英]Is there one line of code for changing all folders and sub folders file extensions using powershell?

I used the following code to batch change one extension in one folder at a time. 我使用以下代码批量更改了一个文件夹中的一个扩展名。

Dir *.mkv | rename-item -newname { $_.name -replace ".mkv",".vlc" }

What I want is to be able to have one line of code that will 我想要的是能够拥有一行代码,

  • change all file extension types to one file extension 将所有文件扩展名类型更改为一个文件扩展名

  • include files inside sub folders 在子文件夹中包含文件

Any help on this would be greatly appreciated, THANKS 在此方面的任何帮助,将不胜感激,谢谢

Use -Recurse to include subfolders. 使用-Recurse包含子文件夹。 Use BaseName which is the filename without extension. 使用BaseName ,即没有扩展名的文件名。
Read the documentation for more info on either 阅读文档以获取有关以下任一内容的更多信息

Get-ChildItem *.mkv -Recurse | Rename-Item -NewName {"$($_.BaseName).vlc"}

Edit 编辑

Apologies, I missed this part: 抱歉,我错过了这一部分:

change all file extension types to one file extension 将所有文件扩展名类型更改为一个文件扩展名

To change all file extensions you just need to exclude the *mkv but as covered by LotPings's answer this isn't recommended; 要更改所有文件扩展名,您只需要排除*mkv但是不建议使用LotPings的答案 it's very easy to change the extension of files you didn't mean to. 更改您不需要的文件扩展名非常容易。 He covers using the -Include parameter, which is the recommended way as it filters early on. 他介绍了-Include参数的使用,这是建议的方法,因为它会尽早过滤。

In the interest of showing another way, you could use the Extension property. 为了显示另一种方法,可以使用Extension属性。 This allows you to leverage all of the PowerShell comparison operators - examples below. 这使您可以利用所有PowerShell 比较运算符 -下面的示例。

Get-ChildItem -Recurse | Where-Object {$_.Extension -eq ".mkv"}
Get-ChildItem -Recurse | Where-Object {$_.Extension -in @(".mkv",".avi")}
Get-ChildItem -Recurse | Where-Object {$_.Extension -ne ".vlc"}

as you also tagged it batch-file : 正如您还将其标记为batch-file

for /r %%a in (*.mkv) do @ECHO ren "%%a" "%%~na.vlc"

(this is batch-file syntax. If you want to use it directly on command line) (这是批处理文件语法。如果要直接在命令行上使用它)

for /r %a in (*.mkv) do @ECHO ren "%a" "%~na.vlc"

Remove @ECHO , if the output fits your needs. 如果输出符合您的需求,请删除@ECHO

I wouldn't change ALL extensions, just use -Include and present a list 我不会更改所有扩展名,只使用-Include并显示一个列表

Get-ChildItem -Path X:\start\here -Recurse -Include ('*.mkv','*.xyz')| Rename-Item -NewName {$_.BaseName+'.vlc'} -WhatIf

If the output looks OK, remove the trailing -WhatIf 如果输出看起来-WhatIf ,请删除结尾的-WhatIf

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 所有子文件夹中每个文件中的 Powershell 更新字符串 - Powershell update string in each file within all sub-folders 有没有最简单的方法使用powershell解压缩文件夹和子文件夹 - Is there any easiest way to unzip folders and sub folders using powershell 如何使用Powershell获取文件夹中所有文件夹名称而不是子文件夹中文件/子文件夹的.txt文件? - How to get a .txt file of all folders names inside a folder but not the files/subfolders inside the child folders using powershell? Powershell删除除一个文件夹之外的所有文件夹 - Powershell delete all folders except one 使用 PowerShell 比较文件夹和子文件夹 - Compare folders and sub-folders with PowerShell 将所有最新文件从文件夹/子复制到 powershell 中的同名文件夹/子 - Copy all latest files from folders/sub to the same name folders/sub in powershell 此Powershell不会将项目计数分配给所有子目录或子目录或子目录 - This powershell doesnt give item count to all the sub or sub-sub or sub-sub-sub folders 在Powershell中的一个zip文件中添加多个文件夹 - Add multiple folders in one zip file in Powershell 使用PowerShell重命名给定目录和子文件夹中所有没有扩展名的文件 - Rename all files with no extension in a given directory and sub-folders using PowerShell 使用 powershell 将子文件夹内容上移一级; 不同的父文件夹 - Using powershell to move sub-folder contents up one level; different parent folders
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM