PowerShell-使用文件夹的名称使用葡萄牙字符
我正在编写一个将许多文件夹从源到目标路径移动的脚本。问题是,并非每个文件夹都应该转到同一目录。因此,我拥有带有文件夹名称列表的.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')
Is there a way to work arround this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的输出表明您要读取的文本文件是从没有BOM的UTF-8文件中, Windows PowerShell 假定为ANSi-nsi-编码,导致编码错误的字符串。
( powershell(core)7+现在幸运的是,默认为(bom-bom-bom-bom-bom-never)UTF-8,一致地)。
要避免此问题,请将您的文件明确读取为UTF-8文件;例如:
有关两个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.:
For more information about default character encodings in both PowerShell editions, see this answer.