Two Questions

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
john924xps
Posts: 65
Joined: 08 Jun 2012 07:48

Two Questions

#1 Post by john924xps » 18 Oct 2012 07:16

Hello all! I've got two questions today...

The first one is: how do you set a variable into the data of a certain file? Such as, I have a text file with the words "Hello World" inside, and I want to set a variable named info into the data of that file, therefore resulting in...
info=Hello World
Is that possible?

Second question:
Is it possible to easily implement a variable/argument into another variable/argument? In this example, let's implement a variable into another variable. Let's say I keep pending the user for infinite variables... and then later on, I want to output them into a text file. I would do something like : %var%num%%. Where the processor first evaluates the num variable, example ONE, so it becomes %var1%. THEN it evaluates the entire expression, outputting in the data in which is being held by the variable var1.

I'm sorry if I didn't state it clearly enough, since honestly, I don't know how to explain my problem. Thanks!!!

Boombox
Posts: 80
Joined: 18 Oct 2012 05:51

Re: Two Questions

#2 Post by Boombox » 18 Oct 2012 07:27

Code: Select all

set /p mydata=<message.txt

set mydata=info=%mydata%

echo %mydata%>message.txt


This will answer your first question...
Providing your message is stored in a filed called message.txt

We create a variable called 'mydata', send the contents of message.txt to it. (Note arrow direction)

Then set the contents of the new data = to itself, with the additional data inserted before hand.

Finally send the edited variable back to the text file


Not sure what you're asking for the 2nd question?

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

Re: Two Questions

#3 Post by foxidrive » 18 Oct 2012 08:07

john924xps wrote:Second question:
Is it possible to easily implement a variable/argument into another variable/argument? In this example, let's implement a variable into another variable. Let's say I keep pending the user for infinite variables... and then later on, I want to output them into a text file. I would do something like : %var%num%%. Where the processor first evaluates the num variable, example ONE, so it becomes %var1%. THEN it evaluates the entire expression, outputting in the data in which is being held by the variable var1.

I'm sorry if I didn't state it clearly enough, since honestly, I don't know how to explain my problem. Thanks!!!



Explain what you want to do and someone will give you ideas about how to achieve it.

If you only suggest the way that you think is best then you will get a poorer set of choices to use.

john924xps
Posts: 65
Joined: 08 Jun 2012 07:48

Re: Two Questions

#4 Post by john924xps » 18 Oct 2012 08:30

Boombox: Worked great! Except, what if I had multiple lines of text in my text file? How would I set it into that variable?
Foxidrive: I'll try again later on. Gonna be at least two paragraphs long. SO hard to explain :P

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Two Questions

#5 Post by dbenham » 18 Oct 2012 08:37

I would move the 2nd redirection to the front just in case the file content ends with a number. We don't want the number to be interpreted as a file handle for the redirection.

No need to modify the value of mydata - can output the final result directly.

Finally, as a matter of style I like to use quotes in the SET /P, even though they are not needed in this case.

Code: Select all

<message.txt set  /p "mydata="
>message.txt echo info=%mydata%


With regard to 2nd question, take a look at this:

Code: Select all

@echo off
setlocal enableDelayedExpansion

::define 3 variables
set var1=A
set var2=B
set var3=C
set var

set index=1

