如何更改目录中多个文件的扩展名? [英] How do I change the extension of many files in a directory?

查看:38
本文介绍了如何更改目录中多个文件的扩展名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在一个带有 .txt 扩展名的目录中有大量文件.

Suppose I have a large number of files in a directory with .txt extension.

如何使用以下命令行环境将所有这些文件的扩展名更改为 .c:

How can I change the extension of all these files to .c using the following command line environments:

  • Windows 中的 Powershell
  • Windows 中的 cmd/DOS
  • bash 中的终端

推荐答案

在 Windows 上,转到所需目录,然后键入:

On Windows, go to the desired directory, and type:

ren *.txt *.c

在 PowerShell 中,最好使用 Path.ChangeExtension 方法而不是 -replace(感谢 Ohad Schneider 评论):

In PowerShell, it is better to use the Path.ChangeExtension method instead of -replace (thanks to Ohad Schneider for the remark):

Dir *.txt | rename-item -newname { [io.path]::ChangeExtension($_.name, "c") }

对于 Linux (Bash):

For Linux (Bash):

for file in *.txt
do
 mv "$file" "${file%.txt}.c"
done

这篇关于如何更改目录中多个文件的扩展名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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