Creating .bat file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
matt2k12
Posts: 8
Joined: 13 Nov 2013 06:33

Creating .bat file

#1 Post by matt2k12 » 13 Nov 2013 07:01

Hi guys,

I'm trying to create a batch file to install a bunch of programs silently. I know there are tools/other programming languages that would get this done very efficiently, but the point of the exercise is to learn DOS commands :wink:

Basically, if a have a folder (e.g. apps) which contains folders for each of the programs (e.g. app1, app2 etc) I want to run a .bat to open folder 'app1' (and any subsequent folders) until it finds a *.exe (e.g. setup exe, but i want it to only look for the .exe not the name) and then runs (installs) it silently. Once that is done I would like it to loop back to the next folder in line and repeat the process (i.e. goto app2) until everything has been installed.
I'm looking to create this as a .bat over other methods as I am trying to learn the basic commands :)

a basic outline of the program I am trying to create:

/
change directory to current path (so it can install programs from the folder it is in)

loop over x=1 to n (bsically the folders containing the installers numbered as 1 to n)
open folder x
IF exists *.exe THEN
run *.exe /silent (running as silent so the program can run without user input)
set x=x+1 (upon succesful completion of installation go to the next folder and repeat process)
goto open folder x

ELSE goto open folder x (basically to keep opening the first folder in each sub folder till it finds a .exe)

UNTIL x>n (command to stop the process when there are no more folders to go through)