:: Access the correct value using CALL, doubling the percents
:: for the outer variable that must be evaluated last.
:: This method is slow and not always reliable.
echo(
call echo via CALL: var%index% = %%var%index%%%

:: Access the value using delayed and normal expansion.
echo(
echo Delay+Normal expansion: var%index% = !var%index%!
:: The method fails if used within code block and index is
:: set within same block because %index% is value before block
(
  set index=2
  echo Delay+Normal expansion fail: var!index!=!var%index%!
  echo %%index%%=%index%
  echo ^^^!index^^^!=!index!
)

:: Transfer index value to FOR variable and use delayed expansion
:: This method can be used anywhere
echo(
(
  for %%N in (!index!) do echo Delayed with For variable: var%%N = !var%%N!
  set index=3
  for %%N in (!index!) do echo Delayed with For variable: var%%N = !var%%N!
)

output:

Code: Select all

var1=A
var2=B
var3=C

via CALL: var1 = A

Delay+Normal expansion: var1 = A
Delay+Normal expansion fail: var2=A
%index%=1
!index!=2

Delayed with For variable: var2 = B
Delayed with For variable: var3 = C


Dave Benham

john924xps
Posts: 65
Joined: 08 Jun 2012 07:48

Re: Two Questions

#6 Post by john924xps » 21 Oct 2012 08:22

Yes. But what if the file you want to set the variable into has multiple lines of text? How would we group together all of them and insert them into the variable? I've tried and failed... Thanks

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

Re: Two Questions

#7 Post by foxidrive » 21 Oct 2012 21:23

Explain the task. Setting a paragraph into a variable isn't a normal thing to try and do.

john924xps
Posts: 65
Joined: 08 Jun 2012 07:48

Re: Two Questions

#8 Post by john924xps » 21 Oct 2012 22:28

I'm trying to display the contents of a file into a batch window. There are two ways to do this, right? The for loop, and that command...

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

Re: Two Questions

#9 Post by foxidrive » 21 Oct 2012 22:58

Code: Select all

type "c:\folder\file.txt"
pause >nul

john924xps
Posts: 65
Joined: 08 Jun 2012 07:48

Re: Two Questions

#10 Post by john924xps » 22 Oct 2012 01:33

RIght... that command... but what if, say, I wanted to make some edits to that code through batch? In other words, is it possible to gather data from a file and place them into a file without using the TYPE command? THANKS!!

Anyways, judging by dbenham's answer on the 2nd question, I guess it's impossible to accomplish my second question without using for loops? Thanks dbenham, anyways, for the answer, really informative :DDD

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

Re: Two Questions

#11 Post by foxidrive » 22 Oct 2012 06:23

You can edit the file using SED or VBS or batch - let us know what is in the file, what you want to change and how you want it changed.

john924xps
Posts: 65
Joined: 08 Jun 2012 07:48

Re: Two Questions

#12 Post by john924xps » 22 Oct 2012 06:38

the data in the file:

Hello, my name is [name].
I was born on the [day] of [month].
I really like [thing].

I want the user to be able to set several variables, and all those variables should replace it's corresponding
unit in the text. I want to keep the text the way it is, but create the actual information in another file, since I want
to keep this template :3

Btw, HOW MANY PROGRAMMING LANGUAGES DO YOU KNOW??

Thanks

Boombox
Posts: 80
Joined: 18 Oct 2012 05:51

Re: Two Questions

#13 Post by Boombox » 22 Oct 2012 06:51

Code: Select all

@ECHO OFF
:start
echo.

echo Your first name please?
set /p fname=

echo And your surname?
set /p sname=

echo And your date of birth..?
set /p dob=

echo Any hobbies?
set /p hobby=

echo %fname% %sname%,>%fname%_%sname%.txt
echo Born: %dob%>>%fname%_%sname%.txt
echo Hobbies include: %hobby%>>%fname%_%sname%.txt
cls
echo.
for /f "tokens=*" %%g in (%fname%_%sname%.txt) do echo %%g
echo.
echo Is this information correct?
set /p option=
if %option%==y goto end
if %option%==Y goto end
goto start
:end
cls
echo.& echo Thanks!% timeout /t 2 >nul

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

Re: Two Questions

#14 Post by foxidrive » 22 Oct 2012 07:07

foxidrive wrote:Explain what you want to do and someone will give you ideas about how to achieve it.

If you only suggest the way that you think is best then you will get a poorer set of choices to use.


This still applies.

Make sure to give us actual information - without information about the task you will get guesses and options that may not suit the task at all.

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Two Questions

#15 Post by Aacini » 27 Oct 2012 19:17

the data in the file:

Hello, my name is [name].
I was born on the [day] of [month].
I really like [thing].

Code: Select all

@echo off
setlocal EnableDelayedExpansion

REM I want the user to be able to set several variables
set /P name=Name:
set /P day=Day:
set /P month=Month:
set /P thing=Thing:

REM all those variables should replace it's corresponding unit in the text,
REM but create the actual information in another file, since I want to keep this template
(for /F "delims=" %%a in (template.txt) do (
   set line=%%a
   set line=!line:[=%%!
   set line=!line:]=%%!
   call echo !line!
)) > output.txt

running the program:

Code: Select all

Name: Antonio
Day: 18
Month: September
Thing: dogs

the output:

Hello, my name is Antonio.
I was born on the 18 of September.
I really like dogs.

Post Reply