user1376156
user1376156

Reputation: 9

Using inputs from a batch file to create a new, saved as a different name batch file

I am trying to create a batch file to input 3 pieces of data and use that data to create another batch file. Just create it, and stop. The batch maps several network drives for users that haaven't a clue as to how to do it.
I have a "master.bat" and using notepad I am using "replace" to fill in the "username" "Password" and "drive path". I thought I would try to get it down to entering the variables into the "master.bat" creating a "custom.bat" for that user.
I got a lot of help here getting to the final step. Everything is working except the final part. Now that I have all the variables as well as a template to put them in, how do I get that first batch file to create the cuctomized output as a workable file that I can send the user where all they do is run it.

Upvotes: 0

Views: 1169

Answers (2)

dbenham
dbenham

Reputation: 130819

It is simple to embed the template within your batch file. There are multiple ways to do this. One is to simply prefix each template line with :::. I chose that sequence because : is already used as a batch label and :: is frequently used as a batch comment.

Delayed expansion can be used to do your search and replace automatically!

There are just 3 special characters you need to worry about if you want to include them in your output. These special characters are probably not needed for the original question. But it is good to know how to handle them in a general sense.

An exclamation literal ! must be either escaped or substituted

A caret literal ^ can be escaped or substituted if it appears on a line with an exclamation. But it must not be escaped if there is not an exclamation on the line. Caret substitution is always safe.

Use substitution to start a line with :

@echo off
setlocal

::The following would be set by your existing script code
set drivePath=x:
set username=rumpelstiltskin
set password=gold

::This is only needed if you want to include ! literals using substitution
set "X=!"

::This is only needed if you want to include ^ literal on same line
::containing ! literal
set "C=^"

::This is only needed if you want to start a line with :
set ":=:"

::This is all that is needed to write your output
setlocal enableDelayedExpansion
>mapDrive.bat (
  for /f "tokens=* delims=:" %%A in ('findstr "^:::" "%~f0"') do @echo(%%A
)

::----------- Here begins the template -----------
:::@echo off
:::net use !drivePath! \\server\share "/user:!username!" "!password!"
:::!:!: Use substitution to start a line with a :
:::!:!: The following blank line will be preserved
:::
:::echo Exclamation literal must be escaped ^! or substituted !X!
:::echo Caret with exclamation must be escaped ^^ or substituted !C!
:::echo Caret ^ without exclamation must not be escaped

Upvotes: 1

Joey
Joey

Reputation: 354456

One way would be to use your template in file form and replace placeholders in there by your actual values:

setlocal enabledelayedexpansion
for /f %%L in (template.cmd) (
    set "Line=%%L"
    set "Line=!Line:[username]=!username!"
    ...
    >network-drives.cmd echo !Line!
)

This assumes placeholders like [username] in the template and corresponding variables defined.

However, I always get a little anxious if I use data read from a file in a batch. When I recently had to create a batch file from another I went the following route:

(
  echo @echo off
  echo net use !drivepath! \\server\share "/user:!username!" "!password!"
  echo net use !drivepath2! \\server\share2 "/user:!username!" "!password!"
) > network_drives.cmd

Care has to be taken with things like closing parentheses and several characters reserved for the syntax you may need in the generated batch file. But this approach is entirely self-contained, albeit a little harder to maintain.

Upvotes: 2

Related Questions