【问题标题】:Powershell: rename files that contain certain charactorsPowershell:重命名包含某些字符的文件
【发布时间】:2013-12-17 08:31:30
【问题描述】:

我正在从我们的地图驱动器将大量文件上传到 SharePoint 中,SharePoint 显示错误,因为它不喜欢带有“p&l.xls”和两个句号“XMas..xls”字符的文件名。

我想用一个点“.”替换“..”

用“and”代替&

如何更改脚本中的文件名?

【问题讨论】:

    标签: powershell-2.0


    【解决方案1】:

    在powershell中,使用Get-childitem (gci)来获取已有的文件名$files,我们替换掉我们不想要的字符:

    $files = gci -filter "*&*.xls" |select fullname
    foreach ($file in $files) {
       $filename = $file.fullname
       $newFilename = $filename.Replace("&", "and")
       $newFilename = $newfilename.Replace("..", ".")
       rename-item $filename $newFilename
    } 
    

    如果目标文件不在当前工作目录中,您可以使用 -path "LocationWhereYourFilesAre" 指定文件的位置。

    【讨论】:

      猜你喜欢
      • 2022-01-08
      • 1970-01-01
      • 1970-01-01
      • 2015-02-18
      • 2021-09-07
      • 2022-10-23
      • 2019-04-26
      • 2012-11-17
      • 2020-01-24
      相关资源
      最近更新 更多