简体   繁体   English

正则表达式重命名批处理文件

[英]Regex Rename with batch for files

I would like to rename several hundred files with a batch program.我想用批处理程序重命名数百个文件。 defacon to have their numbers appear first but I'm not very easy with the batch. defacon 让他们的号码首先出现,但我对这批不太容易。 example of the name of my files:我的文件名称示例:

3705_(2x).stl
68403_(4x).stl
6525_(2x).stl
22073_(3x).stl

to:至:

2x-3705.stl
2x-6525.stl
3x-22073.stl
4x-68403.stl

I have tried:我努力了:

@echo off
set /p d=disque: // c:
set /p c=chemin: // \Users\celes\file
cls
%d%
cd%c%
set nb = findstr /r '[0-9]+x'
set p = findstr /r '[0-9]{2,}'
ren *.stl %nb%%p%.stl
pause>nul

You can use this batch file:您可以使用此批处理文件:

@echo off
for /F "eol=| delims=" %%I in ('dir "*_(*).stl" /A-D /B 2^>nul') do for /F "tokens=1,2 delims=()_" %%A in ("%%~nI") do ECHO ren "%%I" "%%B-%%A%%~xI"
pause

The outer FOR runs command DIR in a separate command process in background to get a list of just file names matching the pattern *_(*).stl which does not change during the file rename operations.外部FOR在后台的单独命令进程中运行命令DIR以获取与模式*_(*).stl匹配的文件名列表,该模式在文件重命名操作期间不会更改。

The outer FOR processes each file name with default behavior of splitting each line up into tokens using horizontal tabs and spaces as delimiters being disabled by specifying an empty list of delimiters.外部FOR处理每个文件名的默认行为是使用水平制表符和空格作为分隔符将每一行拆分为标记,通过指定一个空的分隔符列表来禁用分隔符。

The inner FOR loop processes just each file name string specified in double quotes.内部FOR循环仅处理双引号中指定的每个文件名字符串。 The command FOR is instructing with the option string tokens=1,2 delims=()_ to interpret all parentheses and underscores as delimiters and split the file name string up to at least two substrings.命令FOR使用选项字符串tokens=1,2 delims=()_指示将所有括号和下划线解释为分隔符,并将文件名字符串拆分为至少两个子字符串。 The first substring is the number before underscore which is assigned to specified loop variable A .第一个子字符串是下划线之前的数字,分配给指定的循环变量A The second substring being the string between the parentheses is assigned to next loop variable according to ASCII table which is the loop variable B .第二个子字符串是括号之间的字符串,根据循环变量BASCII表分配给下一个循环变量。

The command REN executed by the inner FOR renames the current file as hold in loop variable I of outer FOR to new name using the two substrings determined by the inner FOR .内部FOR执行的命令REN使用内部FOR确定的两个子字符串将当前文件重命名为外部FOR的循环变量I中的保持文件为新名称。

This batch file contains command ECHO before command ren to just output the rename command instead of really running it.此批处理文件在命令ren之前包含命令ECHO ,仅输出重命名命令而不是真正运行它。 So you can check the batch file and verify the file rename operations before really doing them.因此,您可以在真正执行之前检查批处理文件并验证文件重命名操作。 Remove command ECHO to really do the file renames.删除命令ECHO以真正进行文件重命名。

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.要了解使用的命令及其工作原理,请打开命令提示符窗口,在其中执行以下命令,并仔细阅读每个命令显示的所有帮助页面。

  • echo /?
  • for /?
  • pause /?
  • ren /?

Read also the Microsoft article about Using command redirection operators for an explanation of 2>nul .另请阅读有关使用命令重定向运算符的 Microsoft 文章,了解2>nul的说明。 The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir command line with using a separate command process started in background.重定向运算符>必须在FOR命令行上使用脱字符^进行转义,以便在 Windows 命令解释器在执行命令FOR之前处理此命令行时解释为文字字符,该命令使用在后台启动的单独命令进程执行嵌入式dir命令行。

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

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