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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
aGerman
Expert
Posts: 3763
Joined: 22 Jan 2010 18:01
Location: Germany

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

#16 Post by aGerman » 07 Oct 2019 13:43

That's weird. Right now I tried the code. Works nicely for me.

Steffen

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

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

#17 Post by PAB » 07 Oct 2019 14:07

Sorry, it was my fault!

I added the userprofile%\Desktop\ to the beginning of "File1.exe" "File2.exe" "File3.exe"

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"
)
Took them out and it works perfectly!

CJM
Posts: 1
Joined: 25 Oct 2019 20:34

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

#18 Post by CJM » 25 Oct 2019 20:55

The FOR command is perfect for this, and I'm a big fan of testing the presence of any attributes (using %%~a) to determine if a file exists. By doing this in a subsequent FOR ... IN (), lack of file attributes will result in a (null) FOR set and will not GOTO, so if any of the files in the original FOR list are present, the GOTO will exit further checking.

Code: Select all

@For %%F in (
		File1.txt
		Fil2.exe
		Filehere.dat

) do @For %%f in (%%~aF) do GOTO :Exist
ECHO=No files found
GOTO:EOF

:Exist
Instead of GOTO, CALL or SET could be used to instead check every file, or using the bit method previously mentioned.

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

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

#19 Post by PAB » 27 Oct 2019 05:37

Thanks for the input CJM, it is very much appreciated!

Post Reply