Remove character from file name in powershell

后端 未结 2 692
不思量自难忘°
不思量自难忘° 2020-12-17 05:00

I\'m new to powershell and wanted to know if there\'s a way to remove a character from a file name. The character I\'m trying to remove is a dash \"-\" and sometimes there

相关标签:
2条回答
  • 2020-12-17 05:40

    To remove or replace characters in a file name use single quotes ' and for "special" characters escape them with a backslash \ so the regular expression parser takes it literally.

    The following removes the $ (dollar sign) from all file names in your current directory:

    Get-Item * | ForEach-Object { rename-item $_ ($_.Name -replace '\$', '') }
    

    the following is the same as above using the shorter alias for each command:

    gi * | % { rni $_ ($_.Name -replace '\$', '') }
    

    The following is removing the standard character "Z" from all file names in your current directory:

    gi * | % { rni $_ ($_.Name -replace 'Z', '') }
    
    0 讨论(0)
  • 2020-12-17 05:41
    Get-Item .\some-file-with-hyphens.txt | ForEach-Object {
      Rename-Item $_ ($_.Name -replace "-", "")
    }
    

    This question may be more suitable for SuperUser.

    0 讨论(0)
提交回复
热议问题