如何将具有特殊字符的文件名转换为有效的文件名? [英] how to convert filenames with special characters to valid filenames?

查看:1336
本文介绍了如何将具有特殊字符的文件名转换为有效的文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



例如,如果我有一个文件



pre $ 我的演示文稿(fix).xlsx

我想将它重新命名为:

  my presentation fix.xlsx 

感谢您的指导。

我们需要这样做的原因是,我们正在将许多文件迁移到SharePoint 2010中,并且SharePoint不会像特殊字符的文件那样!

解决方案

没有测试过这个代码,但是我想你可能需要类似这样的东西:
$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $函数Rename-Files($ path)
{
Get-ChildItem -path $ path |
Foreach-Object {
$ newName = $ _。name -replace'[^ A-Za-z0-9-_ \.\ [\]]',''$ b $如果(不是($ _。name -eq $ newname)){
Rename-Item -Path $ _。fullname -newname($ newName)
}
}
} #end函数

重命名文件路径C:\somepath



有效地,只需循环目录(修改函数为递归),然后替换任何不适当的(修改appropo的正则表达式)字符并替换它们没有什么(有效地将它们去掉)


I would like to know whether it is possible to REMOVE special characters recursively in powershell from all files in an entire directory structure?

for example if I have a file called:

my presentation (fix).xlsx

I would like to rename it to:

my presentation fix.xlsx

thank you for your guidance.

the reason we need to do this is we are migrating many files into sharepoint 2010 and sharepoint doesnt like files with special characters!

解决方案

I haven't tested this code, but I think you probably want something similar to this:

Function Rename-Files($path)
{
 Get-ChildItem -path $path | 
 Foreach-Object { 
      $newName = $_.name -replace '[^A-Za-z0-9-_ \.\[\]]', ''
      if (-not ($_.name -eq $newname)){
        Rename-Item -Path $_.fullname -newname ($newName) 
      } 
 }
} #end function

Rename-Files -path "C:\somepath"

effectively, just loop through the directory (modify the function to be recursive) and then replace any inappropriate (modify the regex for what is appropo) characters and replace them with nothing (effectively strip them out).

这篇关于如何将具有特殊字符的文件名转换为有效的文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