[SOLVED] If one of two files exist then do something.
Moderator: DosItHelp
[SOLVED] If one of two files exist then do something.
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.
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.
Re: If one of two files exist then do something.
Easiest method to work around logical OR is using a helper variable.
But you could also reverse the logic. The two IF statements in a row behave like logical AND.
Steffen
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
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
Re: If one of two files exist then do something.
Brilliant, Thanks Steffen.
Just out of interest, which of the two codes is more suitable/best to use please?
Just out of interest, which of the two codes is more suitable/best to use please?
Re: If one of two files exist then do something.
Depends on the situation. In your case the second should be okay.
Steffen
Steffen
Re: If one of two files exist then do something.
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.
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.
Re: If one of two files exist then do something.
Don't know if I understood your logic. But I guess it should be one out of these two
Steffen
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
Re: If one of two files exist then do something.
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.
Thanks in advance.
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
)
Re: If one of two files exist then do something.
You can use a bitset where you add powers of two for every found file.
Steffen
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
Re: If one of two files exist then do something.
Thanks Steffen, that works great!
Can I just trouble you to explain this piece of code please?
Thanks in advance.
Can I just trouble you to explain this piece of code please?
Code: Select all
set /a "f1orf2=bitset & 3, f3=bitset & 4"
Re: If one of two files exist then do something.
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.
Now I set the bits using bitwise OR (|-operator).
E.g. file 1 found
Say file 2 is not present, so no update of the bitset. But file 3 exists:
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.
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.
If the result is not 0 file 3 exists.
That's it.
Steffen
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
E.g. file 1 found
Code: Select all
000
| 001
_____
001
Code: Select all
001
| 100
_____
101
For file 1 and 2 I use number 3 as bitmask. Its binary expression is 011.
Code: Select all
101
& 011
_____
001
For file 3 I use number 4 as bitmask. Its binary expression is 100.
Code: Select all
101
& 100
_____
100
That's it.
Steffen
Re: If one of two files exist then do something.
Wow Steffen, thank you so much for taking the time to explain that to me, it is very much appreciated.
Re: If one of two files exist then do something.
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:
Antonio
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
)
Re: If one of two files exist then do something.
Thanks for the reply Antonio,
Unfortunately, that doesn't work. Every scenario produces the message no exe files.
Thanks in advance.
Unfortunately, that doesn't work. Every scenario produces the message no exe files.
Thanks in advance.
Re: If one of two files exist then do something.
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 thatEvery file name enclosed in surrounding quotes.
Steffen
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"
)
Steffen
Re: If one of two files exist then do something.
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.
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.