您现在的位置是:网站首页> 编程资料编程资料
Powershell小技巧之删除不规则字符_PowerShell_
2023-05-26
350人已围观
简介 Powershell小技巧之删除不规则字符_PowerShell_
在路径中,其中有像引号冒号等不规则字符。如果你脚本中的活动路径是来自某段信息,你一定会需要返回符合规则的路径。
这里有段函数教你在任何路径中使用下划线去替换其中不规则部分:
function Get-LegalPathName($Path) { $illegalChars = [System.IO.Path]::GetInvalidFileNameChars() foreach($illegalChar in $illegalChars) { $Path = $Path.Replace($illegalChar, '_') } $Path } 同时来测试一下它的效果:
PS> Get-LegalPathName 'some:"illegal"\path.txt' some__illegal__path_chars_.txt
支持所有PS版本
您可能感兴趣的文章:
相关内容
- Windows Powershell 复制数组_PowerShell_
- Windows Powershell 访问数组_PowerShell_
- PowerShell小技巧之同时使用可选强制参数_PowerShell_
- PowerShell小技巧之添加远程防火墙规则_PowerShell_
- PowerShell小技巧之配置机器的静态IP_PowerShell_
- PowerShell小技巧之启动远程桌面连接_PowerShell_
- Windows Powershell 创建数组_PowerShell_
- Windows Powershell 命令返回数组_PowerShell_
- Windows Powershell 变量的幕后管理_PowerShell_
- Windows Powershell 变量的类型和强类型_PowerShell_
