【问题标题】:How to make Powershell script run in all sub directories?如何使 Powershell 脚本在所有子目录中运行?
【发布时间】:2013-11-20 22:04:07
【问题描述】:

我已经能够拼凑这个脚本来从文件名中删除句点而不影响扩展名。我一直在努力让它在子目录中运行,而不是在每个子目录中手动运行。

这是我一直在使用的脚本:

dir | rename-item -newname {       
       [System.IO.Path]::GetFileNameWithoutExtension($_.name).Replace("."," ") + 
       [System.IO.Path]::GetExtension($_.name); }

最好的,

康纳

【问题讨论】:

    标签: windows powershell windows-server-2008


    【解决方案1】:

    尝试使用 dir -recurse 之类的东西

    【讨论】:

      【解决方案2】:

      您可以使用-Recurse 开关dir / Get-ChildItem 进行递归搜索(搜索子文件夹)。

      我还建议使用 powershell 属性而不是 [System.IO.Path] 函数:

      $_.BaseName     #Filename without extension
      $_.Extension    #File-extension
      

      【讨论】:

        【解决方案3】:
        Get-ChildItem -Recurse | Where-Object { $_.PSIsContainer -eq $false } | Rename-Item -NewName { $_.Basename.replace("."," ") + $_.Extension }
        

        我就是这样做的。

        【讨论】:

          猜你喜欢
          • 2014-01-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-06-11
          • 2014-09-06
          • 2023-01-30
          • 1970-01-01
          相关资源
          最近更新 更多