1

I'm looking for a solution on this.

I have a huge number of files that look like the following,

{some_texthere_133}_Ritem_123.xml
{some_texthere_233}_Ritem_456.xml 
{some_texthere_333}_Ritem_564.xml 

I would like to detect any file names in the current directory that contain text or underscore within brackets as the prefix followed by underscore outside it as well, and remove that portion of the file name.

Ritem_123.xml
Ritem_456.xml
Ritem_789.xml

The above mentioned format should be the result. Can any one help me.

3
  • @aschipfl..This is one of the command i found.@ECHO OFF FOR %%F IN ("[] *") DO CALL :process "%%F" GOTO :EOF :process SET oldname=%1 SET "newname=%~nx1" SET "newname=%newname:] =%" RENAME %oldname% "%newname%"....which works fine when there are square brackets and a space.could you help me modify it??? Commented Mar 22, 2016 at 12:50
  • Please include your code into the question by editing it; thanks! Commented Mar 22, 2016 at 14:54
  • @a_horse_with_no_name--IT would be very useful if files are renamed as Ritem_121.xml Ritem_452.xml Ritem_783.xml..The last digit has be changed numbering it from 1 to 9. Commented Oct 27, 2016 at 3:06

4 Answers 4

3

Pure batch:

@echo off
setlocal disableDelayedExpansion
for /f "delims=" %%F in ('dir /b /a-d {*}_*') do (
  set "file=%%F"
  setlocal enableDelayedExpansion
  ren "!file!" "!file:*}_=!"
  endlocal
)

If you know that none of your filenames contain the ! character, then the code simplifies to:

@echo off
setlocal enableDelayedExpansion
for /f "delims=" %%F in ('dir /b /a-d {*}_*') do (
  set "file=%%F"
  ren "!file!" "!file:*}_=!"
)

Or you could use JREN.BAT - a regular expression file renaming batch utility. It is pure script (hybrid JScript/batch) that runs natively on any Windows machine from XP onward. You should have no problem using this on any site that allows you to create and use a batch file. Simply copy the code from the link into a file named JREN.BAT.

jren "^{.*?}_" ""

You must use CALL JREN if you put the command within a batch script.

JREN is a very powerful and handy renaming tool. Full documentation is available from the command line via jren /?, or jren /?? if you want paged output.

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

Comments

0

You want to use any regex renamer. I think you are in the wrong kind of forum for this question.

Here is one possible solution.

I tested around a bit using notepad++. Not every regex functions exactly the same, so proceed with caution.

Regex:

\{[^}]+\}_(Ritem_\d+.xml)

Replace With:

$1

Some explanation:

  • \{ and \} are normal curly brackets
  • [^}]+ is anything but a closing curly bracket, 1 or more times
  • (...) mark a group
  • \d+ decimals repeated one or more times.

Replacing uses only $1. You can use the matched groups when replacing. $0 is everything you matched. $1 is inside the first group brackets.

For more information on regex look up more options in a regex cheat sheet.

3 Comments

@johannes..i want this task to be done from cmd ,as i need to perform this at my workplace,where using other softwares are not allowed.
@rahul Talk to your employer, surely he wants you to be able to finish your task on time.
@johannes..yes ,but using cmd only.
0
ren *Ritem_???.xml Ritem_???.xml

EDIT:

Try this:

@echo off

setlocal enableDelayedExpansion
for %%# in ("{*}*") do (
  for /f "tokens=1,2,3,4 delims=_" %%a in ("%%#") do (
    ren "%%#" "%%b_%%c_%%d"
  ) 
)

9 Comments

@rahul - are there files with the same end digits?
No ..that was just as example..the file name is RstoreItem_1234_20160322(datetime when the file is created),
@npocmaka..This is one of the command i found.@ECHO OFF FOR %%F IN ("[] *") DO CALL :process "%%F" GOTO :EOF :process SET oldname=%1 SET "newname=%~nx1" SET "newname=%newname:] =%" RENAME %oldname% "%newname%"....which works fine when there are square brackets and a space.could you help me modify it???
Do the filenames contain curly brackets {} and is the RstoreItem_1234_20160322 a someting within the brackets?
Filename:{5gnf-kjg6-kjg8-tyr9}_RstoreItem_1234_20160322182705.xml
|
0

In this method the number of underscores after the "}" may be anyone:

@echo off
setlocal EnableDelayedExpansion

rem Process all files, split the name at the "}"
rem ie: "%%a" = "{5gnf-kjg6-kjg8-tyr9", "%%b" = "_RstoreItem_1234_20160322182705.xml"

for /F "tokens=1* delims=}" %%a in ('dir /B "{*}*"') do (

   rem Get the part after the "}", ie: "_RstoreItem_1234_20160322182705.xml"
   set "name=%%b"

   rem Do the rename, exclude the first char from the name, ie: "RstoreItem_1234_20160322182705.xml"
   ren "%%a}%%b" "!name:~1!"

)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.