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.
1
Possible duplicate of Append to start of filename
– user193661 – 2015-11-29T12:56:02.9531@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