I’m writing my first batch file, and as I’ve never used Windows Command Line before, I’m running into some issues.
Here’s my scenario. I use this batch file on several images to install something. I want the batch file to check the folder that the installer SHOULD be in, and if it’s not there, I want the batch file to search the computer for the installer. After that, I want it to run said installer.
Here’s what I have:
ECHO.
ECHO Starting Foo installation
IF EXIST Install\Installer.cmd (CALL Install\Installer.cmd & GOTO NextPart) ELSE (GOTO SearchInstaller)
:SearchInstaller
SET the_path =
E: & CD\
DIR /S/B Installer.cmd > installer_location.txt
IF EXIST installer_location.txt (SET /P the_path =< installer_location.txt & GOTO FoundIt)
C: & CD\
DIR /S/B Installer.cmd > installer_location.txt
IF EXIST installer_location.txt (SET /P the_path =< installer_location.txt & GOTO FoundIt)
D: & CD\
DIR /S/B Installer.cmd > installer_location.txt
IF EXIST installer_location.txt (SET /P the_path =< installer_location.txt & GOTO FoundIt)
ECHO Installation file not found.
:FoundIt
ECHO Batch file found at%the_path%
CALL %the_path%
ECHO Finished installation & ECHO.
GOTO NextPart
:NextPart
(more stuff)
I think the problem is that it isn’t saving the path, once I use DIR to locate it. I’ve been researching for days, and my Google searches are all full of purple links. Everything I find says my syntax is right, but I know I’m doing something wrong.
I’ve tried placing an ECHO Program execution reached this point. in several spots, so I know where it does get to, at least. The problems I’m seeing are with the line that assigns the text file’s contents to the_path and in the line where I’m trying to ECHO the path, so I can see it worked.
In your
SET /Pcommands, you’ve got a space between the variable name and the equals sign. This does exactly what you’re asking it to, rather than what you wanted: it sets a variable whose name ends in a space. Get rid of the extra spaces and I think it will work.Additional:-
To avoid the problem Neil pointed out (good catch!) try this:
It turns out that if
installer.cmdisn’t found,installer_location.txtis empty andthe_pathdoesn’t get defined. If you want to be extra careful, try