This question is about the correct syntax for passing file name arguments to a subroutine in a batch file. Feels like this question should have been asked before, but I can't seem to find the right answer. Assume there are three text files in a folder called C:\Batch File Example
. The following code (partly borrowed from this answer) will output the file names:
@echo off
SETLOCAL Enableextensions
ECHO Line before FOR
FOR /R "C:\Batch File Example\" %%i in (*.txt) DO ECHO %%i
ECHO Line after FOR & PAUSE>NUL
Output:
Line before FOR
C:\Batch File Example\File1.txt
C:\Batch File Example\FIle2.txt
C:\Batch File Example\File3.txt
Line after FOR
Now, I want to produce the same output by using a subroutine instead, like this:
@echo off
SETLOCAL Enableextensions
ECHO Line before FOR
FOR /R "C:\Batch File Example\" %%i in (*.txt) DO CALL :doecho %%i
ECHO Line after FOR & PAUSE>NUL
GOTO :EOF
:doecho
SET VAR=%1
ECHO VAR is %VAR%
EXIT /b
But this gives the following output, where the result is truncated:
Line before FOR
VAR is C:\Batch
VAR is C:\Batch
VAR is C:\Batch
Line after FOR
The result above suggests that whitespace is treated as a delimiter and that %1
only contains C:\Batch
, so I tried using the following for loop instead, with the /F
flag and a comma delimiter to suppress the whitespace:
FOR /F "delims=," %%i IN ("C:\Batch File Example\*.txt") DO CALL :doecho %%i
However, this also returns the truncated result, with the only difference that there is only one iteration instead of three.
I've tried using enhanced variable substitionts like %~I
instead of %%i
and %1
, but I'm stuck. So what am I missing?