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

[文件操作] [分享]批处理重命名文件和文件夹并添加数字序号

[复制链接]
发表于 2022-6-25 09:37:35 | 显示全部楼层 |阅读模式
【问题描述】
  1. @echo off
  2. cd /d "%~dp0"
  3. set "NewFolder=一年一班"
  4. ren "%~nx1" "%NewFolder%"
  5. setlocal enabledelayedexpansion
  6. set "n=1"
  7. for /f "delims=" %%i in ('dir /b /a-d "%NewFolder%\*.txt"') do (
  8.     ren "%NewFolder%\%%i" "大一 (!n!)%%~xi"
  9.     >"%NewFolder%\大一 (!n!)%%~xi" echo 一年一班
  10.     set /a n+=1
  11. )
  12. REM 处理子文件夹
  13. for /f "delims=" %%i in ('dir /b /ad "%NewFolder%"') do (
  14.     REM 假设只有一个子文件夹
  15.     ren "%NewFolder%\%%i" "%NewFolder%"
  16.     set "n=1"
  17.     for /f "delims=" %%j in ('dir /b /a-d "%NewFolder%\%NewFolder%\*.txt"') do (
  18.         ren "%NewFolder%\%NewFolder%\%%j" "大一 (!n!)%%~xj"
  19.         >"%NewFolder%\%NewFolder%\大一 (!n!)%%~xj" echo 一年一班
  20.         set /a n+=1
  21.     )
  22. )
复制代码
这个目前是只支持一个子文件夹。如果有多个文件夹应该怎么修改?


或者是子文件夹里面还有一个子文件夹,应该怎么修改?
 楼主| 发表于 2022-6-25 09:38:26 | 显示全部楼层
处理多个子文件夹.bat
  1. @echo off
  2. cd /d "%~dp0"
  3. set "NewFolder=一年一班"
  4. setlocal enabledelayedexpansion

  5. REM 处理当前文件夹
  6. ren "%~nx1" "%NewFolder%"
  7. call :RenFile "%NewFolder%"

  8. REM 处理子文件夹
  9. set "FolderN=1"
  10. for /f "delims=" %%i in ('dir /b /ad "%NewFolder%"') do (
  11.     ren "%NewFolder%\%%i" "%NewFolder% (!FolderN!)"
  12.     call :RenFile "%NewFolder%\%NewFolder% (!FolderN!)"
  13.     set /a FolderN+=1
  14. )
  15. goto :eof

  16. :RenFile
  17. pushd "%~1"
  18. set "FileN=1"
  19. for /f "delims=" %%i in ('dir /b /a-d "*.txt"') do (
  20.     ren "%%i" "大一 (!FileN!)%%~xi"
  21.     >"大一 (!FileN!)%%~xi" echo 一年一班
  22.     set /a FileN+=1
  23. )
  24. popd
复制代码
发表于 2022-6-26 01:35:39 | 显示全部楼层
虽然我不怎么看得懂,但能感觉到你是比较专业的
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-3-19 20:35 , Processed in 0.016305 second(s), 8 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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