4

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.

3
  • Try ren *%src% *%dest%` where src and dest are input from a set /p src=... and set /p dest=... Commented Dec 9, 2013 at 11:48
  • For some reason,Its syntax error.can you write a FOR loop? Commented Dec 9, 2013 at 12:12
  • Please check where I went wrong? code REN "*%src%.sh" "*%dest%.sh" acc' to what you suggested and my src=991 and my dest=998.Initially my filename was Test991.sh but now,its test9998.sh.why is this extra 9 coming and where from it is coming?Iam surprised.And How can I remove this character which is appearing in some filenames?Please help. Commented Dec 9, 2013 at 12:49

2 Answers 2

6

Often times you do need a small batch script to execute some seemingly simple rename operations. However, you only need the REN command in this case.

ren *001.txt *03.txt 

See How does the Windows RENAME command interpret wildcards? for an explanation as to why this works.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your reply.But I don't want src and dest to be hardcoded.I want them as input from the users.Don't know why an extra '9' is coming and how I can remove it.
5

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

1 Comment

If any needs to modify multiple files with pattern just use the code provided by Infinite Recursion, only modifiy the %string1%.* with %string1%*.*

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.