How to replace part of a file name with another name in batch processing

batch-file, batch-processing

Solution

Use the following commands in a batch file, say `sample.bat`

@echo off
setlocal enableDelayedExpansion
set /p "string1=Enter the sequence to be replaced : "
set /p "string2=Enter the new sequence  : "
for %%F in (*%string1%.*) do (
  set "filename=%%F"
  ren "!filename!" "!filename:%string1%=%string2%!"
)

You can execute this batch file from the command prompt using the following command:

sample.bat

Problem

I have been trying to replace a part of some file names in a directory to new names. I have found many examples regarding this replacement using `REN` command with a for loop. Example: if I want to replace test001 to test003, I can replace using `REN`. But what,if I take 001 and 003 as user input through set `\p` command and I want the output to be test003, for the input test001. I have the following files in my folder: Test001.txt user001.txt fjkdjdl001.txt I want to convert them to Test0003.txt user003.txt and so on. But 001,003 are user inputted. How for+ren act with user inputted characters.Please help me.

Original source