【问题标题】:Rename certain characters from filenames in all files in a folder and his sub-folders重命名文件夹及其子文件夹中所有文件的文件名中的某些字符
【发布时间】:2020-09-03 10:57:51
【问题描述】:

我想替换我文件中的某些字符 例如

我想改变>“原始”->“其他东西”

我使用此脚本仅使用 powershell 将其替换到一个文件夹中

Dir |>> Rename-Item -NewName { $_.Name -replace "original","something " }

问题是,使用该代码,仅替换一个文件夹中的文件,而不替换子文件夹中的文件 我需要它来替换它在文件夹和子文件夹文件中的文件,这可能吗?

(我对这个有点陌生)

注意:当文件中有任何子文件夹时,此代码会显示此错误

错误 =

Rename-Item : Source and destination path must be different.
At line:2 char:1
+ Rename-Item -NewName { $_.Name -replace "original","something " }
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (F:\folder-name\...ff\py-compat:String) [Rename-Item], IOException
    + FullyQualifiedErrorId : RenameItemIOError,Microsoft.PowerShell.Commands.RenameItemCommand

操作系统:windows

信息

操作系统型号:windows 10

【问题讨论】:

  • 段落的第二行:快速修复>我要更改>“原始”->“某事”
  • Get-ChildItem -Path 'TheRootFolder' -File -Recurse | Rename-Item -NewName ($_.Name -replace 'original', 'something')

标签: windows powershell replace directory


【解决方案1】:

在目录的情况下,rename-item commandlet 获得与输入相同的路径以及-newname 参数的值,因此它会出错。 将-verbose-whatif 开关参数添加到命令将显示。

使用-Path 参数指定要递归的基本路径,-recurse 开关参数以递归方式访问每个子文件夹,-file 开关参数用于过滤目录,和/或使用-filter包含文件子集的参数。

例子:

get-childitem -Recurse -File -Path 'c:\temp' | rename-item -newname { $_.name -replace "original","replacement }

【讨论】:

    猜你喜欢
    • 2017-04-20
    • 2016-07-24
    • 1970-01-01
    • 2015-10-07
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 2013-04-24
    相关资源
    最近更新 更多