Loop variables must be single character variables because of tokens=1,4,6-9
feature which would not be possible with loop variables with any name.
For example from CSV file input.csv
the first data column, the fourth data column and the data columns six to nine should be written into CSV file output.csv
.
@echo off
if not exist "input.csv" exit /B
if exist "output.csv" del "output.csv"
for /F "usebackq tokens=1,4,6-9 delims=," %%A in ("input.csv") do (
echo %%A,%%B,%%C,%%D,%%E,%%F>>"output.csv"
)
Note 1: This works only if there are no empty field values in each data row.
Also it would be very difficult for Windows command processor to distinguish between referencing a loop variable and an environment variable if loop variables could have any name.
You can always assign value of a loop variable to an environment variable and use on the other command lines within the loop the environment variable using delayed expansion.
@echo off
setlocal EnableExtensions EnableDelayedExpansion
for /R F:\foo\ %%# in (bar_*.txt) do (
set "FileName=%%#"
echo Current file is: !FileName!
)
endlocal
Note 2: This works only for file names not containing !
in name or path.
Environment variables are not case-sensitive, but loop variables are case-sensitive. And avoid using letters for loop variables which could be interpreted also as modifiers for example on using %~nI
, %~nxI
or something similar as this could be problematic.
BTW: Why not using COPY with option /S
or XCOPY or ROBOCOPY to copy recursively all files matching the wildcard pattern bar_*.txt
?