powershell
遍历当前目录所有txt文件(包括子目录)
以字体大小9的微软雅黑,计算字符宽度,宽度大于607时添加换行符(CRLF)
完事后,会在原来的文件名前添加new_new_前缀,生成新的txt文件
请不要吐槽效率- #@&cls&pause&powershell "type -literalpath '%~f0'|out-string|iex"&pause&exit/b
-
- $linewidth=607
- $fontname='Microsoft YaHei'
- $fontsize=9
- $wd=@{}
-
- Add-Type -AssemblyName system.drawing
- $font=[System.Drawing.Font]::new($fontname,[single]$fontsize)
- $canvas=[System.Drawing.Graphics]::FromImage([System.Drawing.Bitmap]::new(64,64))
-
- $files=[object[]](gci -r -filter "*.txt")
-
- foreach($file in $files){
- $lines=[object[]](gc -Encoding utf8 -LiteralPath ($file.fullname))
-
- (0..($lines.length-1)).foreach({
- $line=$lines[$_].ToCharArray()
-
- if($line){
- $s=''
- $w=0
- $line|%{
- if($wd.Contains($_)){
- $w+=$wd[$_];$s+=$_
- }else{
- $z=$canvas.MeasureString($_,$font).width
- $wd.add($_,$z)
- $w+=$z;$s+=$_
- }
- if($w -gt $linewidth){$s+="`r`n";$w=0}
- }
- $lines[$_]=$s
- }
- })
-
- sc -Value $lines -Encoding utf8 -LiteralPath ($file.DirectoryName+"\new_new_"+$file.Name)
- }
复制代码
|