20

Assume I have this structure:

d:/
  -- /alpha/
  -- /beta/
  -- /gamma/
  -- /delta/

I'm trying to execute a batch file that goes through those folders (but not through sub-folders within them).

How do I get this result using a FOR LOOP (assuming I don't know the name and quantity of the folders):

ren alpha alpha1
ren beta beta1
ren gamma gamma1
ren delta delta1

1 Answer 1

40

This is all you should need -- directly at the command prompt:

for /D %i in (*) do rename "%i" "%i1"

or in a batch file (note the doubled %):

for /D %%i in (*) do rename "%%i" "%%i1"

The /D performs the command against the directory names, as per the help, which can be obtained using the command for /?

If Command Extensions are enabled, the following additional forms of the FOR command are supported:

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

If set contains wildcards, then specifies to match against directory
names instead of file names.
Sign up to request clarification or add additional context in comments.

6 Comments

for /D %%a in (*) do rename "%%a" "%%a1" <--- use this style in a batch file. The important changes are the doubled percent signs, and the quotes allow you to handle long path names.
Ah yes, enclosing the names in quotation marks is a good idea too, thanks for the heads-up @foxidrive.
This will run the command for the current directory! What changes to make to run it for any specific directory, say C:\New ?
Just put the path where the wildcards are, e.g. (c:\new\*)
@Neoryder, put them in parentheses ()
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.