File not found
@for /f "tokens=*" %%a in ('dir /b /ad "C:\Progra~1\Apache"') do @...
The error that you see when you run this command appears for standard error output. But this is only a warning printed on the console. When this happens, the iteration body will not be evaluated, and the algorithm based on this "for / dir" instruction is actually right.
Now, if you want to get rid of this ugly error message, you need to add the following to your script in order to redirect the standard error to a null device:
2>NUL
so, for example, in a batch file with the corresponding escape character:
:: echo list of files @for /f "tokens=*" %%a in ('dir /b /ad "%srcPath%" 2^>NUL') do @echo(%srcPath%\%%a
source share