Storing input strings within batch script file itself

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
koko
Posts: 38
Joined: 13 Oct 2016 00:40

Storing input strings within batch script file itself

#1 Post by koko » 30 Nov 2017 00:04

Inquiring to the helpful minds here to see if there's a way of storing user input in a batch script itself, by writing to the file in a specific position.

Essentially the concept is to allow a user to store certain key settings such as program paths by dragging them into the batch script window and writing the strings to the script in a permanent manner (after being escaped for problematic characters), positioning them next to set var= lines so they become variables when running. The idea being it would be an easier method than editing the batch script in a text editor, finding how to copy the full path of a file, then adding the lines manually and making sure the file isn't saved with the incorrect encoding or needing manual escaping of certain characters.

I think I've vaguely heard of writing to batch scripts themselves before but can't remember if it was possible to place the stored strings in exact line/column positions.

Is this concept doable?

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Storing input strings within batch script file itself

#2 Post by ShadowThief » 30 Nov 2017 00:14

I mean you can always

Code: Select all

echo something >>"%~0"
but a script editing itself on the fly tends to act irrationally so I really can't recommend that you do it.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Storing input strings within batch script file itself

#3 Post by aGerman » 30 Nov 2017 02:00

As ShadowThief said you can append lines to a running script.

Code: Select all

@echo off &setlocal
REM try to get variable values from :setvar subroutine
call :setvar
REM if the values were set then you don't need to prompt the user
if defined foo if defined bar goto isdefined

REM prompt the user
:loop1
set /p "foo=Enter foo: "
if not defined foo goto loop1

:loop2
set /p "bar=Enter bar: "
if not defined bar goto loop2

REM write variable definitions to subroutine :setvar
set "me=%~f0"
setlocal EnableDelayedExpansion
>>"!me!" echo set "foo=!foo!"
>>"!me!" echo set "bar=!bar!"
endlocal

REM at this point both variables are defined
:isdefined
setlocal EnableDelayedExpansion
echo foo is !foo!
echo bar is !bar!
endlocal

pause
REM quit the main program
exit /b


REM subroutine for vartiable definitions
REM !!! make sure there is an empty line at the end of the script !!!
:setvar

It's more common to write and read values from another file though.

Steffen

koko
Posts: 38
Joined: 13 Oct 2016 00:40

Re: Storing input strings within batch script file itself

#4 Post by koko » 01 Dec 2017 12:24

aGerman wrote:
30 Nov 2017 02:00
As ShadowThief said you can append lines to a running script.

*snip*

Steffen
Thanks for the suggestions and example. Very much appreciated. Will probably use this method for now.

Was also thinking it might be possible by either splitting the original batch script into different pieces in separate files then concatenating them with the user input positioned at a non-bottom position, or by copying the original script then performing a search and replace on the desired line to be changed (making sure the changes were after the line being executed) then copying the modified output to the original script (or alternatively launching the modified copy, replacing the original's contents then launching the original again and deleting the copy).

Became stuck while experimenting with the find and replace idea using one of jeb's posts as a source since the output expands all the original variables, not just the desired string to be replaced.

Post Reply