Compile Batch File from a Batch File

May 12, 2010 at 09:00:00
Specs: Windows XP
So here's what I'm trying to accomplish. There are a certain series of steps me and my team have to perform when we setup a new machine in our 40k+ employee company. I'm trying to automate some of the simple tasks in behalf of my team so as to make our job a bit more automated. However, I wrote a batch file which must be ran and coded to match the file server on site which could be one of hundreds of servers. Since there's a variable there, I've added that to the batch file so when the batch file runs, it looks in config.txt for the proper server name which I've instructed my team on how to configure per site.

Well, the problem is that this bombs out when run from a UNC path as opposed to a network drive. There is a common shared network drive that we can access (the G drive in our organization), however, sometimes with a new user or during an acquisition, we perform these processes before our AD group has setup their login scripts and mapping those drives manually is counter-intuitive.

So I thought about taking the batch file I wrote and making a "config.bat" which I will post below, and my guys can be prompted to enter the server name of the location they're at and I don't have to worry about UNC paths for this file to run correctly and then it dumps the entire thing into the "install.bat" or whatever that was my original intention and can be run from the UNC path.

@echo off
echo @echo off >> INS_POST_INSTALL.txt

::Sets Up The Server Information
SET /P SVRNAME=[Enter the server name]
echo set filecontent=%SVRNAME% >> INS_POST_INSTALL.txt
::Ends Setting up the Server Information

::Begin Meat And Potatoes
echo goto menu >> INS_POST_INSTALL.txt
echo :menu >> INS_POST_INSTALL.txt
echo COLOR 07 >> INS_POST_INSTALL.txt
echo cls >> INS_POST_INSTALL.txt
echo echo. >> INS_POST_INSTALL.txt
echo echo Please choose what you are doing: >> INS_POST_INSTALL.txt
echo echo. >> INS_POST_INSTALL.txt
echo echo 1 Running Pre-CMWSUP Tasks >> INS_POST_INSTALL.txt
echo echo 2 Running Post-CMWSUP Tasks >> INS_POST_INSTALL.txt
echo echo 3 Quit >> INS_POST_INSTALL.txt
echo echo. >> INS_POST_INSTALL.txt
echo  >> INS_POST_INSTALL.txt
echo :choice >> INS_POST_INSTALL.txt
echo set /P C=[1,2,3]? >> INS_POST_INSTALL.txt
echo if "%C%"=="3" goto abort >> INS_POST_INSTALL.txt
echo if "%C%"=="2" goto post >> INS_POST_INSTALL.txt
echo if "%C%"=="1" goto pre >> INS_POST_INSTALL.txt
echo goto choice >> INS_POST_INSTALL.txt
echo  >> INS_POST_INSTALL.txt
echo :pre >> INS_POST_INSTALL.txt
echo cls >> INS_POST_INSTALL.txt
echo COLOR 17 >> INS_POST_INSTALL.txt
echo echo  +----------------------------------------------+ >> INS_POST_INSTALL.txt
echo echo.    >> INS_POST_INSTALL.txt
echo echo     Copying appropriate files now. Please wait   >> INS_POST_INSTALL.txt
echo echo.    >> INS_POST_INSTALL.txt
echo echo  +----------------------------------------------+ >> INS_POST_INSTALL.txt
echo echo. >> INS_POST_INSTALL.txt
echo copy \\%filecontent%\MyDocs\Apps\files\AdobeRdr92.EXE C:\TIVOLI\Stage >> INS_POST_INSTALL.txt
echo copy \\%filecontent%\MyDocs\Apps\files\GRC_DAT-01.EXE C:\TIVOLI\Stage >> INS_POST_INSTALL.txt
echo copy \\%filecontent%\MyDocs\Apps\files\Controls-13.exe C:\TIVOLI\Stage >> INS_POST_INSTALL.txt
echo cls >> INS_POST_INSTALL.txt
echo echo  +----------------------------------------------+ >> INS_POST_INSTALL.txt
echo echo.    >> INS_POST_INSTALL.txt
echo echo             Please Initiate CMWSUP NOW            >> INS_POST_INSTALL.txt
echo echo.    >> INS_POST_INSTALL.txt
echo echo  +----------------------------------------------+ >> INS_POST_INSTALL.txt
echo pause >> INS_POST_INSTALL.txt
echo goto menu >> INS_POST_INSTALL.txt
echo  >> INS_POST_INSTALL.txt
echo :post >> INS_POST_INSTALL.txt
echo cls >> INS_POST_INSTALL.txt
echo COLOR 17 >> INS_POST_INSTALL.txt
echo echo  +----------------------------------------------+ >> INS_POST_INSTALL.txt
echo echo.    >> INS_POST_INSTALL.txt
echo echo     Copying appropriate files now. Please wait    >> INS_POST_INSTALL.txt
echo echo.    >> INS_POST_INSTALL.txt
echo echo  +----------------------------------------------+ >> INS_POST_INSTALL.txt
echo echo. >> INS_POST_INSTALL.txt
echo mkdir c:\cbdsag\ >> INS_POST_INSTALL.txt
echo copy \\%filecontent%\SVRSHARE\Apps\files\PROFILE_GENERATOR_V2.0-01.EXE echo c:\cbdsag\ >> INS_POST_INSTALL.txt
echo copy echo \\%filecontent%\SVRSHARE\Apps\files\PROFILE_GENERATOR_V2.0_UPDATE-01.EXE echo c:\cbdsag\ >> INS_POST_INSTALL.txt
echo copy \\%filecontent%\SVRSHARE\Apps\files\CBD_ECC26-01.EXE c:\cbdsag\ >> INS_POST_INSTALL.txt
echo c:\cbdsag\SAGITTA_PROFILE_GENERATOR_V2.0-01.EXE >> INS_POST_INSTALL.txt
echo c:\cbdsag\SAGITTA_PROFILE_GENERATOR_V2.0_UPDATE-01.EXE >> INS_POST_INSTALL.txt
echo c:\cbdsag\CBD_ECC26-01.EXE >> INS_POST_INSTALL.txt
echo cls >> INS_POST_INSTALL.txt
echo COLOR 4f >> INS_POST_INSTALL.txt
echo echo  +----------------------------------------------+ >> INS_POST_INSTALL.txt
echo echo.    >> INS_POST_INSTALL.txt
echo echo    Updating the Local Group Policy. Please Wait >> INS_POST_INSTALL.txt
echo echo.    >> INS_POST_INSTALL.txt
echo echo  +----------------------------------------------+ >> INS_POST_INSTALL.txt
echo echo. >> INS_POST_INSTALL.txt
echo gpupdate >> INS_POST_INSTALL.txt
echo cls >> INS_POST_INSTALL.txt
echo COLOR 2f >> INS_POST_INSTALL.txt
echo echo  +----------------------------------------------+ >> INS_POST_INSTALL.txt
echo echo. >> INS_POST_INSTALL.txt
echo echo                      Success!                      >> INS_POST_INSTALL.txt
echo echo. >> INS_POST_INSTALL.txt
echo echo    Please Shutdown the Computer "%computername%".  >> INS_POST_INSTALL.txt
echo echo. >> INS_POST_INSTALL.txt
echo echo  +----------------------------------------------+ >> INS_POST_INSTALL.txt
echo echo. >> INS_POST_INSTALL.txt
echo pause >> INS_POST_INSTALL.txt
echo goto menu >> INS_POST_INSTALL.txt
echo  >> INS_POST_INSTALL.txt
echo :abort >> INS_POST_INSTALL.txt
echo exit >> INS_POST_INSTALL.txt

