简体   繁体   中英

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. 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.

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.

The inner FOR loop processes just each file name string specified in double quotes. 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. The first substring is the number before underscore which is assigned to specified loop variable 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 .

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 .

This batch file contains command ECHO before command ren to just output the rename command instead of really running it. 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.

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 . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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