The flag 'delayed expansion' in the batch file is enabled. It starts with:
@echo off
setlocal EnableDelayedExpansion
The batch file intends to make a tree of all the '.c' files in the current folder and all of its subfolders. Here it is:
:: -------------------
:: Generate file tree
:: -------------------
@echo off
setlocal EnableDelayedExpansion
for /f %%a in ('copy /Z "%~f0" nul') do set "CR=%%a"
:: 1. Delete old file trees
:: -------------------------
for /r %%x in (*FileTree.txt) do (
REM echo."File %%x deleted"
del "%%x"
)
:: 2. Generate new file tree
:: -------------------------
break > localFileTree.txt
set mypath=
set localPath=
call :treeProcess
goto :eof
:treeProcess
setlocal
for %%f in (*.c) do (
set localPath=%mypath%%%f
echo.!localPath! >> %~dp0\localFileTree.txt
)
for /D %%d in (*) do (
set mypath=%mypath%%%d\
echo.!mypath!
cd %%d
call :treeProcess
cd ..
)
endlocal
exit /b
A variable can be referenced in several ways:
- Without anything:
mypath=...
- With leading double percent signs:
%%f
or%%d
- With percent signs at either side:
%mypath%
- With exclamation marks at either side:
!mypath!
I'm getting a bit confused. I know it has something to do with the 'delayed expansion' flag that is switched on. But how do these variable references actually differ from each other?