So I've taken the batch file I wrote and I'm trying to dump it into a new batch file but giving my guys the ability to dump it with the variables pre-configured. The problem is that anything inside a "%" doesn't get outputted.

Is this even possible to do? Am I doing something wrong? Anyway I can dump this file into something that works?


See More: Compile Batch File from a Batch File

Report •

#1
May 12, 2010 at 10:29:16
Question: What do you intend to gain by wrapping a batch script in another batch script?

Report •

#2
May 12, 2010 at 10:31:57
By being able to configure certain aspects of the output batch file (server name primarily via having the user input the correct server name) and not having UNC path errors by calling on an external config file to load the server name.

Report •

#3
May 12, 2010 at 10:36:53
But you already are using UNC's. Case in point:
echo copy \\%filecontent%\MyDocs\Apps\files\AdobeRdr92.exe C:\TIVOLI\Stage >> INS_POST_INSTALL.txt
Well, not really my problem, I guess.

To answer your question, you need to double up your percent signs. Every '%%' in the wrapper script will translate into '%' in the child script.


Report •

Related Solutions

#4
May 12, 2010 at 11:01:07
The issue of echoing % etc can be handled but I recommend another approach.

Make a default bat file with a placekeeper for SVRNAME.
In this exampl, XXXXXXX.


==============================
@echo off > installer.bat & setLocal enableDELAYedeXpansion

SET /P SVRNAME=[Enter the server name]

for /f "tokens=* delims= " %%a in (default) do (
set S=%%a
set S=!S:XXXXXXX=%SVRNAME%!
>> installer.bat echo.!S!
)


=====================================
Helping others achieve escape felicity

M2


Report •

#5
May 12, 2010 at 12:55:25
Razor2.3, you answered my question, thanks!

While I'm using UNC's to COPY the data, my batch file errors out when I launch the batch file via the UNC path, rather than a network drive.

Since Windows defaults batch files to the "default location", my batch file couldn't find the config.txt file I was using to pull the server name.

Thanks so much for the help!


Report •

#6
May 12, 2010 at 13:13:37
OH, that was your issue? Just start your script with this:
pushd %~f0\..


Report •

#7
May 12, 2010 at 15:58:36
You could also make your script a bit easier to read by getting rid of the >> INS_POST_INSTALL.txt on almost every line and using parenthesis instead e.g.

(
Your script lines here
...
...
)>INS_POST_INSTALL.txt


Report •

Ask Question