add "text" to end of multiple filenames

5

Using the code below I add text to end of file names which are:

1.mp3
2.mp3

and should be:

1text.mp3
2text.mp3

but it changed to:

1.mp3text
2.mp3text

Code:

@ECHO ON

FOR %%A IN ("C:\Users\user123\Desktop\mp3\*.mp2") DO (
    CALL :RenameFiles "%%~A" "%%~NXA"
)
GOTO EOF

:RenameFiles

SET fname=%~2
SET renname=%fname=%
REN "%~1" "%renname%text%"
GOTO E

user123

Posted 2015-11-29T12:32:06.993

Reputation: 349

1

Possible duplicate of Append to start of filename

– user193661 – 2015-11-29T12:56:02.953

1@user193661 The dupe is adding to the start of the filename. The question is asking to add at the end of the filename. – DavidPostill – 2015-11-29T13:01:14.547

Seems like a trivial difference – user193661 – 2015-11-29T13:01:45.647

1@user193661 If you look at the answer to the dupe you will see that its not a trivial difference. – DavidPostill – 2015-11-29T13:06:24.230

Okay, I'll look. – user193661 – 2015-11-29T13:07:49.140

Answers

3

How do I add "text" to the end of multiple filenames.

Before: 1.mp3 2.mp3

After: 1text.mp3 2text.mp3

There are multiple errors in your batch file, for example:

  • *.mp2 should be *.mp3
  • goto E - the label :E is missing.
  • you dont need to call anything.

Simple working batch file

rem @echo off
setlocal
setlocal enabledelayedexpansion
for %%a in ("C:\Users\user123\Desktop\mp3\*.mp3") do (
    copy "%%a" "%%~dpnatext%%~xa"
    del "%%a" 
)
endlocal

Notes:

  • The batch file will work with any length filename, so for example, 12345.mp3 will be rename to 12345text.mp3

cmd shell solution

You don't need to use a batch file. You can just use:

cd C:\Users\user123\Desktop\mp3\
ren ?.mp3 ?text.mp3

Or:

ren *.mp3 ?text.mp3

Note that the following does not work:

ren *.mp3 *text.mp3

You will get filenames like 1.mp3text.mp3 with that command.

Example:

F:\test>dir *.mp3
 Volume in drive F is Expansion
 Volume Serial Number is 3656-BB63

 Directory of F:\test

29/11/2015  12:52                 0 1.mp3
29/11/2015  12:52                 0 2.mp3
               2 File(s)              0 bytes
               0 Dir(s)  1,777,665,769,472 bytes free

F:\test>ren ?.mp3 ?text.mp3

F:\test>dir *.mp3
 Volume in drive F is Expansion
 Volume Serial Number is 3656-BB63

 Directory of F:\test

29/11/2015  12:52                 0 1text.mp3
29/11/2015  12:52                 0 2text.mp3
               2 File(s)              0 bytes
               0 Dir(s)  1,777,665,769,472 bytes free

Further Reading

DavidPostill

Posted 2015-11-29T12:32:06.993

Reputation: 118 938

instead of ? I should use *. I try ? not work but * works. – user123 – 2015-11-29T13:20:45.783

@user123 <shrug> I've tested ? and it works. ren *.mp3 ?text.mp3 also works. ren *.mp3 *text.mp3 doesn't work. You get names like 1.mp3text.mp3 – DavidPostill – 2015-11-29T13:27:52.817

@user123 I've also modified the answer to give you a much simpler working batch file. – DavidPostill – 2015-11-29T13:28:52.447

1Your batch file is broken. The REN target cannot include drive or path. It must be name and extension only. – dbenham – 2015-11-29T15:28:09.483

@dbenham Well spotted, I will take a look a little later. – DavidPostill – 2015-11-29T15:30:33.873

@dbenham fixed. – DavidPostill – 2015-11-29T15:50:38.423

you are right but for the name that has more than one character I should set more???? instead of ?. – user123 – 2015-11-29T15:51:44.870

@user123 If it has more than one character use the batch file. – DavidPostill – 2015-11-29T15:52:49.270

cmd works fine by adding more ???. both method works fine. thanks for your time. – user123 – 2015-11-29T15:57:11.547

2

