首页 > 解决方案 > 如何在批处理脚本中文件名的最后 6 位之前附加符号?

问题描述

我正在为以下任务创建批处理脚本:

我为任务 1创建了一个脚本,它正在工作。

脚本:

@echo off
setlocal enabledelayedexpansion

set "replace=.txt_"
set "find=.txt."

for %%i in ("C:\Script test\*.*") do (
   set name=%%~ni
   set ext=%%~xi
   ren "%%i" "!name:%find%=%replace%!!ext!"
)
pause

但是我在任务 2 中感到震惊(= 在文件名的最后 6 位数字之前附加一个符号(_))。

任何帮助完成它?

标签: batch-file

解决方案


这是一种方法,无需使用“查找”和“替换”字符串:

@For /F Delims^= %%G In ('Set "PATHEXT=" ^& %SystemRoot%\System32\where.exe
 ".":"*.txt.??????????????" 2^>NUL') Do @(Set "}=%%~xG"
    SetLocal EnableDelayedExpansion & For /F Delims^=. %%H In ("!}:~,9!_!}:~9!"
) Do @EndLocal & Ren "%%G" "%%~nG_%%~H")

推荐阅读