在 Windows 上使用一个命令更改文件夹中所有文件的扩展名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9885241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 09:23:30  来源:igfitidea点击:

Changing all files' extensions in a folder with one command on Windows

windowscmdjpeg

提问by Berker Yüceer

How can I use the Windows command line to change the extensions of thousands of files to *****.jpg?

如何使用 Windows 命令行将数千个文件的扩展名更改为*****.jpg

回答by keyser

You can use ren(as in rename):

您可以使用ren(如rename):

ren *.XXX *.YYY

And of course, switch XXX and YYY for the appropriate extensions. It will change fromXXX toYYY. If you want to change all extensions, just use the wildcard again:

当然,切换 XXX 和 YYY 以获得适当的扩展名。它将XXX变为YYY。如果要更改所有扩展名,只需再次使用通配符:

ren *.* *.YYY

One way to make this work recursively is with the FORcommand. It can be used with the /Roption to recursively apply a command to matching files. For example:

使这项工作递归的一种方法是使用FOR命令。它可以与/R选项一起使用以递归地将命令应用于匹配的文件。例如:

for /R %x in (*.txt) do ren "%x" *.renamed

for /R %x in (*.txt) do ren "%x" *.renamed

will change all .txtextensions to .renamedrecursively, starting in the current directory. %xis the variable that holds the matched file names.

将从当前目录开始,将所有.txt扩展更改为.renamed递归。 %x是保存匹配文件名的变量。

And, since you have thousands of files, make sure to wait until the cursor starts blinking again indicating that it's done working.

而且,由于您有数千个文件,请务必等到光标再次开始闪烁,表明它已完成工作。

Note: this works only on cmd. Won't work on Powershell or Bash

注意:这仅适用于 cmd。不适用于 Powershell 或 Bash

回答by Habib

on CMD

在 CMD 上

type

类型

ren *.* *.jpg

.will select all files, and rename to * (what ever name they have) plus extension to jpg

. 将选择所有文件,并重命名为 *(它们有什么名称)加上扩展名为 jpg

回答by PictureGuy

Rename behavior is sometimes 'less than intuitive'; for example...

重命名行为有时“不太直观”;例如...

ren *.THM *.jpg will rename your THM files to have an extension of .jpg. eg: GEDC003.THM will be GEDC003.jpg

ren *.THM *.jpg 将重命名您的 THM 文件,使其扩展名为 .jpg。例如:GEDC003.THM 将是 GEDC003.jpg

ren *.THM *b.jpg will rename your THM files to *.THMb.jpg. eg: GEDC004.THM will become GEDC004.THMb.jpg

ren *.THM *b.jpg 会将您的 THM 文件重命名为 *.THMb.jpg。例如:GEDC004.THM 将变成 GEDC004.THMb.jpg

ren *.THM *.b.jpg will rename your THM files to *.b.jpg eg: GEDC005.THM will become GEDC005.b.jpg

ren *.THM *.b.jpg 将您的 THM 文件重命名为 *.b.jpg 例如:GEDC005.THM 将成为 GEDC005.b.jpg

回答by gbonetti

NOTE: not for Windows

注意:不适用于 Windows

Using ren-1.0 the correct form is:

使用 ren-1.0 的正确形式是:

"ren *.*" "#2.jpg"

From man ren

man ren

The replacement pattern is another filename with embedded wildcard indexes, each of which consists of the character # followed by a digit from 1 to 9. In the new name of a matching file, the wildcard indexes are replaced by the actual characters that matched the referenced wildcards in the original filename.

替换模式是另一个带有嵌入通配符索引的文件名,每个文件名由字符 # 后跟一个从 1 到 9 的数字组成。在匹配文件的新名称中,通配符索引由与引用匹配的实际字符替换原始文件名中的通配符。

and

Note that the shell normally expands the wildcards * and ?, which in the case of ren is undesirable. Thus, in most cases it is necessary to enclose the search pattern in quotes.

请注意,shell 通常会扩展通配符 * 和 ?,这在 ren 的情况下是不可取的。因此,在大多数情况下,需要将搜索模式括在引号中。

回答by Imran Rizvi

thats simple

那很简单

ren *.* *.jpg

try this in command prompt

在命令提示符下试试这个

回答by Jonathan

Just for people looking to do this in batch files, this code is working:

仅对于希望在批处理文件中执行此操作的人,此代码有效:

FOR /R "C:\Users\jonathan\Desktop\test" %%f IN (*.jpg) DO REN "%%f" *.png

In this example all files with .jpg extensions in the C:\Users\jonathan\Desktop\test directory are changed to *.png.

在此示例中,C:\Users\jonathan\Desktop\test 目录中所有扩展名为 .jpg 的文件都更改为 *.png。

回答by nanduvtlr

Rename multiple file extensions:

重命名多个文件扩展名:

You want to change ringtone1.mp3, ringtone2.mp3to ringtone1.wav, ringtone2.wav

你想改变ringtone1.mp3ringtone2.mp3ringtone1.wavringtone2.wav

Here is how to do that: I am in d drive on command prompt (CMD) so I use:

以下是如何做到这一点:我在命令提示符 (CMD) 的 d 驱动器中,所以我使用:

d:\>ren *.* *.wav 

This is just an example of file extensions, you can use any type of file extension like WAV, MP3, JPG, GIF, bmp, PDF, DOC, DOCX, TXT this depends on what your operating system.

这只是文件扩展名的一个例子,您可以使用任何类型的文件扩展名,如 WAV、MP3、JPG、GIF、bmp、PDF、DOC、DOCX、TXT,这取决于您的操作系统。

And, since you have thousands of files, make sure to wait until the cursor starts blinking again indicating that it's done working.

而且,由于您有数千个文件,请务必等到光标再次开始闪烁,表明它已完成工作。

回答by Marc.2377

In my case I had a directory with 800+ files ending with .StoredProcedure.sql(they were scripted with SSMS).

就我而言,我有一个包含 800 多个文件的目录.StoredProcedure.sql(它们是用SSMS编写的)。

The solutions posted above didn't work. But I came up with this:

上面发布的解决方案不起作用。但我想出了这个:

(Based on answers to batch programming - get relative path of file)

(基于批处理编程的答案- 获取文件的相对路径

@echo off
setlocal enabledelayedexpansion
for %%f in (*) do (
  set B=%%f
  set B=!B:%CD%\=!
  ren "!B!" "!B:.StoredProcedure=!"
)

The above script removes the substring .StoredProcedurefrom the filename. I'm sure it can be adapted to cover more cases, ask for input and be overall more generic.

上面的脚本.StoredProcedure从文件名中删除子字符串。我相信它可以适用于涵盖更多情况、征求意见并总体上更通用。

回答by Mohamed

I know this is so old, but i've landed on it , and the provided answers didn't works for me on powershell so after searching found this solution

我知道这太旧了,但我已经找到了,并且提供的答案在 powershell 上对我不起作用,因此在搜索后找到了这个解决方案

to do it in powershell

在powershell中做到这一点

Get-ChildItem -Path C:\Demo -Filter *.txt | Rename-Item -NewName {[System.IO.Path]::ChangeExtension($_.Name, ".old")}

credit goes to http://powershell-guru.com/powershell-tip-108-bulk-rename-extensions-of-files/

归功于http://powershell-guru.com/powershell-tip-108-bulk-rename-extensions-of-files/

回答by Sagar Gavhane

An alternative way to rename files using the renamernpm package.

使用renamernpm 包重命名文件的另一种方法。

below is an example of renaming files extensions

以下是重命名文件扩展名的示例

renamer -d --path-element ext --find ts --replace js *