找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 26206|回复: 13

[文件操作] 批处理 如何按windows资源管理器里文件的顺序读取文件夹内的文件名

[复制链接]
发表于 2023-3-23 13:08:40 | 显示全部楼层 |阅读模式
如题,想给某个文件夹内的文件重新命名,这些文件本身是有顺序的,在资源管理器内都调整好的,但遇到a(1)、a(2)、a(3)......a(11)、a(12)......a(20)、a(21)......这样的文件名时,用dir 读取的顺序却是:
a(1)、a(10)、a(11)......a(2)、a(20)、a(21)......
求问:如何能按照资源管理器内的顺序即a(1)、a(2)、a(3)......a(11)、a(12)......a(20)、a(21)......来读取文件呢。我用的是For+dir 命令。
盼大神解答
发表于 2023-3-23 13:21:08 | 显示全部楼层
回复 1# arthrrwong


请发一个资源管理器里面的完整截图,看看是如何排序的。

如果需要上传截图,可以找个图床,例如:
http://bbs.bathome.net/thread-60985-1-1.html
发表于 2023-3-23 14:37:57 | 显示全部楼层
文件名的数字部分在windows资源管理器里是按数值大小排序的,而dir是ASCII码排序的。
发表于 2023-3-23 14:46:40 | 显示全部楼层
发表于 2023-3-23 15:04:23 | 显示全部楼层
 楼主| 发表于 2023-3-23 17:10:41 | 显示全部楼层
回复 4# newswan


    谢谢,我是小白,不太懂。我就是需要在BAT文件中运行一段代码,将BAT文件所在目录的文件重新按资源管理器中的排序重新编号,什么Linux sort ,我不会用啊
需求:
需要将a(1)、a(2)、a(3).......a(10)、a(11)........a(20)、a(21)......a(30)、a(31).......等连续的文件按顺序编号成0001、0002、0003.......
也就是说,把a(1)改名为0001,a(2)改成0002,以此类推,但现在用dir 后,却把a(10)改成了0002
发表于 2023-3-23 22:29:19 | 显示全部楼层
5楼的帖子没看明白吗,我帮你写一下
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. for /l %%l in (1,1,10000) do (
  4.         set num=000%%l
  5.         if exist "a(%%l).jpg" (
  6.                 ren "a(%%l).jpg" "!num:~-4!.jpg"
  7.         )
  8. )
  9. endlocal
  10. pause
复制代码


使用前先测试
发表于 2023-3-24 08:41:28 | 显示全部楼层
回复 6# arthrrwong
好像排序并不影响重命名,将批处理文件另存为ANSI编码:
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. for /f "tokens=1,2* delims=()" %%i in ('dir /b /a-d "*(*)*"') do (
  4.     set n=000%%j
  5.     ren "%%i(%%j)%%k" "!n:~-4!%%k"
  6. )
  7. pause
复制代码
发表于 2023-3-24 08:54:46 | 显示全部楼层
牵涉到文件相关的,最好把扩展名一并写出来,便于处理。
 楼主| 发表于 2023-10-9 15:37:15 | 显示全部楼层
谢谢各位大神
发表于 2023-10-9 19:32:22 | 显示全部楼层
可以试试用powershell
版本5.1以上

  1. Add-Type -TypeDefinition @"
  2. using System.Runtime.InteropServices;
  3. namespace the{
  4. public static class sort{
  5.     [DllImport("Shlwapi.dll", CharSet = CharSet.Unicode)]
  6.     public static extern int StrCmpLogicalW(string psz1, string psz2);
  7. }}
  8. "@

  9. class exsort:System.Collections.IComparer{
  10. [int]Compare($a,$b){return [the.sort]::StrCmpLogicalW($a,$b)}
  11. }

  12. $a=gci -file -n
  13. [array]::sort($a,[exsort]::new())
  14. (0..($a.Length-1)).ForEach({rni -LiteralPath ($a[$_]) ([string]::format("{0:d4}",$_+1))})
复制代码
发表于 2023-10-9 21:18:46 | 显示全部楼层
  1. @echo off
  2. Set currentDir=powershell -noprofile -executionpolicy bypass "(dir -Exclude %~nx0|?{[int]$_.Attributes -ne 16}|sort{$name=$_.Name -replace '.*\((\d+)\)','$1';if($name -match '^\d+$'){[int]$name}}).Name"
  3. for /f "delims=" %%1 in ('%currentDir%')do echo %%1
  4. pause
复制代码
发表于 2023-10-9 21:33:11 | 显示全部楼层
可以试试用powershell
版本5.1以上
Five66 发表于 2023-10-9 19:32



    你代码还需要调整一下,在我电脑5.1测试报错
发表于 2023-10-10 06:13:30 | 显示全部楼层
回复 13# Nsqs


啊,因为那代码是直接在powershell窗口里输入执行的
脚本文件执行的话试试下面的(加上了打印,方便测试,不过修改名字时还是不包含拓展名)
还有较高版本的系统不知道行不行

  1. #@&cls&powershell -sta "$self='%~nx0';type -literalpath '%~f0'|out-string|iex"&pause&exit/b

  2. Add-Type -TypeDefinition @"
  3. using System.Runtime.InteropServices;
  4. namespace the{
  5. public static class sort{
  6.     [DllImport("Shlwapi.dll", CharSet = CharSet.Unicode)]
  7.     public static extern int StrCmpLogicalW(string psz1, string psz2);
  8. }}
  9. "@

  10. class exsort:System.Collections.IComparer{
  11. $st=0
  12. exsort($t){$this.st=$t}
  13. [int]Compare($a,$b){return $this.st::StrCmpLogicalW($a,$b)}
  14. }

  15. $a=gci -file -n -exclude ($MyInvocation.MyCommand.Name,$self)
  16. [array]::sort($a,[exsort]::new([the.sort]))
  17. (0..($a.Length-1)).ForEach({
  18. [console]::writeline("$($a[$_])        rename to        "+[string]::format("{0:d4}",$_+1))
  19. #rni -LiteralPath ($a[$_]) ([string]::format("{0:d4}",$_+1))
  20. })
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-18 13:19 , Processed in 0.021470 second(s), 8 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表