Solved Windows batch IF & ELSE... With a higher directory first

February 17, 2013 at 11:59:01
Specs: Windows 8, 16GB
This is the folder/file structure:

dirfolder test and the subfolder with name testing
subfolder contains hello.txt
DIRfolder contains text1.txt and text2.txt

Problem is that the batch finds to hello.txt but if i delete that one and wants to continue to text1.txt, thats the problem it can't find back to dir folder with text1 and text 2.txt
question 1 : how do i let the script go to dir after it was in subfolder and didnt find hello?
question 2 : How do i let the script continue to text2 if text1 fails?
This is what i got:

[CODE]CD ..\test\testing
IF EXIST hello.txt. (
start hello.txt.
) ELSE text1.txt. (
start text1.txt.
) ELSE text2.txt (
start text2.txt.
)
pause[/CODE]


Would be grateful if you helped me guys!


See More: Windows batch IF & ELSE... With a higher directory first

Report •


#1
February 17, 2013 at 12:57:24
✔ Best Answer
Here the solution; notice the importance to use indentation (two spaces) to master a unfriendly code.

@echo off
cd ..\test\testing
if exist hello.txt (
  start hello.txt
) else (
  if exist ..\test1.txt (
    start ..\test1.txt
  ) else (
    if exist ..\test2.txt (
      start ..\test2.txt
    ) else (
      echo.No file found
    )
  )
)
pause


Report •

#2
February 17, 2013 at 17:46:15
Might also nail it with this:
pushd \test\testing
dir hello.txt && start hello.txt || dir ..\test1.txt && start test1.txt || dir ..\test2.txt && start test2.txt || echo NO FILE TO START
popd

or:
for /f "tokens=*" %%a in ('dir /b /s \test\hello.txt \test\test1.txt \test\test2.txt') do (
start %%a 2>nul
goto :eof)
echo NO FILE


Report •

Related Solutions


Ask Question