结合本系列文章第一篇,看本文。
利用批处理对文件名进行操作。
操作文件前应当对原文件有备份,因为Windows系统对文件名排序这块是02在1之后。
@echo off
@REM @Author: xianghongai@gmail.com
@REM @Date: 2012-02-06
mode con cols=100 lines=38
color 02
title 文件名操作 by:小可(xianghongai@gmail.com)
SETLOCAL ENABLEDELAYEDEXPANSION
:GTBegin
cls
@echo 文件名操作 [前后缀、序数、替换、删除]
@echo --------------------------------------------
@echo 所有操作按回车键 (Enter) 继续
@echo --------------------------------------------
set /p HandleDir=拖一个需处理的文件夹进来:
:GTCONTINUE
cls
@echo 文件名操作
@echo --------------------------------------------
@echo 处理的目录:%HandleDir%
@echo.
@echo -----------------1.加前缀-----------------
@echo 11 - 当前目录:目录名-文件名
@echo 12 - 层级子目录:子目录名-文件名
@echo 13 - 当前目录:前缀字符-文件名
@echo 14 - 层级子目录:前缀字符-文件名
@echo -----------------2.加后缀-----------------
@echo 21 - 当前目录:文件名-目录名
@echo 22 - 层级子目录:文件名-子目录名
@echo 23 - 当前目录:文件名-后缀字符
@echo 24 - 层级子目录:文件名-后缀字符
@echo -----------------3.加序数-----------------
@echo 31 - 当前目录:序数
@echo 32 - 层级子目录:序数
@echo 33 - 当前目录:文件名-序数
@echo 34 - 层级子目录:文件名-序数
@echo 35 - 当前目录:目录名-序数
@echo 36 - 层级子目录:子目录名-序数
@echo 37 - 当前目录:字符-序数
@echo -------------31.加序数(零占位)-------------
@echo 311 - 当前目录:目录名-两位序数
@echo 312 - 层级子目录:子目录名-两位序数
@echo 313 - 当前目录:字符-两位序数
@echo 314 - 当前目录:目录名-三位序数
@echo 315 - 层级子目录:子目录名-三位序数
@echo 316 - 当前目录:字符-三位序数
@echo ----------------4.替换删除----------------
@echo 41 - 替换当前目录下文件名
@echo 42 - 替换所有子层级中的文件名
@echo 43 - 删除当前目录下文件名
@echo 44 - 删除所有子层级中的文件名
@echo -------------------其它-------------------
@echo 8 - 重新选择目录
@echo 9 - 退出
@echo.
set GTOperator=9
set /p GTOperator="选择操作数后: "
@echo.
if %GTOperator% equ 11 goto GTDIRPREFIX
if %GTOperator% equ 12 goto GTSUBDIRPREFIX
if %GTOperator% equ 13 goto GTDIRPREFIXCUSTOM
if %GTOperator% equ 14 goto GTSUBDIRPREFIXCUSTOM
if %GTOperator% equ 21 goto GTDIRSUFFIX
if %GTOperator% equ 22 goto GTSUBDIRSUFFIX
if %GTOperator% equ 23 goto GTDIRSUFFIXCUSTOM
if %GTOperator% equ 24 goto GTSUBDIRSUFFIXCUSTOM
if %GTOperator% equ 31 goto GTDIRORDINAL
if %GTOperator% equ 32 goto GTSUBDIRORDINAL
if %GTOperator% equ 33 goto GTDIRFLIENAMEORDINAL
if %GTOperator% equ 34 goto GTSUBDIRFILENAMEORDINAL
if %GTOperator% equ 35 goto GTDIRNAMEORDINAL
if %GTOperator% equ 36 goto GTSUBDIRNAMEORDINAL
if %GTOperator% equ 37 goto GTDIRCUSTOMNAMEORDINAL
if %GTOperator% equ 311 goto GTDIRNAMEORDINALOO
if %GTOperator% equ 312 goto GTSUBDIRNAMEORDINALOO
if %GTOperator% equ 313 goto GTDIRCUSTOMNAMEORDINALOO
if %GTOperator% equ 314 goto GTDIRNAMEORDINALOOO
if %GTOperator% equ 315 goto GTSUBDIRNAMEORDINALOOO
if %GTOperator% equ 316 goto GTDIRCUSTOMNAMEORDINALOOO
if %GTOperator% equ 41 goto GTDIRREPLACE
if %GTOperator% equ 42 goto GTSUBDIRREPLACE
if %GTOperator% equ 43 goto GTDIRDEL
if %GTOperator% equ 44 goto GTSUBDIRDEL
if %GTOperator% equ 8 goto GTBegin
if %GTOperator% equ 9 goto GTExit
echo 请选择正确的操作数
pause >nul
goto GTCONTINUE
rem @echo 不能做文件名称的字符
rem @echo \\ / : * ? " < > |
rem 11.当前目录:目录名-文件名
:GTDIRPREFIX
cd /d "!HandleDir!"
for /f "delims=" %%i in ("%cd%") do set "DirNamePrefix=%%~nxi"
for /f "delims=" %%i in (\'dir /a-d /b *.* 2^>nul\') do (
if not "%~nx0"=="%%i" (
ren "%%i" "%DirNamePrefix%-%%~nxi"
)
)
echo.
echo :) 搞定(回车继续其它操作)
pause >nul
goto GTCONTINUE
rem 12.子目录:子目录名-文件名
:GTSUBDIRPREFIX
cd /d "!HandleDir!"
rem for /r %%a in (*.*) do (for %%b in ("%%a\\..") do (ren "%%a" "%%~nxb-%%~nxa"))
for /f "usebackq delims=" %%i in (`dir /ad /b /s^|sort /r`) do (
for /f "usebackq delims=" %%j in (`dir /a-d /b "%%i\\*.*" 2^>nul`) do (
ren "%%i\\%%j" "%%~nxi-%%~nxj" ) )
echo.
echo :) 搞定(回车继续其它操作)
pause >nul
goto GTCONTINUE
rem 13.当前目录:前缀字符-文件名
:GTDIRPREFIXCUSTOM
set FilePrefix=""
set /p FilePrefix="前缀字符:"
cd /d "!HandleDir!"
for /f "usebackq delims=" %%i in (`dir /a-d/b`) do (
if not "%%i"==%0 ren "%%i" "%FilePrefix%%%~nxi")
echo.
echo :) 搞定(回车继续其它操作)
pause >nul
goto GTCONTINUE
rem 14.子目录:前缀字符-文件名
:GTSUBDIRPREFIXCUSTOM
set SubFilePrefix=""
set /p SubFilePrefix="前缀字符:"
cd /d "!HandleDir!"
rem for /f "usebackq delims=" %%i in (`dir /a-d/b/s`) do (if not "%%i"==%0 ren "%%i" "%SubFilePrefix%%%~nxi")
for /f "usebackq delims=" %%i in (`dir /ad /b /s^|sort /r`) do (
for /f "usebackq delims=" %%j in (`dir /a-d /b "%%i\\*.*" 2^>nul`) do (
ren "%%i\\%%j" "!SubFilePrefix!%%~nxj"))
echo.
echo :) 搞定(回车继续其它操作)
pause >nul
goto GTCONTINUE
rem 21.当前目录:文件名-目录名
:GTDIRSUFFIX
cd /d "!HandleDir!"
for /f "delims=" %%i in ("%cd%") do set "DirNameSuffix=%%~nxi"
for /f "delims=" %%i in (\'dir /a-d /b *.* 2^>nul\') do (
if not "%~nx0"=="%%i" (
ren "%%i" "%%~ni-%DirNameSuffix%%%~xi"
)
)
echo.
echo :) 搞定(回车继续其它操作)
pause >nul
goto GTCONTINUE
rem 22.子目录:文件名-子目录名
:GTSUBDIRSUFFIX
cd /d "!HandleDir!"
rem for /r %%a in (*.*) do (for %%b in ("%%a\\..") do (ren "%%a" "%%~na-%%~nxb%%~xa"))
for /f "usebackq delims=" %%i in (`dir /ad /b /s^|sort /r`) do (
for /f "usebackq delims=" %%j in (`dir /a-d /b "%%i\\*.*" 2^>nul`) do (
ren "%%i\\%%j" "%%~nj-%%~nxi%%~xj" ) )
echo.
echo :) 搞定(回车继续其它操作)
pause >nul
goto GTCONTINUE
rem 23.当前目录:文件名-后缀字符
:GTDIRSUFFIXCUSTOM
set FileSuffix=""
set /p FileSuffix="后缀字符:"
cd /d "!HandleDir!"
for /f "usebackq delims=" %%i in (`dir /a-d/b`) do (
if not "%%i"==%0 ren "%%i" "%%~ni!FileSuffix!%%~xi")
echo.
echo :) 搞定(回车继续其它操作)
pause >nul
goto GTCONTINUE
rem 24.子目录:文件名-后缀字符
:GTSUBDIRSUFFIXCUSTOM
set SubFileSuffix=""
set /p SubFileSuffix="后缀字符:"
cd /d "!HandleDir!"
rem for /f "usebackq delims=" %%i in (`dir /a-d/b/s`) do (if not "%%i"==%0 ren "%%i" "%%~ni%SubFileSuffix%%%~xi")
for /f "usebackq delims=" %%i in (`dir /ad /b /s^|sort /r`) do (
for /f "usebackq delims=" %%j in (`dir /a-d /b "%%i\\*.*" 2^>nul`) do (
ren "%%i\\%%j" "%%~nj!SubFileSuffix!%%~xj"))
echo.
echo :) 搞定(回车继续其它操作)
pause >nul
goto GTCONTINUE
rem 31.当前目录:序数
:GTDIRORDINAL
set NOrderBy=1
@echo.
@echo 文件排序方式
@echo --------------------------------------------
@echo 1.文件大小
@echo 2.文件小大
@echo 3.创建时间早晚
@echo 4.创建时间晚早
@echo 5.文件名A-Z
@echo 6.文件名Z-A
@echo 7.扩展名A-Z
@echo 8.扩展名Z-A
@echo --------------------------------------------
:GTDIRORDINALORDERBY
set /p NOrderBy="文件排序方式:"
set OrderBy=d
if %NOrderBy% equ 1 (set OrderBy=-s)
if %NOrderBy% equ 2 (set OrderBy=s)
if %NOrderBy% equ 3 (set OrderBy=d)
if %NOrderBy% equ 4 (set OrderBy=-d)
if %NOrderBy% equ 5 (set OrderBy=n)
if %NOrderBy% equ 6 (set OrderBy=-n)
if %NOrderBy% equ 7 (set OrderBy=-e)
if %NOrderBy% equ 8 (set OrderBy=e)
echo %NOrderBy%|findstr /be "[1-8]" >nul || (echo 选择支持的排序方式 & goto GTDIRORDINALORDERBY)
cd /d "!HandleDir!"
set /a num=0
for /f "usebackq tokens=*" %%i in (`dir /a-d /b /o!OrderBy! /tc`) do (
if not "%%i"=="%~n0%~x0" (
set /a num+=1
ren "%%i" "!num!%%~xi" ))
echo.
echo :) 搞定(回车继续其它操作)
pause >nul
goto GTCONTINUE
rem 32.子目录:序数
:GTSUBDIRORDINAL
set NOrderBy=1
@echo.
@echo 文件排序方式
@echo --------------------------------------------
@echo 1.文件大小
@echo 2.文件小大
@echo 3.创建时间早晚
@echo 4.创建时间晚早
@echo 5.文件名A-Z
@echo 6.文件名Z-A
@echo 7.扩展名A-Z
@echo 8.扩展名Z-A
@echo --------------------------------------------
:GTSUBDIRORDINALORDERBY
set /p NOrderBy="文件排序方式:"
set OrderBy=d
if %NOrderBy% equ 1 (set OrderBy=-s)
if %NOrderBy% equ 2 (set OrderBy=s)
if %NOrderBy% equ 3 (set OrderBy=d)
if %NOrderBy% equ 4 (set OrderBy=-d)
if %NOrderBy% equ 5 (set OrderBy=n)
if %NOrderBy% equ 6 (set OrderBy=-n)
if %NOrderBy% equ 7 (set OrderBy=-e)
if %NOrderBy% equ 8 (set OrderBy=e)
echo %NOrderBy%|findstr /be "[1-8]" >nul || (echo 选择支持的排序方式 & goto GTSUBDIRORDINALORDERBY)
cd /d "!HandleDir!"
for /f "usebackq delims=" %%i in (`dir /ad /b /s^|sort /r`) do (
set /a num=0
for /f "usebackq delims=" %%j in (`dir /a-d /b /o!OrderBy! "%%i\\*.*" 2^>nul`) do (
set /a num+=1
ren "%%i\\%%j" "!num!%%~xj" ))
echo.
echo :) 搞定(回车继续其它操作)
pause >nul
goto GTCONTINUE
rem 33.当前目录:文件名-序数
:GTDIRFLIENAMEORDINAL
set NOrderBy=1
@echo.
@echo 文件排序方式
@echo --------------------------------------------
@echo 1.文件大小
@echo 2.文件小大
@echo 3.创建时间早晚
@echo 4.创建时间晚早
@echo 5.文件名A-Z
@echo 6.文件名Z-A
@echo 7.扩展名A-Z
@echo 8.扩展名Z-A
@echo --------------------------------------------
:GTDIRFLIENAMEORDINALORDERBY
set /p NOrderBy="文件排序方式:"
set OrderBy=d
if %NOrderBy% equ 1 (set OrderBy=-s)
if %NOrderBy% equ 2 (set OrderBy=s)
if %NOrderBy% equ 3 (set OrderBy=d)
if %NOrderBy% equ 4 (set OrderBy=-d)
if %NOrderBy% equ 5 (set OrderBy=n)
if %NOrderBy% equ 6 (set OrderBy=-n)
if %NOrderBy% equ 7 (set OrderBy=-e)
if %NOrderBy% equ 8 (set OrderBy=e)
echo %NOrderBy%|findstr /be "[1-8]" >nul || (echo 选择支持的排序方式 & goto GTDIRFLIENAMEORDINALORDERBY)
cd /d "!HandleDir!"
set /a num=0
for /f "usebackq delims=" %%i in (`dir /a-d /b /o!OrderBy! *.* 2^>nul`) do (
if not "%~nx0"=="%%i" (
set /a num+=1
ren "%%i" "%%~ni-!num!%%~xi"))
echo.
echo :) 搞定(回车继续其它操作)
pause >nul
goto GTCONTINUE
rem 34.子目录:文件名-序数
:GTSUBDIRFILENAMEORDINAL
set NOrderBy=1
@echo.
@echo 文件排序方式
@echo --------------------------------------------
@echo 1.文件大小
@echo 2.文件小大
@echo 3.创建时间早晚
@echo 4.创建时间晚早
@echo 5.文件名A-Z
@echo 6.文件名Z-A
@echo 7.扩展名A-Z
@echo 8.扩展名Z-A
@echo --------------------------------------------
:GTSUBDIRFILENAMEORDINALORDERBY
set /p NOrderBy="文件排序方式:"
set OrderBy=d
if %NOrderBy% equ 1 (set OrderBy=-s)
if %NOrderBy% equ 2 (set OrderBy=s)
if %NOrderBy% equ 3 (set OrderBy=d)
if %NOrderBy% equ 4 (set OrderBy=-d)
if %NOrderBy% equ 5 (set OrderBy=n)
if %NOrderBy% equ 6 (set OrderBy=-n)
if %NOrderBy% equ 7 (set OrderBy=-e)
if %NOrderBy% equ 8 (set OrderBy=e)
echo %NOrderBy%|findstr /be "[1-8]" >nul || (echo 选择支持的排序方式 & goto GTSUBDIRFILENAMEORDINALORDERBY)
cd /d "!HandleDir!"
for /f "usebackq delims=" %%i in (`dir /ad /b /s^|sort /r`) do (
set /a num=0
for /f "usebackq delims=" %%j in (`dir /a-d /b /o!OrderBy! "%%i\\*.*" 2^>nul`) do (
set /a num+=1
ren "%%i\\%%j" "%%~nj-!num!%%~xj" ))
echo.
echo :) 搞定(回车继续其它操作)
pause >nul
goto GTCONTINUE
rem 35.当前目录:目录名-序数
:GTDIRNAMEORDINAL
set NOrderBy=1
@echo.
@echo 文件排序方式
@echo --------------------------------------------
@echo 1.文件大小
@echo 2.文件小大
@echo 3.创建时间早晚
@echo 4.创建时间晚早
@echo 5.文件名A-Z
@echo 6.文件名Z-A
@echo 7.扩展名A-Z
@echo 8.扩展名Z-A
@echo --------------------------------------------
:GTDIRNAMEORDINALORDERBY
set /p NOrderBy="文件排序方式:"
set OrderBy=d
if %NOrderBy% equ 1 (set OrderBy=-s)
if %NOrderBy% equ 2 (set OrderBy=s)
if %NOrderBy% equ 3 (set OrderBy=d)
if %NOrderBy% equ 4 (set OrderBy=-d)
if %NOrderBy% equ 5 (set OrderBy=n)
if %NOrderBy% equ 6 (set OrderBy=-n)
if %NOrderBy% equ 7 (set OrderBy=-e)
if %NOrderBy% equ 8 (set OrderBy=e)
echo %NOrderBy%|findstr /be "[1-8]" >nul || (echo 选择支持的排序方式 & goto GTDIRNAMEORDINALORDERBY)
cd /d "!HandleDir!"
set /a num=0
for /f "usebackq delims=" %%i in (\'%cd%\') do set "DirNamePrefix=%%~nxi"
for /f "usebackq delims=" %%i in (`dir /a-d /b /o!OrderBy! *.* 2^>nul`) do (
if not "%~nx0"=="%%i" (
set /a num+=1
ren "%%i" "%DirNamePrefix%-!num!%%~xi"))
echo.
echo :) 搞定(回车继续其它操作)
pause >nul
goto GTCONTINUE
rem 36.子目录:子目录名-序数
:GTSUBDIRNAMEORDINAL
cd /d "!HandleDir!"
for /f "usebackq delims=" %%i in (`dir /ad /b /s^|sort /r`) do (
set /a num=0
for /f "usebackq delims=" %%j in (`dir /a-d /b /o!OrderBy! "%%i\\*.*" 2^>nul`) do (
set /a num+=1
ren "%%i\\%%j" "%%~nxi-!num!%%~xj" ))
echo.
echo :) 搞定(回车继续其它操作)
pause >nul
goto GTCONTINUE
rem 37.自定义字符和开始序数
:GTDIRCUSTOMNAMEORDINAL
set SubFilePrefix=""
set StartNum=0
set /p SubFilePrefix="前缀字符:"
set /p StartNum="开始序数:"
set NOrderBy=1
@echo.
@echo 文件排序方式
@echo --------------------------------------------
@echo 1.文件大小
@echo 2.文件小大
@echo 3.创建时间早晚
@echo 4.创建时间晚早
@echo 5.文件名A-Z
@echo 6.文件名Z-A
@echo 7.扩展名A-Z
@echo 8.扩展名Z-A
@echo --------------------------------------------
:GTORDERBY
set /p NOrderBy="文件排序方式:"
set OrderBy=d
if %NOrderBy% equ 1 (set OrderBy=-s)
if %NOrderBy% equ 2 (set OrderBy=s)
if %NOrderBy% equ 3 (set OrderBy=d)
if %NOrderBy% equ 4 (set OrderBy=-d)
if %NOrderBy% equ 5 (set OrderBy=n)
if %NOrderBy% equ 6 (set OrderBy=-n)
if %NOrderBy% equ 7 (set OrderBy=-e)
if %NOrderBy% equ 8 (set OrderBy=e)
echo %NOrderBy%|findstr /be "[1-8]" >nul || (echo 选择支持的排序方式 & goto GTORDERBY)
cd /d "!HandleDir!"
for /f "usebackq tokens=*" %%i in (`dir /a-d /b /o!OrderBy!`) do (
if not "%%i"=="%~n0%~x0" (
set /a StartNum+=1
ren "%%i" "!SubFilePrefix!!StartNum!%%~xi" ))
echo.
echo :) 搞定(回车继续其它操作)
pause >nul
goto GTCONTINUE
rem 311.当前目录:目录名-两位序数
:GTDIRNAMEORDINALOO
set NOrderBy=1
@echo.
@echo 文件排序方式
@echo --------------------------------------------
@echo 1.文件大小
@echo 2.文件小大
@echo 3.创建时间早晚
@echo 4.创建时间晚早
@echo 5.文件名A-Z
@echo 6.文件名Z-A
@echo 7.扩展名A-Z
@echo 8.扩展名Z-A
@echo --------------------------------------------
:GTDIRNAMEORDINALOOORDERBY
set /p NOrderBy="文件排序方式:"
set OrderBy=d
if %NOrderBy% equ 1 (set OrderBy=-s)
if %NOrderBy% equ 2 (set OrderBy=s)
if %NOrderBy% equ 3 (set OrderBy=d)
if %NOrderBy% equ 4 (set OrderBy=-d)
if %NOrderBy% equ 5 (set OrderBy=n)
if %NOrderBy% equ 6 (set OrderBy=-n)
if %NOrderBy% equ 7 (set OrderBy=-e)
if %NOrderBy% equ 8 (set OrderBy=e)
echo %NOrderBy%|findstr /be "[1-8]" >nul || (echo 选择支持的排序方式 & goto GTDIRNAMEORDINALOOORDERBY)
cd /d "!HandleDir!"
set /a num=0
for /f "usebackq delims=" %%i in (\'%cd%\') do set "DirNamePrefix=%%~nxi"
for /f "usebackq delims=" %%i in (`dir /a-d /b /o!OrderBy! *.* 2^>nul`) do (
if not "%~nx0"=="%%i" (
set /a num+=1
if !num! LSS 10 ( ren "%%i" "%DirNamePrefix%-0!num!%%~xi" ) else ( ren "%%i" "%DirNamePrefix%-!num!%%~xi" )
))
echo.
echo :) 搞定(回车继续其它操作)
pause >nul
goto GTCONTINUE
rem 312.子目录:子目录名-两位序数
:GTSUBDIRNAMEORDINALOO
set NOrderBy=1
@echo.
@echo 文件排序方式
@echo --------------------------------------------
@echo 1.文件大小
@echo 2.文件小大
@echo 3.创建时间早晚
@echo 4.创建时间晚早
@echo 5.文件名A-Z
@echo 6.文件名Z-A
@echo 7.扩展名A-Z
@echo 8.扩展名Z-A
@echo --------------------------------------------
:GTSUBDIRNAMEORDINALOOORDERBY
set /p NOrderBy="文件排序方式:"
set OrderBy=d
if %NOrderBy% equ 1 (set OrderBy=-s)
if %NOrderBy% equ 2 (set OrderBy=s)
if %NOrderBy% equ 3 (set OrderBy=d)
if %NOrderBy% equ 4 (set OrderBy=-d)
if %NOrderBy% equ 5 (set OrderBy=n)
if %NOrderBy% equ 6 (set OrderBy=-n)
if %NOrderBy% equ 7 (set OrderBy=-e)
if %NOrderBy% equ 8 (set OrderBy=e)
echo %NOrderBy%|findstr /be "[1-8]" >nul || (echo 选择支持的排序方式 & goto GTSUBDIRNAMEORDINALOOORDERBY)
cd /d "!HandleDir!"
for /f "usebackq delims=" %%i in (`dir /ad /b /s^|sort /r`) do (
set /a num=0
for /f "usebackq delims=" %%j in (`dir /a-d /b /o!OrderBy! "%%i\\*.*" 2^>nul`) do (
set /a num+=1
if !num! LSS 10 ( ren "%%i\\%%j" "%%~ni-0!num!%%~xj" ) else ( ren "%%i\\%%j" "%%~ni-!num!%%~xj" )
))
echo.
echo :) 搞定(回车继续其它操作)
pause >nul
goto GTCONTINUE
rem 313.当前目录:自定义字符和开始序数(两位)
:GTDIRCUSTOMNAMEORDINALOO
set SubFilePrefix=""
set StartNum=0
set /p SubFilePrefix="前缀字符:"
set /p StartNum="开始序数:"
set /a StartNum-=1
set NOrderBy=1
@echo.
@echo 文件排序方式
@echo --------------------------------------------
@echo 1.文件大小
@echo 2.文件小大
@echo 3.创建时间早晚
@echo 4.创建时间晚早
@echo 5.文件名A-Z
@echo 6.文件名Z-A
@echo 7.扩展名A-Z
@echo 8.扩展名Z-A
@echo --------------------------------------------
:GTDIRCUSTOMNAMEORDINALOOORDERBY
set /p NOrderBy="文件排序方式:"
set OrderBy=d
if %NOrderBy% equ 1 (set OrderBy=-s)
if %NOrderBy% equ 2 (set OrderBy=s)
if %NOrderBy% equ 3 (set OrderBy=d)
if %NOrderBy% equ 4 (set OrderBy=-d)
if %NOrderBy% equ 5 (set OrderBy=n)
if %NOrderBy% equ 6 (set OrderBy=-n)
if %NOrderBy% equ 7 (set OrderBy=-e)
if %NOrderBy% equ 8 (set OrderBy=e)
echo %NOrderBy%|findstr /be "[1-8]" >nul || (echo 选择支持的排序方式 & goto GTDIRCUSTOMNAMEORDINALOOORDERBY)
cd /d "!HandleDir!"
for /f "usebackq tokens=*" %%i in (`dir /a-d /b /o!OrderBy!`) do (
if not "%%i"=="%~n0%~x0" (
set /a StartNum+=1
if !StartNum! LSS 10 ( ren "%%i" "!SubFilePrefix!0!StartNum!%%~xi" ) else ( ren "%%i" "!SubFilePrefix!!StartNum!%%~xi" )
))
echo.
echo :) 搞定(回车继续其它操作)
pause >nul
goto GTCONTINUE
rem 314.当前目录:目录名-三位序数
:GTDIRNAMEORDINALOOO
set NOrderBy=1
@echo.
@echo 文件排序方式
@echo --------------------------------------------
@echo 1.文件大小
@echo 2.文件小大
@echo 3.创建时间早晚
@echo 4.创建时间晚早
@echo 5.文件名A-Z
@echo 6.文件名Z-A
@echo 7.扩展名A-Z
@echo 8.扩展名Z-A
@echo --------------------------------------------
:GTDIRNAMEORDINALOOOORDERBY
set /p NOrderBy="文件排序方式:"
set OrderBy=d
if %NOrderBy% equ 1 (set OrderBy=-s)
if %NOrderBy% equ 2 (set OrderBy=s)
if %NOrderBy% equ 3 (set OrderBy=d)
if %NOrderBy% equ 4 (set OrderBy=-d)
if %NOrderBy% equ 5 (set OrderBy=n)
if %NOrderBy% equ 6 (set OrderBy=-n)
if %NOrderBy% equ 7 (set OrderBy=-e)
if %NOrderBy% equ 8 (set OrderBy=e)
echo %NOrderBy%|findstr /be "[1-8]" >nul || (echo 选择支持的排序方式 & goto GTDIRNAMEORDINALOOOORDERBY)
cd /d "!HandleDir!"
set /a num=0
for /f "usebackq delims=" %%i in (\'%cd%\') do set "DirNamePrefix=%%~nxi"
for /f "usebackq delims=" %%i in (`dir /a-d /b /o!OrderBy! *.* 2^>nul`) do (
if not "%~nx0"=="%%i" (
set /a num+=1
if !num! LSS 10 (
ren "%%i" "%DirNamePrefix%-00!num!%%~xi"
) else (
if !num! LSS 100 (
ren "%%i" "%DirNamePrefix%-0!num!%%~xi"
) else (
ren "%%i" "%DirNamePrefix%-!num!%%~xi"
)
)
))
echo.
echo :) 搞定(回车继续其它操作)
pause >nul
goto GTCONTINUE
rem 315.子目录:子目录名-三位序数
:GTSUBDIRNAMEORDINALOOO
set NOrderBy=1
@echo.
@echo 文件排序方式
@echo --------------------------------------------
@echo 1.文件大小
@echo 2.文件小大
@echo 3.创建时间早晚
@echo 4.创建时间晚早
@echo 5.文件名A-Z
@echo 6.文件名Z-A
@echo 7.扩展名A-Z
@echo 8.扩展名Z-A
@echo --------------------------------------------
:GTSUBDIRNAMEORDINALOOOORDERBY
set /p NOrderBy="文件排序方式:"
set OrderBy=d
if %NOrderBy% equ 1 (set OrderBy=-s)
if %NOrderBy% equ 2 (set OrderBy=s)
if %NOrderBy% equ 3 (set OrderBy=d)
if %NOrderBy% equ 4 (set OrderBy=-d)
if %NOrderBy% equ 5 (set OrderBy=n)
if %NOrderBy% equ 6 (set OrderBy=-n)
if %NOrderBy% equ 7 (set OrderBy=-e)
if %NOrderBy% equ 8 (set OrderBy=e)
echo %NOrderBy%|findstr /be "[1-8]" >nul || (echo 选择支持的排序方式 & goto GTSUBDIRNAMEORDINALOOOORDERBY)
cd /d "!HandleDir!"
for /f "usebackq delims=" %%i in (`dir /ad /b /s^|sort /r`) do (
set /a num=0
for /f "usebackq delims=" %%j in (`dir /a-d /b /o!OrderBy! "%%i\\*.*" 2^>nul`) do (
set /a num+=1
if !num! LSS 10 (
ren "%%i\\%%j" "%%~nxi-00!num!%%~xj"
) else (
if !num! LSS 100 (
ren "%%i\\%%j" "%%~nxi-0!num!%%~xj"
) else (
ren "%%i\\%%j" "%%~nxi-!num!%%~xj"
)
)
))
echo.
echo :) 搞定(回车继续其它操作)
pause >nul
goto GTCONTINUE
rem 316.自定义字符和开始序数(三位)
:GTDIRCUSTOMNAMEORDINALOOO
set SubFilePrefix=""
set StartNum=0
set /p SubFilePrefix="前缀字符:"
set /p StartNum="开始序数:"
set /a StartNum-=1
set NOrderBy=1
@echo.
@echo 文件排序方式
@echo --------------------------------------------
@echo 1.文件大小
@echo 2.文件小大
@echo 3.创建时间早晚
@echo 4.创建时间晚早
@echo 5.文件名A-Z
@echo 6.文件名Z-A
@echo 7.扩展名A-Z
@echo 8.扩展名Z-A
@echo --------------------------------------------
:GTDIRCUSTOMNAMEORDINALOOOORDERBY
set /p NOrderBy="文件排序方式:"
set OrderBy=d
if %NOrderBy% equ 1 (set OrderBy=-s)
if %NOrderBy% equ 2 (set OrderBy=s)
if %NOrderBy% equ 3 (set OrderBy=d)
if %NOrderBy% equ 4 (set OrderBy=-d)
if %NOrderBy% equ 5 (set OrderBy=n)
if %NOrderBy% equ 6 (set OrderBy=-n)
if %NOrderBy% equ 7 (set OrderBy=-e)
if %NOrderBy% equ 8 (set OrderBy=e)
echo %NOrderBy%|findstr /be "[1-8]" >nul || (echo 选择支持的排序方式 & goto GTDIRCUSTOMNAMEORDINALOOOORDERBY)
cd /d "!HandleDir!"
for /f "usebackq tokens=*" %%i in (`dir /a-d /b /o!OrderBy!`) do (
if not "%%i"=="%~n0%~x0" (
set /a StartNum+=1
if !StartNum! LSS 10 (
ren "%%i" "!SubFilePrefix!00!StartNum!%%~xi"
) else (
if !StartNum! LSS 100 (
ren "%%i" "!SubFilePrefix!0!StartNum!%%~xi"
) else (
ren "%%i" "!SubFilePrefix!!StartNum!%%~xi"
)
)
))
echo.
echo :) 搞定(回车继续其它操作)
pause >nul
goto GTCONTINUE
rem 替换当前目录下文件名
:GTDIRREPLACE
cd /d "!HandleDir!"
set /p oldStr=原字符:
set /p newStr=替换成:
for /f "usebackq tokens=*" %%a in (`dir /a-d /b`) do (
set "newFileName=%%~na"
set "newFileName=!newFileName:%oldStr%=%newStr%!"
ren "%%a" "!newFileName!%%~xa"
)
echo.
echo :) 搞定(回车继续其它操作)
pause >nul
goto GTCONTINUE
rem 替换所有子层级中的文件名
:GTSUBDIRREPLACE
cd /d "!HandleDir!"
set /p oldStr=原字符:
set /p newStr=替换成:
for /f "usebackq delims=" %%i in (`dir /ad /b /s^|sort /r`) do (
for /f "usebackq delims=" %%j in (`dir /a-d /b "%%i\\" 2^>nul`) do (
set "newFileName=%%~nj"
set "newFileName=!newFileName:%oldStr%=%newStr%!"
ren "%%i\\%%j" "!newFileName!%%~xj"))
set oldStr=""
set newStr=""
set newFileName=""
echo.
echo :) 搞定(回车继续其它操作)
pause >nul
goto GTCONTINUE
rem 删除当前目录下文件名
:GTDIRDEL
cd /d "!HandleDir!"
set /p DelStr=删除字符:
for /f "usebackq tokens=*" %%a in (`dir /a-d /b`) do (
set "str=%%~na"
set "str=!str:%DelStr%=!"
ren "%%a" "!str!%%~xa"
)
echo.
echo :) 搞定(回车继续其它操作)
pause >nul
goto GTCONTINUE
rem 删除所有子层级中的文件名
:GTSUBDIRDEL
cd /d "!HandleDir!"
set /p DelStr=删除字符:
for /f "usebackq delims=" %%i in (`dir /ad /b /s^|sort /r`) do (
for /f "usebackq delims=" %%j in (`dir /a-d /b "%%i\\" 2^>nul`) do (
set "str=%%~nj"
set "str=!str:%DelStr%=!"
ren "%%i\\%%j" "!str!%%~xj"))
set DelStr=""
set str=""
echo.
echo :) 搞定(回车继续其它操作)
pause >nul
goto GTCONTINUE
:GTExit
exit
转载保留原文地址和作者名,只为错误勘正和信息补充。
版权声明:自由转载-非商用-非衍生-保持署名(创意共享3.0许可证)