command-line - 批量重命名脚本以仅保留原始文件名的某些字符?

标签 command-line batch-file file-rename

我需要用可变大小的原始文件名重命名数千个 rar 文件。我必须通过保留原始文件名的前 3 个和最后 4 个字符并在中间添加 3 个随机字符 [数字],使它们的长度为 10 个字符。

示例:

输入:

“约翰·多伊 - 简·多伊 - 19073275.rar”

“XXXX-XYXY-98705674.rar

输出:

“Joh1273275.rar”

“XXX9795674.rar”

接下来,.bat 应该为每个文件生成一个 .txt,其中包含原始名称和修改后的名称!

我知道这是可能的,但在写它时我完全愚蠢。请帮忙!

最佳答案

下面的批处理文件可以完成您想要的操作:

@echo off
setlocal EnableDelayedExpansion
for %%a in (*.rar) do (
   set name=%%~Na
   set num=00!random!
   set newName=!name:~0,3!!num:~-3!!name:~-4!
   ren "%%a" "!newName!%%~Xa"
   echo "%%a" modified to "!newName!%%~Xa" >> log.txt
)

关于command-line - 批量重命名脚本以仅保留原始文件名的某些字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14928294/

相关文章:

batch-file - 批处理 '+ unexpected' 错误

c++ - 尝试向文件添加扩展名时,Rename() 函数不起作用

mysql - 为什么我在尝试使用命令行进入 mysql 时收到 "Permission Denied"?

linux - 如何使 Bash 命令将 "-"识别为文件名的一部分而不是选项?

python - 从 bash 命令行获取未扩展的参数

bash脚本: how to change the position of artist and song name in a filename

php - PDF uploader 脚本

command-line - 如何在 Bitbucket 中通过命令行发送拉取请求?

excel - 使用 VBA 自动进行 FTP 上传

audio - 通过执行批处理文件/powershell 脚本设置播放设备