pause (simply so the user can see that it's finished and press enter)
:EXIT
//

Any help/pointers in the right direction would be greatly appreciated, if it is even possible as it stands :D

cheers folks!
Matt

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

Re: Creating .bat file

#2 Post by foxidrive » 13 Nov 2013 07:41

AIUI programs use a variety of switches to determine a silent install - so /silent isn't going to work with every program, and other switches may be needed, such as target folder, user, and what have you.

I don't think this is a one-size-fits-all task in that regard.

matt2k12
Posts: 8
Joined: 13 Nov 2013 06:33

Re: Creating .bat file

#3 Post by matt2k12 » 13 Nov 2013 08:30

The silent install is just a bonus, I don't need a fully functioning program, just the individual commands that would result in something like this when put together.

anyhow, so far I have
cd %~dp0 (to change the directory to the one the batch file is in)
then I was thinking searching for *.exe's in all its sub directories along the lines of "dir/s *.exe" and somehow running them from the list...

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

Re: Creating .bat file

#4 Post by foxidrive » 13 Nov 2013 08:36

Seeing how you want to learn the commands, add a /b to the dir command you listed and see what it returns.

matt2k12
Posts: 8
Joined: 13 Nov 2013 06:33

Re: Creating .bat file

#5 Post by matt2k12 » 22 Nov 2013 07:27

Ok, ended up hardcoding silent installation commands for each program.

I now have the batch file changing directory to current ( cd %~dp0), request administrator rights (So the UAC prompt doesnt come up for each installation, just once at the start)
with each installation running along the lines of

start /wait ProgramX\setup.exe /l* /S /v" /qn /norestart

and a pause at the end to show that its completed.

This has been working quite well, now I'm hoping someone can help me with the next bit: I would like to output a confirmation of successful/unsuccessful installation for each program and then display a message box asking the user if the want to shutdown
yes - shutdown
no - exit

I've been using the log switch, but that generates more data than necessary, all I would need is a simple yes/no. any help would be greatly appreciated :)

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

Re: Creating .bat file

#6 Post by foxidrive » 22 Nov 2013 07:31

Paste your batch file here, and we can take a look.

matt2k12
Posts: 8
Joined: 13 Nov 2013 06:33

Re: Creating .bat file

#7 Post by matt2k12 » 22 Nov 2013 07:38

-----------------------------------------------------------------------------------------------------------
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"

if '%errorlevel%' NEQ '0' (
echo Requesting administrative privileges...
goto UACPrompt
) else ( goto gotAdmin )
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
set params = %*:"=""
echo UAC.ShellExecute "%~s0", "%params%", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
exit /B
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
pushd "%CD%"
CD /D "%~dp0"

start /wait Program1\setup.exe /l* /S /v" /qn /norestart
start /wait Program1\setup.exe /l* /S /v/qn
start /wait Program1\setup.exe /l* /oemmode:1 /oem:oemresf.dll /terms:NJAA5Y
start /wait Program1\setup.exe /s

pause
-------------------------------------------------------------------------------------------------------
So first is the UAC getadmin and set directory, the /wait is for consecutive installs and the various other switches are me playing around and seeing what they do, with the pause at the end.

matt2k12
Posts: 8
Joined: 13 Nov 2013 06:33

Re: Creating .bat file

#8 Post by matt2k12 » 22 Nov 2013 07:40

with different programs, they're not all program1 :P

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

Re: Creating .bat file

#9 Post by foxidrive » 22 Nov 2013 08:12

You said that works. There is no way it could work.

You need to consider how honest you are in your dealings with people - because you are asking people to spend time on your problem, free of charge.

matt2k12
Posts: 8
Joined: 13 Nov 2013 06:33

Re: Creating .bat file

#10 Post by matt2k12 » 22 Nov 2013 08:37

Tis but a copy paste error (hopefully) the whole thing is saved in a folder as a .bat

UAC and Directory setting:


>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
if '%errorlevel%' NEQ '0' (
echo Requesting administrative privileges...
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
set params = %*:"=""
echo UAC.ShellExecute "%~s0", "%params%", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
exit /B
:gotAdmin
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
pushd "%CD%"
CD /D "%~dp0"


Installing programs:

start /wait Program1\setup.exe /l* /S /v" /qn /norestart
start /wait Program1\setup.exe /l* /S /v/qn
start /wait Program1\setup.exe /l* /oemmode:1 /oem:oemresf.dll /terms:NJAA5Y
start /wait Program1\setup.exe /s


AND now instead of pause I've added:

shutdown -s -f

to force shutdown the computer. It's all running fine so far in a windows 8 environment, dont know what could be causing an error :/
Also, I am appreciative of any help I can get, I do not expect someone to simply write my code for me, the whole point of me doing this is to learn how to do it.

matt2k12
Posts: 8
Joined: 13 Nov 2013 06:33

Re: Creating .bat file

#11 Post by matt2k12 » 22 Nov 2013 08:39

ah I missed out " :UACPrompt " and " :gotAdmin " originally so the goto function was having a hissy fit. I think.

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

Re: Creating .bat file

#12 Post by foxidrive » 22 Nov 2013 08:53

matt2k12 wrote:Also, I am appreciative of any help I can get, I do not expect someone to simply write my code for me, the whole point of me doing this is to learn how to do it.


Matt, if you don't post the actual code you are using, then people can waste their time studying your code and pointing out problems which may not exist, and not being able to see the problems which DO exist.

Being unable to see the exact code also leads to miscommunication, when you say 'part B doesn't work' and we have to work out why.

You may not feel this way, but it is also disrespectful to the people who are helping you. There is no full and frank disclosure of the actual code.

matt2k12
Posts: 8
Joined: 13 Nov 2013 06:33

Re: Creating .bat file

#13 Post by matt2k12 » 22 Nov 2013 09:23

I fully agree with you, I simply missed out a line (well 2) during the copy/paste. I resubmitted my code in all it's glory, I hope it may help someone else who is looking to do something similar.
but i can confirm it is functioning as intended.

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

Re: Creating .bat file

#14 Post by foxidrive » 22 Nov 2013 18:34

matt2k12 wrote:I resubmitted my code in all it's glory


That's my point. It's not an actual copy and paste of your code.
The people reading have no way of knowing which bits are actual code and which parts have been changed, and which other routines have been removed.

Post Reply