如何使用批处理删除多个文件中某些字符后的字符串? [英] How to delete strings after certain character in multiple files using batch?

查看:162
本文介绍了如何使用批处理删除多个文件中某些字符后的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试删除_1之后的一些字符并重命名该文件,但是卡住了.让我输入示例文件名以使其清楚.

I have been trying to delete some characters after _1 and rename the file but got stuck. let me put in sample file names to make it clear.

aero_name_1_3_7.png
glik_trol_1_5_2_9_5.png
this_that_1_3_1_9.png
....... and so on. 

因此,我的文件名包含完全相同的格式,但名称不同,因此我认为应使用标记和delims.重命名我的文件后,外观应为aero_name_1.png,glik_trol_1.png,this_that_1.png 这是我写的蝙蝠,但这是行不通的.如果有人可以建议,那就太好了.

So my filenames contain exact format but different names and hence I thought to use tokens and delims. After renaming my files should look aero_name_1.png, glik_trol_1.png, this_that_1.png Here is the piece of bat that I wrote but this does not work. If someone could suggest, that would be great.

for /F "tokens=1-4,* delims=_" %%a in ('dir /A-D /B "*.png"') do (
  move "%%a_%%b" "%%a%%~xb"
)

谢谢

推荐答案

查看您的第二个文件名,这是FOR/F循环的内容:

Looking at your 2nd file name, this is what the FOR /F loop has:

%%a=glik
%%b=trol
%%c=1
%%d=5
%%e=2_9_5.png

因此,您的MOVE命令变为:

So your MOVE command becomes:

move "glik_trol" "glik"

请注意,%%b没有扩展名

您可以使用

for /f "tokens=1-3* delims=_" %%a in ('dir /a-d /b *.png') do (
  move "%%a_%%b_%%c_%%d" "%%a_%%b_%%c%%~xd"
)

如果您对正则表达式感到满意,则可以使用我方便的 JREN.BAT 是可在任何本地运行的纯脚本从XP开始的Windows机器.这是执行非常精确的重命名操作的一种非常方便的方法.

If you are comfortable with regular expressions, then you could use my handy JREN.BAT utility - a hybrid JScript/batch script that renames files by performing a regular expression find and replace on the name. JREN.BAT is pure script that runs natively on any Windows machine from XP onward. It is a very convenient method to perform very precise rename operations.

假设您的 JREN.BAT 当前目录,或者更好的是,位于您PATH中的某个位置:

Assuming you have JREN.BAT in your current directory, or better yet, somewhere within your PATH:

jren "^([^_]+_[^_]+_[^_]+)_.*(\.png)$" "$1$2" /i

不需要批次.如果确实在批处理脚本中使用该命令,则应在命令前加上CALL前缀,因为JREN也是批处理脚本.

No batch is needed. If you do use the command in a batch script, then you should prefix the command with CALL because JREN is also a batch script.

这篇关于如何使用批处理删除多个文件中某些字符后的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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