Why does this not work?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Joddis
Posts: 1
Joined: 12 Feb 2018 19:09

Why does this not work?

#1 Post by Joddis » 13 Feb 2018 14:41

FOR %%A IN (*.*) DO (
echo %%A
set myfilename=%%A
echo %%myfilename
)

The "set myfilename=%%A" does not work for each instance of the for loop, just one time .. so, the echo will only output the first filename and I don't understand why?

Thanks in advance!

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Why does this not work?

#2 Post by Squashman » 13 Feb 2018 22:23

Environmental variables are expanded by putting percent symbols on each side of the variable name. But since you are inside a parenthesised code block you need to use delayed expansion for the variables to output correctly. This is done by using exclamation points.

@echo off
Setlocal enabledelayedexpansion
FOR %%A IN (*.*) DO (
echo %%A
set myfilename=%%A
echo !myfilename!
)

Post Reply