1

i am in the process of renaming multiple files from _2. to . I want to remove the "_2" suffix

My first workaround is trying

   ren *_2.* *.*

but this does not work. Help is apperciated.

2
  • About how many files do you need to rename? Are they all the sale width in terms of number of characters?
    – Jim
    Feb 23, 2014 at 7:58
  • About 1000 files and they have different number of characters
    – M. A.
    Feb 23, 2014 at 8:02

6 Answers 6

2

Solution

You can use this batch script:

@echo off
setlocal enabledelayedexpansion

for /r %%G in (*_2.*) do (
set new=%%~nG
set new=!new:~0,-2!
ren "%%~G" "!new!%%~xG"
)

How it works

  1. List all files matching *_2.*.
  2. Remove the _2 suffix from each file name (%%~nG) by stripping the last 2 characters.
  3. Append the original extension (%%~xG) to the new name.
  4. Rename the file.

Test cases

__2.bin --> _.bin
abc_def_2.txt --> abc_def.txt
2014_02_23_2.log --> 2014_02_23.log

Further reading

0
1

You could try this as a little .cmd :

@echo off
setlocal enabledelayedexpansion

for %%a in (*_2.*) do (
set fnm=%%a
echo ren !fnm! !fnm:_2.=.!
)

which seems to do what you expect (remove the echo if satisfied by test run).

I just see a possible issue if you have several _2. in the filename

EDIT : replaced incomplete proposal as per suggestion

3
  • Replacing a substring can give unwanted side-effects. Let's say you have a file called 2014_02_23_2.log. If you just remove all _2 by replacing it with an empty string, you would end up with a file called 2014_023.log. Not really what the OP wanted.
    – and31415
    Feb 23, 2014 at 11:42
  • Should I fix it, since anyway I am obviously "out-answered" with better batchlets ?
    – FredP
    Feb 23, 2014 at 11:48
  • The answer itself is not entirely wrong, but you should improve it by elaborating a little more, add some warning, and possibly provide a working example code.
    – and31415
    Feb 23, 2014 at 11:58
0

Here's how you could do it in PowerShell (it's a one-liner so be sure to scroll to the right):

gci PATH\TO\YOUR\FOLDER\ | ? {!$_.PSIsContainer -and $_.BaseName.EndsWith("_2")} | ren -NewName {("{0}{1}" -f $_.BaseName.Substring(0, $_.BaseName.Length-2), $_.Extension)}

gci is an alias of Get-ChildItem - enter the path to your folder here. If you have subfolders then add -Recurse after the path

? is an alias of Where-Object - here we loop through the items and check it is NOT a folder and its name ends with "_2" (excluding the file extension).

ren - Rename-Item - finally we rename the files that passed the Where-Object filter. Using Substring we just cut off the two last characters of the filename.

0

This should do it, under the assumption that the patterns is really _2.* and not _2*.* (nothing after digit 2) and there is only one underscore in the name as and31415 correctly pointed out - as it will remove anything from _ (underscore) up to extension

for %i in (*_2.*) do (@for /f "delims=_ tokens=1" %j in ("%~ni") do @echo ren "%i" "%j%~xi")

Please note:
- it's one line, as run directly from command line
- it now only echoes (prints) ren command to console. After you verified it would run correctly, remove @echo to have it actually rename files.

2
  • Suppose a file is called abc_def_2.txt. Then your code would rename it to abc.txt, which is not the expected result.
    – and31415
    Feb 23, 2014 at 11:34
  • @and31415 yes good catch - funny I just noticed this myself and was about to add a warning :-)
    – wmz
    Feb 23, 2014 at 11:37
0

Here's the one-liner for doing it with JP Software's Take Command, a command interpreter whose REN command supports regular expressions:

ren ::(.*)_2\.(.*) ::\1.\2

Further reading

  • JP Software. REN. Take Command / TCC Help.
-1

I do not know a really quick and dirty way of doing it.

But, how about piping a bare directory list to a TXT file. Load the file to a spreadsheet and create formula(s) to create the rename commands. Then you can copy the rename commands as values and paste it to a batch file that can be run to do the renames.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .