batch-file - 如何在批处理脚本中文件名的最后 6 位之前附加符号?
问题描述
我正在为以下任务创建批处理脚本:
任务 1:在文件夹中的所有文件名中查找特定名称 (.txt.) 并替换为特定名称 (.txt_)。
任务 2:在批处理脚本中文件名的最后 6 位之前附加一个符号 (_)。
如:Test.txt.20210808654321
待定:Test.txt_20210808_654321
我为任务 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 位数字之前附加一个符号(_))。
任何帮助完成它?
解决方案
这是一种方法,无需使用“查找”和“替换”字符串:
@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")
推荐阅读
- android - 通过android中的adb shell命令行将json字符串发送到模拟器|设备
- javascript - Microsoft Edge - 无法为 KeyboardEvent 设置 keyCode(合成)
- c# - 选择所有类型的雇员,然后选择特定的雇员
- c# - 未找到与 RestoreAssemblyResources AssemblyName 元数据“Xamarin.Firebase.Messaging”匹配的参考
- c# - WPF 应用程序 - 无法更改 MouseDown 上的矩形属性
- css - 将 SAPUI5 按钮图标放在文本上(不是左侧)
- r - 我在梯度提升机(GBM)模型上设置种子,但我不断得到不同的预测
- python - 表中的 Django 选择选项不起作用
- android - Flutter 发布 apk 擦除数据
- sql - Oracle:如果我给出任何日期,则查询以获取一周的最后一个星期五