user3189612
user3189612

Reputation: 1

Variable in for loop in a batch file

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.

Upvotes: 0

Views: 69

Answers (1)

Stephen Connolly
Stephen Connolly

Reputation: 1639

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!
)

Upvotes: 1

Related Questions