Board logo

标题: [文件操作] 批处理怎样重命名多个文件名? [打印本页]

作者: zhg86868    时间: 2021-6-14 21:49     标题: 批处理怎样重命名多个文件名?

文件夹中有多个文件,文件名为第一章,第二章,...第几百几十几章,怎样批量改成第1章,第2章,...第150章
作者: newswan    时间: 2021-6-14 22:20

可以参考一下
http://www.bathome.net/thread-44528-1-1.html
作者: newswan    时间: 2021-6-15 12:55

本帖最后由 newswan 于 2021-6-15 17:36 编辑

论坛没有 powershell 版的,写了个转换中文整数的,主要问题解决
还可以添加 小数 负数 大写
  1. function hz_num ()
  2. {
  3. $a = [ordered] @{"零"=0; "一"=1; "二"=2; "三"=3; "四"=4; "五"=5; "六"=6; "七"=7; "八"=8; "九"=9}
  4. $b = [ordered] @{"十"=10; "百"=100; "千"=1000}
  5. $c = [ordered] @{"万"=10000}
  6. $d = [ordered] @{"亿"=100000000}
  7. $str = $args[0] -replace "(?<=^|[^一二三四五六七八九])十","一十"
  8. $x = $str -split ""
  9. [long[]] $ss = @(0) * 3
  10. $i = 0
  11. while ($i -le $x.count)
  12. {
  13.     $i++
  14.     $s = $x[$i]
  15.     if ($a.keys -contains $s)
  16.     {
  17.         $ss[0] = $ss[0] * 10 + $a[$s]
  18.     } elseif ($b.keys -contains $s)
  19.     {
  20.         $ss[1] += $ss[0] * $b[$s]
  21.         $ss[0] = 0
  22.     } elseif ($c.keys -contains $s)
  23.     {
  24.         $ss[1] = ($ss[1] + $ss[0]) * $c[$s]
  25.         $ss[0] = 0
  26.     } elseif ($d.keys -contains $s)
  27.     {
  28.         $ss[2] = ($ss[2] + $ss[1] + $ss[0]) * $d[$s]
  29.         $ss[1] = 0
  30.         $ss[0] = 0
  31.     } elseif ($s -eq "")
  32.     {
  33.         $ss[2] = $ss[2] + $ss[1] + $ss[0]
  34.         $ss[1] = 0
  35.         $ss[0] = 0
  36.     }
  37. }
  38. return $ss[2]
  39. }
  40. $file = get-childitem *.txt
  41. foreach ($f in $file)
  42. {
  43.     $str= $f.basename -replace "[\s]",""
  44.     if ($str -match "^第" -and $str -match "章$")
  45.     {
  46.         $str= $str -replace "[第章]",""
  47.         if ($str.length -gt 0)
  48.         {
  49.             $a = hz_num $str
  50.             if ($a.length -gt 0)
  51.             {
  52.                 $newname = "第" + $a +"章" + $f.extension
  53.                 write-host $f.fullname `t $newname
  54.                 rename-item $f.fullname -newname $newname
  55.             }
  56.         }
  57.     }
  58. }
复制代码





欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2