17

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

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

Ƭᴇcʜιᴇ007
  • 111,883
  • 19
  • 201
  • 268
MatMis
  • 285
  • 1
  • 2
  • 7

2 Answers2

19

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

DavidPostill
  • 153,128
  • 77
  • 353
  • 394
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.
Ƭᴇcʜιᴇ007
  • 111,883
  • 19
  • 201
  • 268
  • `ren` does work on folders. The limitation is that "you cannot specify a different drive or path for the Target. – DavidPostill Jan 18 '16 at 17:40
  • @DavidPostill Yeah it was an old habit creeping in, I changed it back to `Ren`. – Ƭᴇcʜιᴇ007 Jan 18 '16 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 '16 at 17:42