【问题标题】:windows 7 find and replace string in files and folderswindows 7 在文件和文件夹中查找和替换字符串
【发布时间】:2017-05-31 14:06:26
【问题描述】:

在 windows powershell ISE 中,我试图替换很多文件名和文件夹名示例中的字符串,我有文件和文件夹,例如

foo_file1.txt
file2_foo.php
file3_foo_file.js
foo/folder1/file.ext
folder2/file4_foo.doc
foo_folder/file5.ppt
folder/foo/file_foo.jpg

例如,我想将所有“foo”更改为“bar”。我使用了windows powershell命令

$dir = "the Path of folder Parent"
CD $dir
Get-Childitem -recurse | 
Where-Object {$_.Name -match "foo"}
rename-item -NewName { $_.Name -replace "foo", "bar" }

但是这个命令不起作用

【问题讨论】:

    标签: powershell cmd windows-7-x64


    【解决方案1】:
    Get-Childitem -recurse | Where-Object {$_.Name -match "foo"} | % {  Rename-Item -NewName ( $_.Name -replace "foo", "bar" ) -Path $_.FullName }
    

    你好像忘记了Rename-ITem-Path参数。

    希望对您有所帮助。

    【讨论】:

      【解决方案2】:

      你也可以这样做:

      $dir = "C:\folderpath"
      (Get-Childitem $dir -recurse |Where-Object {$_.Name -match "foo"}) | 
      rename-item -NewName { $_.BaseName -replace "foo", "bar" }
      

      【讨论】:

        猜你喜欢
        • 2011-05-25
        • 1970-01-01
        • 1970-01-01
        • 2013-02-19
        • 2020-08-12
        • 1970-01-01
        • 2013-06-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多