[SOLVED] If one of two files exist then do something.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
PAB
Posts: 57
Joined: 12 Aug 2019 13:57

[SOLVED] If one of two files exist then do something.

#1 Post by PAB » 06 Oct 2019 06:02

Good afternoon,

I want to check if one of two files exists.

[1] If either one of two files exist I want the script to continue running.
[2] If neither of the two files exist then I want to put a message.
[3] If one of the files exists then I want to ignore the fact that the other one doesn't.

So something like this . . .

IF EXISTS "%userprofile%\Desktop\file1.exe" OR "%userprofile%\Desktop\file2.exe" (
THEN Continue running code
) ELSE (
echo The file doesn't exist, please run blah first)

Thanks in advance.
Last edited by PAB on 07 Nov 2019 09:29, edited 1 time in total.

aGerman
Expert
Posts: 3786
Joined: 22 Jan 2010 18:01
Location: Germany

Re: If one of two files exist then do something.

#2 Post by aGerman » 06 Oct 2019 07:00

Easiest method to work around logical OR is using a helper variable.

Code: Select all

set "oneExists="
if exist "%userprofile%\Desktop\file1.exe" set "oneExists=1"
if exist "%userprofile%\Desktop\file2.exe" set "oneExists=1"
if not defined oneExists (
  rem your message and exit
)
rem continue here
But you could also reverse the logic. The two IF statements in a row behave like logical AND.

Code: Select all

if not exist "%userprofile%\Desktop\file1.exe" if not exist "%userprofile%\Desktop\file2.exe" (
  rem your message and exit
)
rem continue here
Steffen

PAB
Posts: 57
Joined: 12 Aug 2019 13:57

Re: If one of two files exist then do something.

#3 Post by PAB » 06 Oct 2019 07:24

Brilliant, Thanks Steffen.

Just out of interest, which of the two codes is more suitable/best to use please?

aGerman
Expert
Posts: 3786
Joined: 22 Jan 2010 18:01
Location: Germany

Re: If one of two files exist then do something.

#4 Post by aGerman » 06 Oct 2019 07:54

Depends on the situation. In your case the second should be okay.

Steffen

PAB
Posts: 57
Joined: 12 Aug 2019 13:57

Re: If one of two files exist then do something.

#5 Post by PAB » 06 Oct 2019 08:03

Thanks Steffen.

I think my memory is going [it's my age]!

I should have included this in the original post, sorry!

I have a File3.exe as well that I want to check to see if it exists at the same time as checking for File1.exe and File2.exe.
If File3.exe doesn't exist then I want the same message as for File1.exe and File2.exe but if it does exist then carry on running the code.

Thanks in advance.

aGerman
Expert
Posts: 3786
Joined: 22 Jan 2010 18:01
Location: Germany

Re: If one of two files exist then do something.

#6 Post by aGerman » 06 Oct 2019 08:58

Don't know if I understood your logic. But I guess it should be one out of these two

Code: Select all

if not exist "%userprofile%\Desktop\file3.exe" (
  rem your message and exit
) else if not exist "%userprofile%\Desktop\file1.exe" if not exist "%userprofile%\Desktop\file2.exe" (
  rem your message and exit
)
rem continue here

Code: Select all

if not exist "%userprofile%\Desktop\file3.exe" (
  rem your message and exit
)
if not exist "%userprofile%\Desktop\file1.exe" if not exist "%userprofile%\Desktop\file2.exe" (
  rem your message and exit
)
rem continue here
Steffen

PAB
Posts: 57
Joined: 12 Aug 2019 13:57

Re: If one of two files exist then do something.

#7 Post by PAB » 06 Oct 2019 09:18

Hi Steffen,

The below code works OK but does them individually which is correct if one or the other files are downloaded!

What I would like is that if NO FILES exist then a message saying for example, NO files are downloaded.

Code: Select all

If NOT EXIST "%userprofile%\Desktop\File3.exe" (
echo.
echo YOU MUST DOWNLOAD File3 TO THE DESKTOP FIRST!
echo.
echo ^< Press ANY key to return to the Options ^> & Pause > NUL
CLS & Goto :Options
)

set "oneExists="
if exist "%userprofile%\Desktop\File1.exe" set "oneExists=1"
if exist "%userprofile%\Desktop\File2.exe" set "oneExists=1"
if not defined oneExists (
echo.
echo YOU MUST DOWNLOAD FILE1 OR FILE2 TO THE DESKTOP FIRST!
echo.
echo ^< Press ANY key to return to the Options ^> & Pause > NUL
CLS & Goto :Options
)
Thanks in advance.

aGerman
Expert
Posts: 3786
Joined: 22 Jan 2010 18:01
Location: Germany

Re: If one of two files exist then do something.

#8 Post by aGerman » 06 Oct 2019 09:42

You can use a bitset where you add powers of two for every found file.

Code: Select all

set "bitset=0"
if exist "%userprofile%\Desktop\File1.exe" set /a "bitset |= 1"
if exist "%userprofile%\Desktop\File2.exe" set /a "bitset |= 2"
if exist "%userprofile%\Desktop\File3.exe" set /a "bitset |= 4"
set /a "f1orf2=bitset & 3, f3=bitset & 4"
if %bitset%==0 (
  rem no exe files
)
if %f3%==0 (
  rem file 3 missing
)
if %f1orf2%==0 (
  rem both file 1 and file 2 missing
)
rem continue
Steffen

PAB
Posts: 57
Joined: 12 Aug 2019 13:57

Re: If one of two files exist then do something.

#9 Post by PAB » 06 Oct 2019 11:30

Thanks Steffen, that works great!

Can I just trouble you to explain this piece of code please?

Code: Select all

set /a "f1orf2=bitset & 3, f3=bitset & 4"
Thanks in advance.

aGerman
Expert
Posts: 3786
Joined: 22 Jan 2010 18:01
Location: Germany

Re: If one of two files exist then do something.

#10 Post by aGerman » 06 Oct 2019 12:01

I figured you would ask.
Background: https://en.wikipedia.org/wiki/Bitwise_operation
I'll refrain from explaining how AND and OR work. Use the Wikipedia link for reference.

It's working on bit-level which means to understand what I'm doing here you have to understand that computers work with zeros and ones only. Thus, numeric values are stored in their binary representation rather than in their decimal representation. Powers of two represent one bit at a certain position. Only show you the 3 least significant bits in this figure because that's what we are using. The remaining 29 bits are 0 anyway.

Code: Select all

dec   bin  represents
 1    001  file1
 2    010  file2
 4    100  file3
Now I set the bits using bitwise OR (|-operator).
E.g. file 1 found

Code: Select all

  000
| 001
_____
  001
Say file 2 is not present, so no update of the bitset. But file 3 exists:

Code: Select all

  001
| 100
_____
  101
To find out which files are found I use bitwise AND (&-operator)
For file 1 and 2 I use number 3 as bitmask. Its binary expression is 011.

Code: Select all

  101
& 011
_____
  001
If the result is not 0 at least one of the files 1 or 2 exists.

For file 3 I use number 4 as bitmask. Its binary expression is 100.

Code: Select all

  101
& 100
_____
  100
If the result is not 0 file 3 exists.
That's it.

Steffen

PAB
Posts: 57
Joined: 12 Aug 2019 13:57

Re: If one of two files exist then do something.

#11 Post by PAB » 06 Oct 2019 13:12

Wow Steffen, thank you so much for taking the time to explain that to me, it is very much appreciated.

Aacini
Expert
Posts: 1621
Joined: 06 Dec 2011 22:15
Location: México City, México

Re: If one of two files exist then do something.

#12 Post by Aacini » 07 Oct 2019 11:54

You don't need the auxiliary f3 variable; just ask for %bitset% less than 4 to know if File3.exe is missing. You may also do some simplifications:

Code: Select all

set /A bitset=0, bit=1
for %%f in (File1.exe File2.exe File3.exe) do (
   if exist "%userprofile%\Desktop\%%f" set /A "bitset|=bit"
   set /A "bit<<=1"
)
if %bitset% equ 0 (
   rem no exe files
) else if %bitset% lss 4 (
   rem file 3 missing
) else if %bitset% equ 4 (
   rem both file 1 and file 2 missing
)
Antonio

PAB
Posts: 57
Joined: 12 Aug 2019 13:57

Re: If one of two files exist then do something.

#13 Post by PAB » 07 Oct 2019 13:08

Thanks for the reply Antonio,

Unfortunately, that doesn't work. Every scenario produces the message no exe files.

Thanks in advance.

aGerman
Expert
Posts: 3786
Joined: 22 Jan 2010 18:01
Location: Germany

Re: If one of two files exist then do something.

#14 Post by aGerman » 07 Oct 2019 13:21

Actually Antonio's idea is pretty good. Sometimes I miss the obvious.
Without having tested yet - the reason for the issue might be that your real file names contain spaces or other special characters. Try to update the loop like that

Code: Select all

for %%f in ("File1.exe" "File2.exe" "File3.exe") do (
   if exist "%userprofile%\Desktop\%%~f" set /A "bitset|=bit"
   set /A "bit<<=1"
)
Every file name enclosed in surrounding quotes.

Steffen

PAB
Posts: 57
Joined: 12 Aug 2019 13:57

Re: If one of two files exist then do something.

#15 Post by PAB » 07 Oct 2019 13:31

Hi Steffen,

I did try that before I posted. I normally use the quotes as a matter of course anyway!
File1 & File2 are .msu files which wouldn't make a difference anyway.

Thanks in advance.

Post Reply