0

Here is my batch file :

    @echo off

    cd "D:/myFiles"

    for /r %%filename in (*) do (

    set f=%%filename

    echo %f%

    )

    pause

What I m expecting is,it will list all the files in myFiles directory.

But what I am getting is 'Echo is Off' for the same number of times as the number of files in directory 'myFiles'.

With

    echo %%filename

I can list filename but I have to do some more operation on filename and so to have to store it in a variable like as currently its stored in variable f.

Any help will be appreciated.

1
  • Search for delayed expansion as this is a question that is asked regularly. Commented Sep 17, 2014 at 11:57

1 Answer 1

1

You need setlocal enabledelayedexpansion to allow the re-evaluation of the variable you set inside the loop. variable expansion needs the !var! usage, rather than %var%. e.g.

setlocal enabledelayedexpansion
@echo off

for /r   %%f in (*) do (
    set n=%%f
    echo !n!
)
Sign up to request clarification or add additional context in comments.

2 Comments

But if I want to manipulate the variable n,then how it can be accomplished ?
Quite easily - but first you need to send everyone a crystal ball so that we can read your mind. Tell us how you want to manipulate the name, and we may be able to help.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.