Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

From the current directory %CD%, I need to go inside a subfolder and execute a subrutineA, then go back to the %CD% directory and go to the next subfolder and execute the same subrutineA, this process need to be done as many time as folders available.

The problem that I have is that I dont know how to define in a batch script the name of the subdirectory since this name it´s variable, and the amount of folders it´s also variable.

Can anyone help me?

share|improve this question
up vote 0 down vote accepted

How’s this for a start:

@echo off
for /F "delims=" %%D in ('dir/b/ad') do (
    cd "%%D"
    Do what you want to do.
    cd ..
)

share|improve this answer
    
Great, it help me a lot :D – frantika Feb 22 '13 at 22:45

When you do not know what directory to return to and do not want to keep track of it, use pushd and popd.

@echo off
for /F "delims=" %%D in ('dir/b/ad') do (
    pushd "%%D"
    Do what you want to do.
    popd
)
share|improve this answer

You must log in to answer this question.

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