Board logo

标题: [文件操作] [已解决]批处理怎样在文件夹下批量修改文件名并将文件名写入txt? [打印本页]

作者: w1983912    时间: 2012-6-13 19:06     标题: [已解决]批处理怎样在文件夹下批量修改文件名并将文件名写入txt?

本帖最后由 w1983912 于 2017-11-16 01:04 编辑

文件甲aaa下    只保留含有“  Section 1”的文件(一共似乎是3个空格,请复制)  不包含全删除掉   然后重命名所有文件 删除掉“  Section 1”

按文件名的顺序  保存文件名到aaa.txt  并修改文件名换成对应的次序   aaa.txt格式为    次序  文件名     




罗 资源  Section 1.txt
罗 资源  Section 2.txt
a xx啊  Section 1.pdf
a xx啊  Section 2.pdf
a xx啊  Section 3.pdf
a xx啊  Section 4.pdf
1 5啊555  Section 1.mp4
1 5啊555  Section 2.mp4
1 5啊555  Section 3.mp4

保留文件
罗 资源  Section 1.txt   变为  罗 资源.txt       在文件夹里按文件名字排序后  排在第3位  修改文件名变成3.txt
a xx啊  Section 1.pdf    变为  a xx啊.pdf        在文件夹里按文件名字排序后  排在第2位  修改文件名变成2.pdf
1 5啊555  Section 1.mp4  变为  1 5啊555.mp4      在文件夹里按文件名字排序后  排在第1位  修改文件名变成1.mp4

aaa.txt内容为  不带.和后缀    序号后要新加个点 (空1或2个空格都可以 我这空了一个空格)

1. 1 5啊555
2. a xx啊
3. 罗 资源

以上是从1开始的排序和命名

我希望能自己指定,从3或30开始,比如从30开始,得到以下结果

保留文件
罗 资源  Section 1.txt   变为  罗 资源.txt       在文件夹里按文件名字排序后  排在第3位  修改文件名变成32.txt
a xx啊  Section 1.pdf    变为  a xx啊.pdf        在文件夹里按文件名字排序后  排在第2位  修改文件名变成31.pdf
1 5啊555  Section 1.mp4  变为  1 5啊555.mp4      在文件夹里按文件名字排序后  排在第1位  修改文件名变成30.mp4

得到aaa.txt内容为  不带.和后缀    序号后要新加个点 (空1或2个空格都可以 我这空了一个空格)

30. 1 5啊555
31. a xx啊
32. 罗 资源

GBK bat批处理
望大家帮忙,先谢谢各位了
作者: powerbat    时间: 2012-6-13 20:21

不太明白你的意思。
  1. @echo off
  2. cd aaa
  3. md ~xxx
  4. move "*  Section 1*" ~xxx >nul
  5. del /q *
  6. move ~xxx\* >nul
  7. rd ~xxx
  8. ( for /f "delims=|" %%a in ('dir /a-d/b/on "*  Section 1*"') do (
  9.     set "old=%%a"
  10.     setlocal enableDelayedExpansion
  11.     set "new=!old:  Section 1=!"
  12.     rem ren "!old!" "!new!"
  13.     echo !new!
  14.     endlocal
  15. ) )>~tmp
  16. set "n=1"
  17. set /p n=start:
  18. set /a skip=n-1
  19. if %skip% gtr 0 (set "skip=skip=%skip%") else (set skip=)
  20. ( for /f "%skip% delims=|" %%a in (~tmp) do (
  21.     set "old=%%~na"
  22.     set "ext=%%~xa"
  23.     setlocal enableDelayedExpansion
  24.     rem ren "!old!!ext!" "!n!!ext!"
  25.     echo !n!. !old!
  26.     endlocal
  27.     set /a n+=1
  28. ) )>aaa.txt
  29. del ~tmp
  30. type aaa.txt
  31. pause
复制代码

作者: weichenxiehou    时间: 2012-6-13 20:29

本帖最后由 weichenxiehou 于 2012-6-13 20:37 编辑
  1. @echo off&setlocal enabledelayedexpansion
  2. cls&set/p num=Input start number:
  3. ::delete non-"section 1" files except myself
  4. for /f "delims=" %%a in ('dir/b^|findstr /iv /c:"section 1"^|findstr
  5. /v "^%~nx0$"') do del "%%a"
  6. ::rename left files and generate logfile
  7. (for /f "delims=" %%a in ('dir/b^|findstr /v "^%~nx0$ record.txt"') do
  8. (
  9.   set "filename=%%a"&set "filename=!filename: Section 1=!"
  10.   ren "%%a" "!num!%%~xa"
  11.   for %%i in ("filename!") do echo,!num!.%%~ni
  12.   set/a num+=1
  13. ))>record.txt
复制代码
将bat放到该文件夹下运行即可。
作者: apang    时间: 2012-6-13 22:21

本帖最后由 apang 于 2012-6-14 09:52 编辑
  1. @echo off&setlocal enabledelayedexpansion
  2. set/p st=输入开始数:
  3. for /f "delims=" %%a in ('dir /a-d /on /b^|findstr /vbe "%~nx0"') do (
  4.   set "name=%%~na"
  5.   if "!name:Section 1=!"=="!name!" (del "%%a"
  6.   ) else (
  7.     ren "%%a" "!name:  Section 1=!%%~xa"
  8.     echo !st!. !name:  Section 1=!>>aaa.txt
  9.     set/a st+=1
  10.   )
  11. )
复制代码
放到aaa文件夹下运行




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