如何在 PowerShell 中重命名文件? [英] How can I rename files in PowerShell?

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

问题描述

我有 2 个要使用 PowerShell 的任务:

I have 2 tasks that I'd like to use PowerShell for:

1 - 我还需要将所有 index.asp 重命名为 Default.aspx

1 - I also need to rename all the index.asp to Default.aspx

2 - 我有一个目录 C:\WWW,我需要在其中递归地将所有 .asp 文件重命名为 .aspx.

2 - I have a directory C:\WWW where I need to rename all the .asp files to .aspx, recursively.

我尝试了 Rename-Item 命令,但总是出错.

I have tried the Rename-Item command but always get errors.

当文件已经存在时无法创建文件

Cannot create a file when that file already exists

如何在 PowerShell 中重命名文件?

How can I rename files in PowerShell?

推荐答案

使用 Get-ChildItem cmdlet 获取所有 index.asp 文件.将结果传送到 Rename-Item cmdlet 并为文件指定新名称.

Use the Get-ChildItem cmdlet to get all index.asp files. Pipe the result to the Rename-Item cmdlet and give the files the new name.

Get-ChildItem c:\www -Filter Index.asp -Recurse | Where-Object {$_.Extension -eq '.asp' } | Rename-Item -NewName Default.aspx

,这也会为您提供 index.aspx 文件,因此将结果通过管道传输到 `Where-Object' cmdlet 并根据文件扩展名进行过滤

, that will get you index.aspx files as well so pipe the result to the `Where-Object' cmdlet and filter based on the file extension

对 asp 文件执行相同的操作,注意现在您也将获得 .aspx 文件,因此将结果通过管道传输到 `Where-Object' cmdlet 并根据文件扩展名进行过滤.在新名称中,只取每个文件的基本名称(不带扩展名)并附加.aspx"

Do the same for asp files, notice that now you will get .aspx files as well so pipe the result to the `Where-Object' cmdlet and filter based on the file extension. in the new name, take just the base name of each file (without extension) and append it '.aspx'

Get-ChildItem c:\www -Filter *.asp -Recurse | Where-Object {$_.Extension -eq '.asp' } | Rename-Item -NewName {$_.BaseName + '.aspx'}

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

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