本帖最后由 newswan 于 2021-6-15 17:36 编辑
论坛没有 powershell 版的,写了个转换中文整数的,主要问题解决
还可以添加 小数 负数 大写- function hz_num ()
- {
- $a = [ordered] @{"零"=0; "一"=1; "二"=2; "三"=3; "四"=4; "五"=5; "六"=6; "七"=7; "八"=8; "九"=9}
- $b = [ordered] @{"十"=10; "百"=100; "千"=1000}
- $c = [ordered] @{"万"=10000}
- $d = [ordered] @{"亿"=100000000}
-
- $str = $args[0] -replace "(?<=^|[^一二三四五六七八九])十","一十"
- $x = $str -split ""
-
- [long[]] $ss = @(0) * 3
- $i = 0
- while ($i -le $x.count)
- {
- $i++
- $s = $x[$i]
- if ($a.keys -contains $s)
- {
- $ss[0] = $ss[0] * 10 + $a[$s]
- } elseif ($b.keys -contains $s)
- {
- $ss[1] += $ss[0] * $b[$s]
- $ss[0] = 0
- } elseif ($c.keys -contains $s)
- {
- $ss[1] = ($ss[1] + $ss[0]) * $c[$s]
- $ss[0] = 0
- } elseif ($d.keys -contains $s)
- {
- $ss[2] = ($ss[2] + $ss[1] + $ss[0]) * $d[$s]
- $ss[1] = 0
- $ss[0] = 0
- } elseif ($s -eq "")
- {
- $ss[2] = $ss[2] + $ss[1] + $ss[0]
- $ss[1] = 0
- $ss[0] = 0
- }
- }
-
- return $ss[2]
- }
-
- $file = get-childitem *.txt
- foreach ($f in $file)
- {
- $str= $f.basename -replace "[\s]",""
- if ($str -match "^第" -and $str -match "章$")
- {
- $str= $str -replace "[第章]",""
- if ($str.length -gt 0)
- {
- $a = hz_num $str
- if ($a.length -gt 0)
- {
- $newname = "第" + $a +"章" + $f.extension
- write-host $f.fullname `t $newname
- rename-item $f.fullname -newname $newname
- }
- }
- }
- }
复制代码
|