To use variables within a For command most effectively, be sure to take the following steps:

  • Use tabs or spaces to indent your code.
  • As with any other programming language, be sure to include closing parentheses for all opening parentheses.
  • Use Rem to precede any comments. Label-type comments such as "::" might cause the script to fail.
  • Use different iterator variables (e.g., %%i, %%j, %%k) in your For commands. Using the same iterator variable in each For command might introduce errors.
  • Clear any environment variables before using them (e.g., Set response=).

If you have problems debugging your code, use Call and Goto commands to break the code into separate code sections to be sure the code is working correctly, then migrate back to the new format. If you're used to running your script with Echo Off to help debug problems, you might find the screen output of nested For commands a bit cryptic. To get started with this new technique, you can take an existing script that you've already debugged and convert it to use the Setlocal ENABLEDELAYEDEXPANSION command and nested For commands.