Getting user input and storing it and reusing it in batch script
I am trying to write a batch script that GETs the user input for location and a name of the file, STOREs/SAVEs it somewhere (in this case, in those 2 text files), and goes and grabs them for future use.
This is gonna be a part of my automation batch, that executes sql files automatically. And our sqls are designed and run on different environments(in this case it is DEV), but the first part of the name is always the same. Makes sense? That's why it would be more efficient if it asked for user input the first time running the files, and for the rest of them, just knows where to go and what to grab automatically.
This is what I have so far. But, it's not fully working.
ECHO -+-+- Ready for DDL Deployment -+-+-
REM SET fileloc=%1
REM SET filenm=%2
SET /P fileloc=Enter the file directory^>
SET /P fileloc=< c:\ddlDeployment\automatic\filedir.txt
SET /P filenm=Enter the file name^>
SET /P filenm=< c:\ddlDeployment\automatic\filename.txt
REM ECHO Please enter the file directory followed by the file name:
ECHO .RUN FILE C:\DUR\DDL\%fileloc%\%filenm%_DEV.sql; >> runfile_dev.txt
As you can see, i have 2 variables where a user will enter their input: fileloc (for the location part) and filenm(where the user enters that first part of the file name. Then those inputs are stored in filedir and filename .txt files.
Then, I want to RUN the file using those use Inputs (without asking for the user to input them again) and so on....
Any help would be appreciated!
8 Months Ago
Related Article:Storing the user input in .bat
is a Legacy Languages discussion thread by super-duper that has 1 reply and was last updated 8 months ago.
There is a seldom used (I hope) technique called "the self modifying program" that, like a GOTO, when used properly can be very convenient. Here is an example of such a program written in vbScript
FILELOC = ""
FILENAM = ""
'keep the above two lines as the first two lines in this script.
set fso = CreateObject("Scripting.FileSystemObject")
'prompt for FILELOC and FILENAM if either value is not already defined
if FILELOC = "" or FILENAM = "" then
'replace the first two lines of code in this file with values
'input by the user
dim pgm: pgm = wscript.ScriptFullName
dim txt: txt = Split(fso.OpenTextFile(pgm).ReadAll,vbCrLf)
txt(0) = "FILELOC = """ & GetParam("Enter File Location: ") & """"
txt(1) = "FILENAM = """ & GetParam("Enter File Name: ") & """"
'prompt for and read a string value from the console
Private Function GetParam(prompt)
GetParam = wscript.StdIn.ReadLine
When the program runs, it checks to see if the configuration values have been defined. If not then the user is prompted to enter them. The program then reads its own source file from disk, modifies the first two lines, then writes itself back to disk. The benefit is that you only ever need the one file. To use this program, copy the code into a file with a "vbs" extension. To run it you can either
or you can set cscript.exe to the default by
cscript //nologo /'h:cscript //s
and then you can run it by typing only
To make it more robust you should do some error checking on the input values to make sure that
the entered file path exists
the entered file name exists
Just plug in whatever extra code you need to do the specific task.