Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: if exist file A or file B  (Read 3732 times)

0 Members and 1 Guest are viewing this topic.

Blisk

    Topic Starter


    Intermediate

    Thanked: 1
    • Experience: Familiar
    • OS: Windows 7
    if exist file A or file B
    « on: December 24, 2018, 03:45:40 AM »
    I am trying to simplfy script so instead of cheking twice
    if exist d:\myfolder\test\ (goto ZA) else (goto YA)
    :YA
    if exist d:\myfolder\testnew\ (goto ZA) else (goto UB)
    :UB

    is it possible to have something like this

    if exist "d:\myfolder\test\" OR "d:\myfolder\testnew\" (goto ZA) else (goto YA)

    Squashman



      Specialist
    • Thanked: 134
    • Experience: Experienced
    • OS: Other
    Re: if exist file A or file B
    « Reply #1 on: December 26, 2018, 12:03:12 PM »
    When you read the help file for the IF command, did you see an OR option?

    Salmon Trout

    • Guest
    Re: if exist file A or file B
    « Reply #2 on: December 26, 2018, 01:07:02 PM »
    Why don't you just do this? (label :UB doesn't even need to exist)

    if exist d:\myfolder\test\ goto ZA
    if exist d:\myfolder\testnew\ goto ZA
    :UB

    Blisk

      Topic Starter


      Intermediate

      Thanked: 1
      • Experience: Familiar
      • OS: Windows 7
      Re: if exist file A or file B
      « Reply #3 on: December 27, 2018, 12:04:13 PM »
      Why don't you just do this? (label :UB doesn't even need to exist)

      if exist d:\myfolder\test\ goto ZA
      if exist d:\myfolder\testnew\ goto ZA
      :UB

      I did that but I thought maybe there is OR which will simplify this

      Salmon Trout

      • Guest
      Re: if exist file A or file B
      « Reply #4 on: December 27, 2018, 12:57:57 PM »
      You don't get a logical OR with the batch IF test.

      You don't get a logical AND either, but you can mimic this by using two IF tests on one line like so

      Mimic IF exist file1 AND if exist file2 THEN do_something (which doesn't work in batch):

      IF exist file1 if exist file2 do_something

      You see how the second IF will only be executed "if" the first one evaluates as true?

      Well, you can use De Morgan's laws to turn a chain of IF NOTs into the equivalent of OR tests

      Mimic IF exist file1 OR if exist file2 THEN do_something (which doesn't work in batch):

      set flag=true
      IF NOT exist file1 IF NOT exist file2 set flag=false
      if %flag%=="true" do_something

      If file1 exists, the first IF NOT test will fail, and the second one will not be executed. the variable %flag% will not be changed, and will stay as "true". This satisfies the OR condition because file1 exists.

      If file1 does not exist, the first IF NOT test will succeed, and the second one will be executed. If file2 exists, the second IF NOT test will fail, and the %flag% will not be changed, and remains as "true". This satisfies the OR condition because file2 exists.

      if file1 does not exist, the first IF test will succeed, and the second IF test will then be executed. If file2 does not exist either, the second test will succeed, and the variable %flag% be changed to "false". Thus the OR test fails because neither file exists.

      The problem is that the above way of doing it uses 3 lines whereas this only uses 2 lines:

      if exist file1 do_something
      if exist file2 do_ something

      However, you can chain a lot of IF NOTS in one line, where the savings of lines will be greater.




      « Last Edit: December 27, 2018, 01:14:11 PM by Salmon Trout »

      Salmon Trout

      • Guest
      Re: if exist file A or file B
      « Reply #5 on: December 27, 2018, 01:32:02 PM »
      Test if a number is one of these: 4, 6, 9, 47, 1234 or 9999, in 3 lines instead of 6:

      @echo off
      set /p "x=Input a number "
      set flag=true
      if not "%x%"=="4" if not "%x%"=="6" if not "%x%"=="9" if not "%x%"=="47" if not "%x%"=="1234" if not "%x%"=="9999" set flag=false
      if "%flag%"=="true" (echo PASS) else (echo FAIL)

      « Last Edit: December 27, 2018, 02:30:42 PM by Salmon Trout »

      Salmon Trout

      • Guest
      Re: if exist file A or file B
      « Reply #6 on: December 27, 2018, 02:31:21 PM »
      For that demo, you don't need this, so I took it out:

      Quote
      setlocal enabledelayedexpansion

      Salmon Trout

      • Guest
      Re: if exist file A or file B
      « Reply #7 on: December 28, 2018, 12:27:52 AM »
      Note: The maximum length of any command line (or variable) within CMD is 8191 characters. A single line should not exceed 8192 characters, even after the expansion of %var% or !var! expressions, else you got an error.