0
set /a n=1
for %%i in (*.mkv) do (
    for %%j in ("A:\Downloads\AoD\AoD (%n%)\*.*") do (
        mkvpropedit "%%i" --add-attachment "%%j"
        )
    set /a n+=1
    )

I want to write a script which attaches the conent of the n-th folder in "A:\Downloads\AoD\AoD (%n%)\*.*" to the n-th .mkv. The above script didn't work because apparently the entire FOR loop is evaluated before execution. After stumbling across this question, I rewrote my script like this

setlocal enabledelayedexpansion
set /a n=1
for %%i in (*.mkv) do (
    for %%j in ("A:\Downloads\AoD\AoD (!n!)\*.*") do (
        mkvpropedit "%%i" --add-attachment "%%j"
        )
    set /a n+=1
    )
endlocal

but it somehow still doesn't work. n increases but apparently !n! isn't interpreted as a variable but rather as the string !n! because this is what cmd tells me

B:\AoD try>(
for %j in ("A:\Downloads\AoD\AoD (!n!)\*.*") do (mkvpropedit "AoD (1).mkv" --add-attachment "%j" )
 set /a n+=1
)
2
B:\AoD try>(
for %j in ("A:\Downloads\AoD\AoD (!n!)\*.*") do (mkvpropedit "AoD (2).mkv" --add-attachment "%j" )
 set /a n+=1
)
3
B:\AoD try>endlocal

This output is from me using the script by pasting it directly into the Command Prompt with only single %s.

Compo
  • 36,585
  • 5
  • 27
  • 39
  • Open a Command Prompt window, type `for /?`, press the `[ENTER]` key and read the output. Within the first handful of lines you should see that you've made an error, using `%i` and `%j`. – Compo Aug 04 '20 at 00:16
  • I don't see the error in using `%i` and `%j`. Is it that I should use two `%`? As far as I understood, I only need to do that when I run a script thorugh a batch file. When I paste the script into the Command Prompt directly, a single `%` seems to suffice. If it's the missing `/F`, I don't understand it either because it works without the `/F` if I fix my `n` manually so that I don't even need to use `EnableDelayedExpansion`. –  Aug 04 '20 at 00:28
  • When you said "I rewrote my script like this", directly before your posted code block, I assumed along with the batch-file tag, the Windows cmd script, was a batch file! Are you telling us therefore that you're using confusing terms and an invalid question tag? – Compo Aug 04 '20 at 00:32
  • I created a batch-file but while trying to make it work, I copy pasted it into the Command Prompt instead of using the batch-file. Changing it to double `%` and starting it from the batch-file somehow made it work. I should have done that from the beginning. Sorry for the confusion. My problem is solved now. –  Aug 04 '20 at 00:52
  • 1
    I've removed your [[tag:batch-file]] tag, as it is clear that your question did not involve one at all. _(You could have copied and pasted the content from any text file, with no, or any, extension)_. I have also removed your resultant solution from the question too. The question area is not where you include solutions to questions, we have an answer area for such things! – Compo Aug 04 '20 at 02:12
  • 2
    @Limechime `setlocal` only works in batch files, not at the cmd prompt. To use delayed expansion at the command line, open a cmd prompt with `cmd /V:ON`. – dxiv Aug 04 '20 at 02:56

0 Answers0