2

我编写了一个 Powershell 脚本,它接收 N 个字符,这些字符必须被用户的另一个给定字符替换。例如,用户想要替换 (, ) 和 -。用户通过此循环输入必须替换的字符:

#Declare array for special characters to replace
$replaceChars = @( )

#Instruct the user about entering characters to replace
Write-Host
Write-Host "Please enter what you would like to replace in your names. This can contain one or more characters. Example: (2) or ^ or _" -ForegroundColor Cyan
Write-Host "Enter -1 to end your input and continue." -ForegroundColor Cyan

#Get values until sentinal is passed
$input = "" #input from user declared to blank

#loop for special characters
while($input -ne -1) {

    #get the input from the user
    $input = Read-Host -Prompt "Enter what you would like to replace (-1 to finish)"

    #if the input isn't sentinal, put it in the replaceChars array
    if($replaceChars.Length -gt 0 -and $input -eq -1) {
        Write-Host
        Write-Host "Your entries have been stored." -ForegroundColor Green
    } #end-if
    elseif($replaceChars.Length -eq 0 -and $input -eq -1) {
        Write-Host
        Write-Host "ERROR: You must enter at least one character to continue." -ForegroundColor Red
        $input = $NULL
    }
    elseif($input -eq '') {
    Write-Host
    Write-Host "Invalid entry. Entry cannot be blank, please enter a value." -ForegroundColor Red
    }#end-elseif
    elseif($input.IndexOfAny([System.IO.Path]::GetInvalidFileNameChars()) -ge 0) {
        Write-Host
        Write-Host "Invalid entry. File names cannot contain / \ : * ? `" < > |" -ForegroundColor Red
    }#end-elseif
    else {
        $replaceChars += $input
    } #end-else

}#end-while

然后用户通过以下代码输入替换字符:

#Get the char to replace to
Write-Host
Write-Host "Please enter what you want to replace the selected characters with. Leave blank and hit enter to delete the old characters." -ForegroundColor Cyan
$newChar = Read-Host -Prompt "Please enter new character(s)"
while($newChar.IndexOfAny([System.IO.Path]::GetInvalidFileNameChars()) -ge 0) {
    Write-Host
    Write-Host "The entry is invalid for file names. File names cannot contain / \ : * ? `" < > |" -ForegroundColor Red
    $newChar = Read-Host -Prompt "Please enter new character(s)"
}
Write-Host
Write-Host "New character has been stored" -ForegroundColor Green

然后我正在处理这些条目:

#Iterate through each character 
foreach($char in $replaceChars) {
    if($type -eq 1) {
        gci -File -Path "$path" | Where-Object { $_.Name -match $char } | ForEach-Object { $_ | rename-item  -NewName $_.Name.Replace($char, $newChar) }
    } elseif ($type -eq 2) { #end-if
        gci -Directory -Path "$path" | Where-Object { $_.Name -match $char } | ForEach-Object { $_ | rename-item  -NewName $_.Name.Replace($char, $newChar) }
    } else { #end-elseif 
        gci -Path "$path" | Where-Object { $_.Name -match $char } | ForEach-Object { $_ | rename-item  -NewName $_.Name.Replace($char, $newChar) }
    }#end-else
} #end-foreach

错误:如果用户输入类似 ( 或 ) 的内容,则脚本的最后一部分将失败,并显示以下错误代码:

parsing "(" - Not enough )'s.
At <path to script>:109 char:50
+ ... gci -File -Path "$path" | Where-Object { $_.Name -match $char } | For ...
+                                              ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], ArgumentException
    + FullyQualifiedErrorId : System.ArgumentException

我的解决方案:我试图使用[regex]::Escape()转义$char$newChar变量来修复括号错误。我还尝试通过用单引号和双引号将变量括起来来解决这个问题;但是,这可以防止发生实际的字符替换。我知道括号/特殊字符是导致问题的原因,我需要以某种方式转义它们;但是,[regex]::Escape() 解决方案会破坏字符匹配,最终不会替换用户输入的任何字符。

我的问题:如何成功转义 GCI cmdlet 中的$charand$newChar变量并仍然替换特殊字符,如 ( 或 )

gci -Path "$path" | Where-Object { $_.Name -match $char } | ForEach-Object { $_ | rename-item  -NewName $_.Name.Replace($char, $newChar) }
4

2 回答 2

1

您是如何尝试[regex]::Escape()准确实施的?如果您尝试在逃脱之前保存 $char,我会看到您的问题。由于您的.replace()方法不使用正则表达式。这应该会给您留下一些选择

  1. 只需更新where子句以使用转义字符进行比较。这并$char没有永久改变。-match无论您是否打算使用而不支持正则表达式字符,这都是一个好主意。

    gci -Path "$path" | Where-Object { $_.Name -match [regex]::Escape($char) } | ...
    
  2. 另存$char为转义并使用-replace也支持正则表达式的运算符。

    gci -Path "$path" | Where-Object { $_.Name -match $char } | 
        ForEach-Object { $_ | rename-item  -NewName ($_.Name -replace $char, $newChar)) 
    
  3. 根本不使用正则表达式运算符

    gci -Path "$path" | Where-Object { $_.Name.Contains($char) } | ...
    

几点评论

  • 不要使用名称 $input。它是一个保留的变量名。参见about_automatic_variables

  • 由于您正在使用文件的全名,我会提醒您,您也可能不小心编辑文件的扩展名。在这种情况下,该basename属性可能对您有用。

  • 正如TheMadTechnician指出的那样,您可以通过在 Get-ChildItem 级别进行过滤来避免正则表达式/非正则表达式问题。

    gci -Path "$path" -Filter "*$char*" | Rename-Item  -NewName {$_.Name.Replace($char, $newChar)}
    
于 2017-10-20T19:32:13.380 回答
0

解决方案:我已经实施了@Matt 的建议并解决了以下错误:

#Iterate through each character 
foreach($char in $replaceChars) {
    if($type -eq 1) {
        gci -File -Path "$path" | Where-Object { $_.Basename.Contains($char) } | ForEach-Object { $_ | rename-item -NewName ($_.Basename.Replace($char, $newChar) + $_.extension) }
    } elseif ($type -eq 2) { #end-if
        gci -Directory -Path "$path" | Where-Object { $_.Basename.Contains($char) } | ForEach-Object { $_ | rename-item -NewName ($_.Basename.Replace($char, $newChar) + $_.extension) }
    } else { #end-elseif 
        gci -Path "$path" | Where-Object { $_.Basename.Contains($char) } | ForEach-Object { $_ | rename-item -NewName ($_.Basename.Replace($char, $newChar) + $_.extension) }
    }#end-else
} #end-foreach
于 2017-10-23T14:30:04.927 回答