【Powershell】批量转换CSV文件编码

 #指定在当前目录需要批量转换的扩展名 
 $files = dir *.csv;
 #指定转换后输出文件名的前缀,留空则为直接覆盖原文件
 $prefix = 'temp_';
 #指定源文件文件编码,可参考https://learn.microsoft.com/zh-cn/dotnet/api/system.text.encodinginfo?view=net-6.0
 $from = 'UTF-8';
 #指定转换后文件的文件编码
 $to = 'gb2312';
 #以下代码无需改动。
 $e = [System.Text.Encoding];
 $sr = [System.IO.StreamReader];
 $sw = [System.IO.StreamWriter];
 $isSame=[string]::IsNullOrEmpty($prefix);
 function getEnc($encName){if('UTF-8' -eq $encName.ToUpper()){return [System.Text.UTF8Encoding]::new($false);
}return [System.Text.Encoding]::GetEncoding($encName);
 };
 function getFileEnc($data) {[object[]]$res = @([System.Text.Encoding]::Default,$false);
if($data[0] -eq 0x2B -and $data[1] -eq 0x2F -and $data[2] -eq 0x76) { return @([System.Text.Encoding]::UTF7,$true);
}elseif($data[0] -eq 0xEF -and $data[1] -eq 0xBB -and $data[2] -eq 0xBF) { return @([System.Text.Encoding]::UTF8,$true);
}elseif($data[0] -eq 0xFF -and $data[1] -eq 0xFE) { return @([System.Text.Encoding]::Unicode,$true);
}elseif($data[0] -eq 0xFE -and $data[1] -eq 0xFF) { return @([System.Text.Encoding]::BigEndianUnicode,$true);
}elseif($data[0] -eq 0 -and $data[1] -eq 0 -and $data[2] -eq 0xFE -and $data[3] -eq 0xFF) { return @([System.Text.Encoding]::UTF32,$true);
}else{ [int]$charByteCounter = 1;
 [byte]$curByte = 0;
 for ($i = 0;
 $i -lt $data.Length;
 $i++) {$curByte = $data[$i];
if ($charByteCounter -eq 1){if ($curByte -ge 0x80){ while (( ($curByte = ($curByte -shl 1)) -band 0x80) -ne 0) {$charByteCounter++;
 } if ($charByteCounter -eq 1 -or $charByteCounter -gt 6) {return $res;
 }}}else{if (($curByte -band 0xC0) -ne 0x80){ return $res;
}$charByteCounter--;
} } if ($charByteCounter -gt 1) {return $res;
 } return @([System.Text.Encoding]::UTF8,$false);
}};
 if($files.Count -lt 1){Write-Host 'No Files...';
exit;
 };
 Write-Host ('Begin:'+(Get-Date -Format 'yyyy-MM-dd HH:mm:ss'));
 foreach($file in $files){ $newFile = (Join-Path -Path $file.DirectoryName -ChildPath ($prefix+$file.BaseName+$file.Extension));
 if($isSame){ $newFile = (Join-Path -Path $file.DirectoryName -ChildPath ('TEMP_'+$file.BaseName+$file.Extension));
};
 $fromEnc = (getEnc -encName $from);
 $toEnc = (getEnc -encName $to);
 $fileEnc = (getFileEnc -data ([System.IO.File]::ReadAllBytes($file.FullName)));
 if($fileEnc[0].BodyName -eq $toEnc.BodyName -and $fileEnc[1] -eq $false){   Write-Host ($file.FullName+',no need to convert.');
  continue;
 } if($fromEnc -ne $fileEnc[0]){ Write-Host ($file.FullName+',encoding should be:' + $fileEnc[0].BodyName);
 $fromEnc = (getEnc -encName $fileEnc[0].BodyName);
 } $srIn = $sr::new($file.FullName,$fromEnc);
 $swOut = $sw::new($newFile,$false,$toEnc,4096);
 while(($line = $srIn.ReadLine()) -ne $null){$swOut.WriteLine($line);
} $srIn.Close();
$swOut.Flush();
$swOut.Close();
 if($isSame){Move-Item -Path $newFile -Destination $file -Force};
};
 Write-Host ('Files:'+$files.Count+',success...');
 Write-Host ('End:'+(Get-Date -Format 'yyyy-MM-dd HH:mm:ss'));

复制以上代码,保存为ps1文件到需要批量转换的目录,双击运行即可。默认覆盖不原文件。

在运行ps1前请以管理员权限运行powershell窗口,并执行以下命令:

开启权限:set-executionpolicy remotesigned

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值