PeterS
PeterS

Reputation: 724

How to rename file with pattern using batch script

For example I have a .txt file named pair.txt.

Example:

AAA_BBB_CCC_DDD_EEE_FFF_GGG_HHH.idoc.xml            AAAA
AAA_BBB_CCC_DDD_EEE_FFF_111_222.idoc.xml            BBBB
AAA_BBB_CCC_DDD_EEE_FFF_333_444.idoc.xml            CCCC

Now this file contains 2 columns of filenames. First column will be the pattern to rename the second column. Now I want to use the right side of 6th and 7th "_" as pattern. The final filenames of the files in the second column must be:

AAAA.GGG_HHH
BBBB.111_222
CCCC.333_444

As you noticed, I didn't include the .idoc.xml part. Now I want to put the code within this for statement:

for /f "tokens=1,2" %%a in ('type c:\user\pair.txt') do (
        echo Renaming file : %%b
        )

How will I able to do this?

Upvotes: 0

Views: 757

Answers (1)

MC ND
MC ND

Reputation: 70923

for /f "usebackq tokens=1,2" %%a in ("c:\user\pair.txt") do (
    for /f "tokens=7,8 delims=_." %%c in ("%%a") do (
        echo Renaming file : %%b = %%b.%%c_%%d
    )
)

Use a second for command to split the first column and then use the adecuated tokens

Upvotes: 1

Related Questions