Use if exist to check for folder and if not change to diff 1

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Lateralus138
Posts: 11
Joined: 17 Aug 2009 13:18
Location: Decatur, Il.
Contact:

Use if exist to check for folder and if not change to diff 1

#1 Post by Lateralus138 » 18 Jul 2012 12:35

I'm not sure if I can do this, I am trying to make a batch file that checks to see if a folder exists and if it does cd to that folder, but if it does not it cd to another folder. More specifically on a 64 bit system checks for Program Files (x86) and if not goes to Program Files. I want to make a batch that my friends and I can use wget to download files without a browser being open and without having to use a bulky 3rd party software. If on a 64 bit system the folder will be located in x86 and if not in the regular program files and I want it to check which and cd to that folder then a user can input the link and download location. I can do the user input and variables I am just having trouble learning how to correctly and effectively use if exist. The code below is what I came up with and I know it's wrong, but it shows some of what I am trying to do.

Code: Select all

if exist %programfiles(x86)% (
cd %programfiles(x86)%
) else (
goto a
)
goto b
:a
cd "%programfiles%"
:b

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Use if exist to check for folder and if not change to di

#2 Post by foxidrive » 18 Jul 2012 12:48

Modify the paths to suit.

Code: Select all

@echo off
if exist "foldera\wget\wget.exe" cd /d "foldera\wget\"
if exist "folderb\wget\wget.exe" cd /d "folderb\wget\"

if not exist wget.exe (
echo wget was not found
pause
goto :EOF
)

rem do wget stuff here.

Lateralus138
Posts: 11
Joined: 17 Aug 2009 13:18
Location: Decatur, Il.
Contact:

Re: Use if exist to check for folder and if not change to di

#3 Post by Lateralus138 » 18 Jul 2012 12:57

foxidrive wrote:Modify the paths to suit.

Code: Select all

@echo off
if exist "foldera\wget\wget.exe" cd /d "foldera\wget\"
if exist "folderb\wget\wget.exe" cd /d "folderb\wget\"

if not exist wget.exe (
echo wget was not found
pause
goto :EOF
)

rem do wget stuff here.


OK, thanx so if it checks for a file and it doesn't exist it will go to the end of the file, but it is found then it skips past the goto :EOF and moves on the the next part?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Use if exist to check for folder and if not change to di

#4 Post by foxidrive » 18 Jul 2012 13:35

Lateralus138 wrote:OK, thanx so if it checks for a file and it doesn't exist it will go to the end of the file, but it is found then it skips past the goto :EOF and moves on the the next part?


Yes. When you do the checking for folders - do the 64 bit folder second. If wget exists in both locations then it will use the 64 bit one.

Post Reply