简体   繁体   English

在Windows批处理脚本中特殊字符的倍数后截断文件名?

[英]Truncate filename after a multiple of a special character in windows batch script?

I want to remove the part of a filename after the third "_" from thousand of files. 我想从数千个文件中删除第三个“ _”之后的文件名部分。 The structure after the third "_" varies and contains "_" in some cases. 第三个“ _”之后的结构有所不同,在某些情况下包含“ _”。 The length of the first part varies so I can't just remove the first 15 characters. 第一部分的长度各不相同,因此我不能只删除前15个字符。 The result should be unique. 结果应该是唯一的。

The filenames look like this: 文件名看起来像这样:

00_TEXT_=Text00._AA1234L_AA1_1.pdf
00_TEX_=Text00._AA1234L_AA1_2.pdf
00_TEXT_=TextText00._DD2023A.pdf
00_TEXT_=Text00._AA2345L_BB1_1.pdf
00_TEXT_=Text00._AA2345L_BB1_2.pdf

The result should look like this: 结果应如下所示:

AA1234L_AA1_1.pdf
AA1234L_AA1_2.pdf
DD2023A.pdf
AA2345L_BB1_1.pdf
AA2345L_BB1_2.pdf

Any idea why this is not working: 任何想法为什么这不起作用:

@echo off
setlocal enabledelayedexpansion
set deletestring=*_*_*_
for /f "delims==" %%F in ('dir /b ^| find "%deletestring%"') do (
set oldfilename=%%F
set newfilename=!oldfilename:%deletestring%=!
Ren "!oldfilename!" "!newfilename!"
)

I was able to get it working with this: 我能够使它与此一起工作:

@echo off
setlocal enabledelayedexpansion
set deletestring=*_*_*_*

for /f "tokens=1,2,3,* delims=_" %%F in ('dir /b "%deletestring%"') do (
    Ren "%%F_%%G_%%H_%%I" "%%I"
)

endlocal

Note that enabledelayedexpansion isn't really needed in the above. 请注意, enabledelayedexpansion实际上并不需要enabledelayedexpansion

Alternately, you could do this as a single line (no batch file needed): 或者,您可以单行执行此操作(无需批处理文件):

for /f "tokens=1,2,3,* delims=_" %F in ('dir /b "*_*_*_*"') do Ren "%F_%G_%H_%I" "%I"

The idea is to simply split the matching filenames apart by underscores and then reconstruct the names during the rename process ( %%F_%%G_%%H_%%I gives the original file name when going through the loop). 这个想法是简单地用下划线将匹配的文件名分开,然后在重命名过程中重建名称( %%F_%%G_%%H_%%I在循环时给出原始文件名)。 Then rename the file to everything after the 3rd underscore, which is the %%I value. 然后将文件重命名为第三个下划线后的所有内容,即%%I值。

Your FINDSTR search is wrong - a string of any characters (wildcard) is .* , not * . 您的FINDSTR搜索错误-任何字符(通配符)的字符串都是.* ,而不是*

Variable find/replace does not support wildcards, except for the !var:*search=! 变量查找/替换不支持通配符,除了!var:*search=! syntax that replaces everthing up until the first occurrence of "search". 直到第一次出现“搜索”之前都将替换所有内容的语法。

There is no need for FINDSTR, all you need is DIR with normal wildcard masking. 不需要FINDSTR,您只需要具有常规通配符掩码的DIR。

You can use FOR /F to parse the name into tokens. 您可以使用FOR / F将名称解析为令牌。 I use two loops - the first to get the entire name, and the second to parse out the portion after the 3rd _ . 我使用两个循环-第一个循环获取完整名称,第二个循环解析出第3个_之后的部分。

The following should work: 以下应该工作:

@echo off
for /f "eol=: delims=" %%A in (
  'dir /b /a-d *_*_*_*'
) do for /f "tokens=3* delims=_" %%B in ("%%A") do ren "%%A" "%%C"

Or you could use my jren.bat utility that renames files using regular expression replacement. 或者,您可以使用我的jren.bat实用程序 ,该实用程序使用正则表达式替换来重命名文件。 It is a hybrid JScript/batch script that runs natively on any Windows machine from XP onward. 它是混合JScript /批处理脚本,可从XP开始在任何Windows计算机上本地运行。

jren "^(.*?_){3}" ""

Use CALL JREN if you put the command within another batch script. 如果将命令放在另一个批处理脚本中,请使用CALL JREN。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM