18

The following command only changes the name of the files but not the folders.

for %a in (*) do ren "%a" "00_%a"

1
  • Do you absolutely have to use a batch file? Can you not use a program like Rename Master to do this? Jan 18, 2016 at 17:34

2 Answers 2

20

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 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.

as advised by dbenham in his answer to add "text" to end of multiple filenames:


Further Reading

0

To perform this For loop on folders (directories) instead of files, simply include the /D switch.

for /D %a in (*) do ren "%a" "00_%a"

From for /?:

FOR /D %variable IN (set) DO command [command-parameters]

If set contains wildcards, then specifies to match against directory
names instead of file names.
3
  • ren does work on folders. The limitation is that "you cannot specify a different drive or path for the Target.
    – DavidPostill
    Jan 18, 2016 at 17:40
  • @DavidPostill Yeah it was an old habit creeping in, I changed it back to Ren. Jan 18, 2016 at 17:40
  • You can do both files and folders in one command (see my answer). And I suspect for /d may also have the problem of trying to process values multiple times (also see my answer).
    – DavidPostill
    Jan 18, 2016 at 17:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .