How to add a prefix to all files and folders in a folder? (windows)
The following command only changes the name of the files but not the folders.
for %a in (*) do ren "%a" "00_%a"
Notes:
- Using
for
as above is not advised. - There is a possibility that files can be renamed multiple times.
- See below for the reason why.
Use the following in a cmd
shell:
for /f "tokens=*" %a in ('dir /b') do ren "%a" "00_%a"
In a batch file (replace %
with %%
):
for /f "tokens=*" %%a in ('dir /b') do ren "%%a" "00_%%a"
Note:
It is critical that you use
FOR /F
and not the simpleFOR
.The
FOR /F
gathers the entire result of theDIR
command before it begins iterating, whereas the simpleFOR
begins iterating after the internal buffer is full, which adds a risk of renaming the same file multiple times.
as advised by dbenham in his answer to add "text" to end of multiple filenames:
Further Reading
- An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
- dir - Display a list of files and subfolders.
- for /f - Loop command against the results of another command.