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.
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.
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"
)
*_2.*
._2
suffix from each file name (%%~nG
) by stripping the last 2 characters.%%~xG
) to the new name.__2.bin --> _.bin
abc_def_2.txt --> abc_def.txt
2014_02_23_2.log --> 2014_02_23.log
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
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.
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.
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.
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
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.