替换PS中所有目录和文件的名称 [英] replace names of all directiories and files in PS

查看:64
本文介绍了替换PS中所有目录和文件的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将所有子文件夹和文件的名称中的所有空格字符替换为_".不幸的是,当我输入:

I want to replace all space characters into "_" in names of all subfolders and files. Unfortunately when I type:

Get-ChildItem -recurse -name | ForEach-Object { Rename-Item $_ $_.replace(" ","_") }

错误信息:

Rename-Item:源路径和目标路径必须不同.在线:1字符:60+ Get-ChildItem -recurse -name |ForEach-Object {重命名项目<<<<<<$_ $.replace(" ","") }+ CategoryInfo : WriteError: (PATH_HERE) [Rename-Item], IOException+ FullQualifiedErrorId : RenameItemIOError,Microsoft.PowerShell.Commands.RenameItemCommand

Rename-Item : Source and destination path must be different. At line:1 char:60 + Get-ChildItem -recurse -name | ForEach-Object { Rename-Item <<<< $_ $.replace(" ","") } + CategoryInfo : WriteError: (PATH_HERE) [Rename-Item], IOException + FullyQualifiedErrorId : RenameItemIOError,Microsoft.PowerShell.Commands.RenameItemCommand

我应该如何改进这个短代码?

How I should improve this short code?

推荐答案

不要使用名称开关,它只输出对象的名称,而不是它们的完整路径.试试这个:

Don't use the Name switch, it outputs only the names of the objects, not their full path. Try this:

Get-ChildItem -Recurse | `
   Where-Object {$_.Name -match ' '} | `
     Rename-Item -NewName { $_.Name -replace ' ','_' }

这篇关于替换PS中所有目录和文件的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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