0

I'm trying to simply get the filenames from a directory into a loop and then process (with the eventual aim of then doing a substring on the filename), but can't get it to work.

I've googled plenty and it seems to me that what I'm doing should work, but I'm obviously missing something.

Here's my code:-

setlocal ENABLEDELAYEDEXPANSION
set files=*.*
FOR %%A IN ("%files%") DO (
  echo %%A
  Set MYVAR=%%A
  echo MYVAR=%MYVAR%
)
pause

The echo of the %%A works fine, but MYVAR is always empty. I had this working on 1 line earlier, before I added the brackets.

Please help. I've spent far too long on this already and am pulling my hair out.

3

1 Answer 1

0

Try using one of the two methods below.

Either:

@Echo Off
SetLocal EnableDelayedExpansion
Set "files=*.*"
For %%A In ("%files%") Do (
    Echo=%%A
    Set "MYVAR=%%A"
    Echo=MYVAR=!MYVAR!
)
Timeout -1

Or:

@Echo Off
Set "files=*.*"
For %%A In ("%files%") Do (
    Echo=%%A
    Set "MYVAR=%%A"
    Call Echo=MYVAR=%%MYVAR%%
)
Timeout -1
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. Using ! instead of % worked. I don't seem to need the Timeout -1 though. What's that for?
You used pause, timeout is effectively a similar command with a little more control, type Timeout /? for more information, you can of course use either!
oh, okay, got you. Thanks.

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.