Batch: Help on If else

December 17, 2009 at 23:00:16
Specs: Windows XP, 2GB

I have the below code, where it will do a simple move files.
i need help to extend it with these condition:
If the files *.brn exist, then do the cp and mv. if not exist, echo "No files".
This is to reduce the cpu time wastage on checking, trying to move and copy when 0 files, logging time. The batch files need to be run every 2 mins.
I have seen if else in the other forums, but im having problem in coding one.
echo "----------------------------START------------------------------------------">>move_brn.log 2>&1
echo &date /T >>move_brn.log 2>&1
echo &time /T >>move_brn.log 2>&1
call :Logit>>move_brn.log 2>&1
echo "----------------------------CLOSE------------------------------------------">>move_brn.log 2>&1
exit /b 0
cd C:\)brn
dir *.brn
copy *.brn X:
move *.brn done
dir *.brn

See More: Batch: Help on If else

Report •

December 18, 2009 at 03:17:40
But this is rather simple, no ?

cd /d "C:\brn"
if exist *.brn goto brn_ok

echo "No File"
goto brn_end

dir *.brn
copy *.brn X:
move *.brn done
dir *.brn


Report •

December 18, 2009 at 03:20:11
cd C:\brn
dir *.brn
if exist *.brn (
  copy *.brn X:
  move *.brn done
) else (
  echo.NO Files Found
dir *.brn

Report •

December 18, 2009 at 03:31:27
IVO's solution is actually better, just because of IF and ELSE usage, which should always be used, instead of GOTO

Report •

Related Solutions

December 18, 2009 at 05:37:04

Thank you, I learned programmming far before Structured (Gotoless) Programming) was developed, but since that evolution in coding style I try to avoid goto statements to develop a more readable and efficient code.

Someone can't believe but batch scripts too can be coded according to structured programming's principles.

Report •

December 21, 2009 at 16:01:34
Hi IVO,tvc,

Thanks. I didnt know the existance of "if exist" command. I used the "if else" batch script of IVO and its working.

Thank you

Report •

December 22, 2009 at 12:55:37
Windows batching is actually reasonably powerful, since XP. You can do a lot with FOR, SET, IF, and some more.

Check out all features, by using:

for /?
set /?
if /?
findstr /?

Report •

Ask Question