PowerShell-使用文件夹的名称使用葡萄牙字符

发布于 2025-02-10 19:29:25 字数 1186 浏览 1 评论 0原文

我正在编写一个将许多文件夹从源到目标路径移动的脚本。问题是,并非每个文件夹都应该转到同一目录。因此,我拥有带有文件夹名称列表的.txt文件,然后根据.txt文件将其移至适当的目的地。

文件夹的名称具有特殊的字符时

此处的问题是,当 并使用以下代码移动它们:

foreach ($folderName in $file_list)
{   
    try{
        if( $folderName.StartsWith("#")){
            $pastaMae = $folderName.substring(1)
            Write-Host "A criar pasta '$pastaMae'"
            New-Item -Path $Destination -Name $pastaMae -ItemType "directory" -ErrorAction Stop | Out-Null
            $newDestination = $Destination+$pastaMae+"\"
            Write-Host "Pasta Mae: $newDestination"
        }else {
            Write-Host "A mover a pasta: $folderName"
            Move-Item -Path [System.IO.DirectoryInfo]$Path$folderName -Destination $newDestination -ErrorAction Stop
            Write-Host "Pasta movida com sucesso!!" -ForegroundColor Green 
        }
    }catch {
        Write-Host "Ocorreu um erro ao mover a pasta '$folderName'" -ForegroundColor Red
        $erro = 1
        $msgErro += " $folderName ||"
    }

}

这是终端中显示的内容。 (这些文件夹命名为“açãoSocial”和“gudeação”)

I'm writing a script that moves a lot of folders from a source to destination path. The thing is that not every folder should go to the same directory. So I have .txt files with a list of folder names and move them to the appropriate destination based on the .txt file.

The issue here is when the folder's name has special charaters like [á, à, ã, â, Ç, ç, etc...] it crashes and says that the folder that contains those characters does not exist

I'm building the path and moving them with the following code:

foreach ($folderName in $file_list)
{   
    try{
        if( $folderName.StartsWith("#")){
            $pastaMae = $folderName.substring(1)
            Write-Host "A criar pasta '$pastaMae'"
            New-Item -Path $Destination -Name $pastaMae -ItemType "directory" -ErrorAction Stop | Out-Null
            $newDestination = $Destination+$pastaMae+"\"
            Write-Host "Pasta Mae: $newDestination"
        }else {
            Write-Host "A mover a pasta: $folderName"
            Move-Item -Path [System.IO.DirectoryInfo]$Path$folderName -Destination $newDestination -ErrorAction Stop
            Write-Host "Pasta movida com sucesso!!" -ForegroundColor Green 
        }
    }catch {
        Write-Host "Ocorreu um erro ao mover a pasta '$folderName'" -ForegroundColor Red
        $erro = 1
        $msgErro += " $folderName ||"
    }

}

This is what shows in the terminal. (Those folders are named 'Ação Social' and 'Educação')

enter image description here

Is there a way to work arround this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾听心声的旋律 2025-02-17 19:29:25

您的输出表明您要读取的文本文件是从没有BOM的UTF-8文件中, Windows PowerShell 假定为ANSi-nsi-编码,导致编码错误的字符串。
powershell(core)7+现在幸运的是,默认为(bom-bom-bom-bom-bom-never)UTF-8,一致地)。

要避免此问题,请将您的文件明确读取为UTF-8文件;例如:

Get-Content -Encoding utf8 -LiteralPath folder_list.txt

有关两个PowerShell版本中默认字符编码的更多信息,请参见此答案

Your output suggests that the text file you're reading the folder paths from is a UTF-8 file without a BOM, which Windows PowerShell assumes to be ANSI-encoded instead, resulting in mis-decoded strings.
(PowerShell (Core) 7+ now fortunately defaults to (BOM-less) UTF-8, consistently).

To avoid this problem, read your file explicitly as a UTF-8 file; e.g.:

Get-Content -Encoding utf8 -LiteralPath folder_list.txt

For more information about default character encodings in both PowerShell editions, see this answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文