使用 Windows 批处理脚本重命名目录中的所有文件 [英] Rename all files in a directory with a Windows batch script

查看:69
本文介绍了使用 Windows 批处理脚本重命名目录中的所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将如何编写一个批处理或 cmd 文件来重命名目录中的所有文件?我使用的是 Windows.

改变这个:

750_MOT_Forgiving_120x90.jpg751_MOT_Persecution_1_120x90.jpg752_MOT_Persecution_2_120x90.jpg753_MOT_Hatred_120x90.jpg754_MOT_Suffering_120x90.jpg755_MOT_Freedom_of_Religion_120x90.jpg756_MOT_Layla_Testimony_1_120x90.jpg757_MOT_Layla_Testimony_2_120x90.jpg

为此:

750_MOT_Forgiving_67x100.jpg751_MOT_Persecution_1_67x100.jpg752_MOT_Persecution_2_67x100.jpg753_MOT_Hatred_67x100.jpg754_MOT_Suffering_67x100.jpg755_MOT_Freedom_of_Religion_67x100.jpg756_MOT_Layla_Testimony_1_67x100.jpg757_MOT_Layla_Testimony_2_67x100.jpg

解决方案

一个 FOR 语句来循环名称(键入 FOR/? 获取帮助),以及字符串搜索和替换(键入 SET/? 寻求帮助).

@echo offsetlocal enableDelayedExpansion对于 (*120x90.jpg) 中的 %%F 做 (设置名称=%%F"ren !名字!"!名称:120x90=67x100!")


更新 - 2012 年 11 月 7 日

我研究了 RENAME 命令如何处理通配符:Windows RENAME 命令如何解释通配符?>

事实证明,使用 RENAME 命令可以非常轻松地解决这个特定问题,而无需任何批处理脚本.

ren *_120x90.jpg *_67x100.*

_ 后面的字符数无关紧要.如果 120x90 变成 xxxxxxxxxxx,重命名仍然可以正常工作.这个问题的重要方面是替换了最后一个_.之间的整个文本.

How would I write a batch or cmd file that will rename all files in a directory? I am using Windows.

Change this:

750_MOT_Forgiving_120x90.jpg
751_MOT_Persecution_1_120x90.jpg
752_MOT_Persecution_2_120x90.jpg
753_MOT_Hatred_120x90.jpg
754_MOT_Suffering_120x90.jpg
755_MOT_Freedom_of_Religion_120x90.jpg
756_MOT_Layla_Testimony_1_120x90.jpg
757_MOT_Layla_Testimony_2_120x90.jpg

To this:

750_MOT_Forgiving_67x100.jpg
751_MOT_Persecution_1_67x100.jpg
752_MOT_Persecution_2_67x100.jpg
753_MOT_Hatred_67x100.jpg
754_MOT_Suffering_67x100.jpg
755_MOT_Freedom_of_Religion_67x100.jpg
756_MOT_Layla_Testimony_1_67x100.jpg
757_MOT_Layla_Testimony_2_67x100.jpg

解决方案

A FOR statement to loop through the names (type FOR /? for help), and string search and replace (type SET /? for help).

@echo off
setlocal enableDelayedExpansion
for %%F in (*120x90.jpg) do (
  set "name=%%F"
  ren "!name!" "!name:120x90=67x100!"
)


UPDATE - 2012-11-07

I've investigated how the RENAME command deals with wildcards: How does the Windows RENAME command interpret wildcards?

It turns out that this particular problem can be very easily solved using the RENAME command without any need for a batch script.

ren *_120x90.jpg *_67x100.*

The number of characters after the _ does not matter. The rename would still work properly if 120x90 became x or xxxxxxxxxx. The important aspect of this problem is that the entire text between the last _ and the . is replaced.

这篇关于使用 Windows 批处理脚本重命名目录中的所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