Wednesday, April 26, 2017

windows - add "text" to end of multiple filenames




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

Answer



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




No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...