错误重命名基于文件中第一行的文本文件的PowerShell代码

huangapple go评论59阅读模式
英文:

Error renaming text files based on the first line in the file in Powershell

问题

以下是翻译好的部分:

"我正在尝试根据第一行来重命名大量的纯文本文件。我遇到的问题是有些第一行包含无效字符(路径中的非法字符),因此会导致错误。这是我正在使用的代码:

$files = Get-ChildItem *.txt

$file_map = @()
foreach ($file in $files) {
    $file_map += @{
        OldName = $file.Fullname
        NewName = "{0}.txt" -f $(Get-Content $file.Fullname | select -First 1)
    }
}

$file_map | % { Rename-Item -Path $_.OldName -NewName $_.NewName }

是否有一种方法可以在重命名时忽略特殊字符?

谢谢。"

英文:

I'm trying to rename a large number of plain text files based on the first line. The issue I'm encoutering is that some first lines have invalid characters (illegal characters in path) so I get errors. This is what I'm using:

$files = Get-ChildItem *.txt

$file_map = @()
foreach ($file in $files) {
    $file_map += @{
        OldName = $file.Fullname
        NewName = "{0}.txt" -f $(Get-Content $file.Fullname| select -First 1)
    }
}

$file_map | % { Rename-Item -Path $_.OldName -NewName $_.NewName }

Is there a way to ignore special characthers when renaming?

Thanks.

答案1

得分: 1

以下可能适用于您。基本上,您可以使用Path.GetInvalidFileNameChars 方法 来获取文件名中的无效字符的字符数组,然后创建一个正则表达式模式来移除这些无效字符:

$invalidChars = ([IO.Path]::GetInvalidFileNameChars() |
    ForEach-Object { [regex]::Escape($_) }) -join ''|'';

Get-ChildItem *.txt | Rename-Item -NewName {
    $firstLine = ($_ | Get-Content -TotalCount 1) -replace $invalidChars
    ''{0}.txt'' -f $firstLine
}

也许一个更简单的方法是移除任何非单词字符 \W:

Get-ChildItem *.txt | Rename-Item -NewName {
    $firstLine = ($_ | Get-Content -TotalCount 1) -replace ''\W''
    ''{0}.txt'' -f $firstLine
}

或者移除不在字符范围 a-zA-Z0-9 或空格 内的任何字符:

Get-ChildItem *.txt | Rename-Item -NewName {
    $firstLine = ($_ | Get-Content -TotalCount 1) -replace ''[^a-z0-9 ]''
    ''{0}.txt'' -f $firstLine
}
英文:

The following might work for you. Essentially, you can use the Path.GetInvalidFileNameChars Method to get a char array of those invalid characters for a file name then create a regex pattern to remove those invalid chars:

$invalidChars = ([IO.Path]::GetInvalidFileNameChars() |
    ForEach-Object { [regex]::Escape($_) }) -join '|'

Get-ChildItem *.txt | Rename-Item -NewName {
    $firstLine = ($_ | Get-Content -TotalCount 1) -replace $invalidChars
    '{0}.txt' -f $firstLine
}

Perhaps an easier approach would be to remove any Non-word character with \W:

Get-ChildItem *.txt | Rename-Item -NewName {
    $firstLine = ($_ | Get-Content -TotalCount 1) -replace '\W'
    '{0}.txt' -f $firstLine
}

Or to remove any character that is not in the character ranges a-z, A-Z, 0-9 or a space :

Get-ChildItem *.txt | Rename-Item -NewName {
    $firstLine = ($_ | Get-Content -TotalCount 1) -replace '[^a-z0-9 ]'
    '{0}.txt' -f $firstLine
}

huangapple
  • 本文由 发表于 2023年3月31日 02:47:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75891928.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定