add "text" to end of multiple filenames

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

  • An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
  • for - Conditionally perform a command on several files.
  • parameters - A command line argument (or parameter) is any value passed into a batch script.

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.


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.