Rename multiple files in cmd
If I have multiple files in a directory and want to append something to their filenames, but not to the extension, how would I do this?
I have tried the following, with test files file1.txt
and file2.txt
:
ren *.txt *1.1.txt
This renames the files to file1.1.txt
and file2.txt1.1.txt
I want the files to be file1 1.1.txt
and file2 1.1.txt
Will this be possible from cmd or do I need to have a bat file to do this? What about PowerShell?
Solution 1:
Make sure that there are more ?
than there are characters in the longest name:
ren *.txt "???????????????????????????? 1.1.txt"
See How does the Windows RENAME command interpret wildcards? for more info.
New Solution - 2014/12/01
For those who like regular expressions, there is JREN.BAT - a hybrid JScript/batch command line utility that will run on any version of Windows from XP forward.
jren "^.*(?=\.)" "$& 1.1" /fm "*.txt"
or
jren "^(.*)(\.txt)$" "$1 1.1$2" /i
Solution 2:
for /f "delims=" %%i in ('dir /b /a-d *.txt') do ren "%%~i" "%%~ni 1.1%%~xi"
If you use the simple for
loop without the /f
parameter, already renamed files will be again renamed.
Solution 3:
Step 1:
Select all files (ctrl + A)
Step 2 :
Then choose rename option
Step 3:
Choose your filename... for ex: myfile
it automatically rename to myfile (01),myfile (02),,.....
If you want to replace spaces & bracket.. continue step 4
Step 4:
Open Windows Powershell from your current folder
Step 5:
For replace empty space to underscore (_)
dir | rename-item -NewName {$_.name -replace [Regex]::Escape(" "),"_"}
Step 6:
For replace open bracket
dir | rename-item -NewName {$_.name -replace [Regex]::Escape("("),""}
For replace close bracket
dir | rename-item -NewName {$_.name -replace [Regex]::Escape(")"),""}
Solution 4:
The below command would do the job.
forfiles /M *.txt /C "cmd /c rename @file \"@fname 1.1.txt\""
source: Rename file extensions in bulk