Use of the simple FOR runs the risk of renaming a file more than once - not good :-( Better to use FOR /F with DIR /B. Also, multiple ? work well in this case for using a simple REN with longer names. See my answer for more info.

– dbenham – 2015-11-29T16:10:08.327

6

You can add text to the end of any file name (prior to the first dot, which is usually the beginning of the extension) by using a series of ? that is at least as long as the original name.

ren "C:\Users\user123\Desktop\mp3\*.mp3" ??????????????????????text.*

The ? greedily matches and preserves any character except ., and is free to match nothing if it runs out of characters. Your text is then appended, and then .* matches and preserves the dot and the remainder of the name.

You can modify the source file mask as you see fit to match whatever files you want. The source mask is entirely independent from the rename mask.

See How does the Windows RENAME command interpret wildcards? for more information.

If some of your files contain more than one . in the name, then you cannot use the simple REN command alone. One option is to use a batch script:

@echo off
pushd "C:\Users\user123\Desktop\mp3\*.mp3"
for /f "delims= eol=:" %%F in ('dir /b /a-d *.mp3') do ren "%%F" "%%~nFtext%%~xF"
popd

Note that it is critical that you use FOR /F and not the simple FOR. The FOR /F gathers the entire result of the DIR command before it begins iterating, whereas the simple FOR begins iterating after the internal buffer is full, which adds a risk of renaming the same file multiple times.

I use EOL=: to guard against the remote possibility that a file name begins with ;. The default EOL is ;, which ignores lines that begin with ;. File names cannot contain :, so it is a safe character to use for the EOL option.

One minor disadvantage of FOR /F with DIR /B is it does not preserve the path information, which is why I used PUSHD at the beginning. Without PUSHD, you could include the path info in the DIR command, but then you would also need to include the path in the REN source mask. The following one liner works on the command line (no batch script):

for /f "delims= eol=:" %F in ('dir /b /a-d "C:\Users\user123\Desktop\mp3\*.mp3"') do @ren "C:\Users\user123\Desktop\mp3\%F" "%~nFtext%~xF"

An alternative is to use the FINDSTR command to list the files, which conveniently preserves the path information:

for /f "delims=" %F in ('findstr /m "^" "C:\Users\user123\Desktop\mp3\*.mp3"') do @ren "%F" "%~nFtext%~xF"

Since each file listed starts with the drive letter, it is safe to use the default EOL=;.

One final alternative - you could use my JREN.BAT regular expression rename utility. It is pure script (hybrid JScript/batch) that runs natively on any Windows machine from XP onward.

jren "(.*)(\.mp3)$" "$1text$2" /i /p "C:\Users\user123\Desktop\mp3"

Since JREN is a batch script, you would need to use CALL JREN if you use the command within another batch script.

dbenham

Posted 2015-11-29T12:32:06.993

Reputation: 9 212

Hmm. You commented on my answer yesterday "The REN target cannot include drive or path. It must be name and extension only". However in your answer you include a one-liner containing do @ren "%F" "C:\Users\user123\Desktop\mp3\%~nFtext%~xF". Looks like you made the same mistake ;) – DavidPostill – 2015-11-30T18:27:59.610

@DavidPostill - Ouch. LOL. My intent was correct, but I pasted the path in front of the wrong parameter. All fixed now. Thanks. – dbenham – 2015-11-30T19:49:46.847

No problem. It's good we spot each other's mistakes (I think I make more though, your batch knowledge is way more than mine). I do enjoy learning from you ;) – DavidPostill – 2015-11-30T19:55:14.753

1

PowerShell Rename-Item command with a regular expression in the search text would work in this situation.

Get-ChildItem -Filter '*mp3' -Recurse | Rename-Item -NewName {$_.name -replace '(.*)\.mp3','$1text.mp3'}

The (.*) is a regex group where the . matches any any character besides line break, and the * matches that zero or more times. The \. matches a period before mp3. Then in the replacement text, the $1 inserts the text that was matched in the first group (.*), followed by the rest of text you want to end with.
This works with variable length file names and file names that include spaces and underscores and other mix of characters.

wickstar

Posted 2015-11-29T12:32:06.993

Reputation: 11

0

Does it have to be done in a batch file? AdvancedRenamer is a free and very versatile program for renaming files or folders in batch. Easily solves your problem.

Joris Groosman

Posted 2015-11-29T12:32:06.993

Reputation: 554

Please read How do I recommend software for some tips as to how you should go about recommending software. You should provide more than just a link, for example some additional information about the software itself, and how it can be used to solve the problem in the question.

– DavidPostill – 2015-11-29T16:49:26.810