Page 1 of 1

For do if exist loop

Posted: 27 Jul 2013 10:46
by Beach Boy
Could someone please assist with the correct syntax for this? It errors out after seeing the second file in the string if it doesn't exist.

Thank you,

for /f "tokens=*" %%X in (hal.dll halacpi.dll halmacpi.dll kernel32.dll ntdll.dll ntoskrnl.exe win32k.sys winload.exe winsrv.dll) do (if exist %%X (
echo %%X
if not exist %%X (
echo %%X does not exist.
) else (
Copy %%X C:\BOOTFILES\
)
)
)

Re: For do if exist loop

Posted: 27 Jul 2013 14:13
by aGerman
You should use a simple FOR loop instead of FOR /F.

Code: Select all

@echo off &setlocal

if not exist C:\BOOTFILES\ md C:\BOOTFILES
pushd "%SystemRoot%\system32"
for %%X in (
  hal.dll
  halacpi.dll
  halmacpi.dll
  kernel32.dll
  ntdll.dll
  ntoskrnl.exe
  win32k.sys
  winload.exe
  winsrv.dll
) do (
  if exist %%X (
    echo %%X
    copy %%X C:\BOOTFILES\
  ) else (
    echo %%X does not exist.
  )
)
popd

pause

Regards
aGerman

Re: For do if exist loop

Posted: 27 Jul 2013 15:12
by Beach Boy
Thank you greatly aGerman. That works!