[英]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.