1

I am having trouble with a bit of code, I don't really know how to describe it but I can explain what doesn't work

FOR /D /r "%cd%\files\" %%G in ("*") DO (
  echo In folder: %%~nxG
  set /a count=1
  echo %%~fG

  For /R "%%~fG" %%B in ("*.mp3") do (
      call :subroutine "%%~nB"
  ) & echo. >>%archive%.txt

)

just if you want to know what the subroutine does:

:subroutine
 echo %count%:%1>>%archive%.txt
 echo %count%: %1
 set /a count+=1
 GOTO :eof

I figured out that it doesn't read the %%~fG inside the second for loop. Can someone please help me.

I am using SETLOCAL EnableDelayedExpansion

Thank you in advance.

0

2 Answers 2

1

Unfortunately you'll need another subroutine as for options are parsed before outer for tokens. Check the following example:

@echo off

echo :::ATTEMPT 1:::
for %%a in (z) do (
    rem the expected delimiter is z and result should be ++::%%a
    for /f "delims=%%a tokens=1,2" %%A in  ("++z%%%%az--") do echo %%A::%%B
)


echo :::ATTEMPT 2:::
for %%a in (z) do (
    call :subr "%%~a"
)

exit /b

:subr
rem the expected delimiter is z and result should be ++::%%a
for /f "delims=%~1 tokens=1,2" %%A in  ("++z%%%%az--") do echo %%A::%%B

the output is:

:::ATTEMPT 1:::

++z::zz--

:::ATTEMPT 2:::

++::%%a

As you can see in the first attempt the %%a symbols are taken as delimiters. But subroutine arguments are parsed imminently so they can be used. To make your code work you can try with:

FOR /D /r "%cd%\files\" %%G in ("*") DO (
  echo In folder: %%~nxG
  set /a count=1
  echo %%~fG

 call ::innerFor "%%~fG"

)
...
exit /b %errorlevel%
:innerFor
  For /R "%~1" %%B in ("*.mp3") do (
      call :subroutine "%%~nB"
  ) & echo. >>%archive%.txt
Sign up to request clarification or add additional context in comments.

Comments

1
For /R "%%~fG" %%B in ("*.mp3") do (

Sadly, for/r can't be run with a variable as the dirname.

I'd suggest

call :anothersubroutine "%%~fG"

and :anothersubroutine For /R "%~1" %%B in ("*.mp3") do (

  • but I've not tried it. Perhaps you'd need to set %%~fG into a variable and use %var% (not tried that either...)

Comments

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.