如何使用BATCH文件为文件夹中的所有文件添加和删除postfix

时间:2013-10-24 11:16:19

标签: windows file batch-file batch-rename

我正在尝试在所有文件夹和子文件夹中添加和删除特定文件类型的后缀 我知道添加前缀的代码,但我不知道postfix的代码

这是前缀

的代码

添加PreFix:

@echo off
pushd "D:\Temp\Test\"
for /F "delims=" %%j in ('dir /s /b *.avi *.mkv *.mp4 *.wmv *.mpeg *.flv *.dat *.mov *.asf *.3gp *.srt') do (
   rename "%%j" "[my.prefix]-%%~nxj"
)
popd

删除PreFix:

@echo off
pushd "D:\Temp\Test\"
for /F "delims=" %%a in ('dir /s /b *.avi *.mkv *.mp4 *.wmv *.mpeg *.flv *.dat *.mov *.asf *.3gp *.srt') do (
    set "fname=%%~nxa"
    set "fpath=%%~dpa"
    setlocal enabledelayedexpansion
    set "nname=!fname:~15!"
    ren "!fpath!!fname!" "!nname!"
    endlocal
)
popd

如何为postfix做这个?

2 个答案:

答案 0 :(得分:1)

添加后缀

@echo off
pushd "D:\Temp\Test\"
for /F "delims=" %%j in ('dir /s /b *.avi *.mkv *.mp4 *.wmv *.mpeg *.flv *.dat *.mov *.asf *.3gp *.srt') do (
   rename "%%j" "%%~nj-[my.suffix]%%~xj"
)
popd

删除后缀

@echo off
pushd "D:\Temp\Test\"
for /F "delims=" %%a in ('dir /s /b *.avi *.mkv *.mp4 *.wmv *.mpeg *.flv *.dat *.mov *.asf *.3gp *.srt') do (
    set "fname=%%~nxa"
    setlocal enabledelayedexpansion
    set "nname=!fname:-[my.suffix]=!"
    ren "%%a" "!nname!"
    endlocal
)
popd

答案 1 :(得分:0)

set fileName=thisIsATest_0001
set fileExtension=.avi

echo %fileName:~0,-5%%fileExtension%

子串的第二部分中的负值表示距字符串末尾的距离