How to find and replace part of filenames from list

最后都变了- 提交于 2020-04-16 05:48:09

问题


I have a list of files that I need to rename at the same part of each file, with different values.

Example:

BL_1402B103_abc.wav > BL_C1234-1_abc.wav

BL_15489B59_abc.wav > BL_C1234-5_abc.wav

So in the first example above I want to replace the 1402B103 with C1234-1 all the files are the same length and the sections I want to replace are separated by "_".

I have some code for finding/replacing parts of a filename but I need to do this for hundreds of files - is there a way to pull Pattern= & Replace= as variables from a csv/list and run as a batch?

Setlocal enabledelayedexpansion

Set "Pattern=1402B103"
Set "Replace=C1234-1"

For %%f in (*.wav) Do (
    Set "File=%%~f"
    Ren "%%f" "!File:%Pattern%=%Replace%!"
)

回答1:


You could create a csv file and add your search/replace strings:

myfile.csv

1402B103,C1234-1
15489B59,C1234-5
etc,etc

The batch file, myrename.cmd

@echo off
setlocal enabledelayedexpansion
for /f "tokens=1,* delims=," %%i in (myfile.csv) do (
    set "search=%%i"
    set "replace=%%j"
    call :fix
)
exit /b
:fix
    for %%a in (*!search!*.wav) do (
        set "file=%%a"
        set "file=!file:%search%=%replace%!!"
        echo ren "%%~fa" "!file!" 
)

It will seatch for each string in the csv file, split by comma assign the first meta variable to the search variable and the second to the replace variable. Then we simply do the replace for each by calling that procedure.

Note!! in this instance I used echo before ren for testing results. Only once you are happy with your results should you remove echo to perform the actual command.




回答2:


I would do such a multi-rename operation of files using shareware Total Commander with its built-in multi-rename tool which has a every easy to use graphical user interface for such tasks making it possible to review the new names of the files before executing the rename operation. This file rename operation could be done with Total Commander nearly complete using only some mouse clicks, just C1234- need to be typed on keyboard. And Total Commander supports even an undo if the rename operation fails for some reason.

Let us assume C1234- in new file name is a fixed sequence of characters and 1 and 5 is a number incremented by one on each renamed each file.

@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "FileNumber=1"
for /F "eol=| delims=" %%I in ('dir BL_????????_abc.wav /A-D-H /B /ON 2^>nul') do (
    move /Y "%%I" "BL_C1234-!FileNumber!_abc%%~xI" >nul
    set /A FileNumber+=1
)
endlocal

This solution works for the example.

But what about string left to first underscore and string right to second underscore vary from file name to file name?

In this case the following batch file could be the right solution:

@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "FileNumber=1"
for /F "eol=| delims=" %%I in ('dir *_????????_*.wav /A-D-H /B /ON 2^>nul') do (
    for /F "eol=| tokens=1,2* delims=_" %%A in ("%%~nxI") do (
        move /Y "%%I" "%%A_C1234-!FileNumber!_%%C" >nul
    )
    set /A FileNumber+=1
)
endlocal

The command MOVE with option /Y is used instead of command REN to make the file rename even on a file with that name is already existing. Total Commander would inform the user about such an issue on renaming files with other files with new name already existing.

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.

  • dir /?
  • echo /?
  • endlocal /?
  • for /?
  • move /?
  • set /?
  • setlocal /?

Read 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 in a separate command process started in background.



来源:https://stackoverflow.com/questions/55866006/how-to-find-and-replace-part-of-filenames-from-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!