It looks like the %comspec% variable has been nulled or changed and it can't find cmd.exe
Test this to see the error, and remove line 2 for it to work fine, on a working machine.
@echo off
set comspec=c:\aaa
for /f "delims=;" %%g in ('dir') do echo %%g
pause
Something that is very interesting is that the path to cmd.exe is cached.
The two scripts below are the same with only the order of the commands changed
This batch file will fail with both for commands
@echo off
set comspec=c:\aaa
for /f "delims=;" %%g in ('dir') do echo %%g
pause
set "comspec=%windir%\System32\cmd.exe"
for /f "delims=;" %%g in ('dir') do echo %%g
pause
and this batch file will work with both for commands.
@echo off
set "comspec=%windir%\System32\cmd.exe"
for /f "delims=;" %%g in ('dir') do echo %%g
pause
set comspec=c:\aaa
for /f "delims=;" %%g in ('dir') do echo %%g
pause
dirneeds to go between backticks. See the output ofhelp for"usebackq". Otherwise single quotes are fine.