Variable Question

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
leftybeaver128
Posts: 11
Joined: 24 May 2011 13:20

Variable Question

#1 Post by leftybeaver128 » 24 May 2011 15:18

Okay. So I know how to use the set /p command on a basic level of options.

For example,
===================================
echo 1
echo 2
echo 3

set /p choice=(Pick a number!)
if not '%choice%'==set choice=%choice=~0,1%
if '%choice%'=='1' goto 1
if '%choice%'=='2' goto 2
if '%choice%'=='3' goto 3
echo '%choice%' is not a valid number!

:1
echo Hello!
pause>nul

:2
echo Hello!
pause>nul

:3
echo Hello!
pause>nul


===========================================

Now the question is, how long is this variable saved for? This script I originally copied from somewhere but have become somewhat familiar with it. (I'm talking about the set /p choice thing. Not the thing above.) How do I have the user define other variables and then I can run them later.

Question #2
===============
Next question. How do I get a batch file to create, edit, and be able to read the text document?



Thanks Guys!

nitt
Posts: 218
Joined: 22 Apr 2011 02:43

Re: Variable Question

#2 Post by nitt » 24 May 2011 16:12

leftybeaver128 wrote:Okay. So I know how to use the set /p command on a basic level of options.

For example,
===================================
echo 1
echo 2
echo 3

set /p choice=(Pick a number!)
if not '%choice%'==set choice=%choice=~0,1%
if '%choice%'=='1' goto 1
if '%choice%'=='2' goto 2
if '%choice%'=='3' goto 3
echo '%choice%' is not a valid number!

:1
echo Hello!
pause>nul

:2
echo Hello!
pause>nul

:3
echo Hello!
pause>nul


===========================================

Now the question is, how long is this variable saved for? This script I originally copied from somewhere but have become somewhat familiar with it. (I'm talking about the set /p choice thing. Not the thing above.) How do I have the user define other variables and then I can run them later.

Question #2
===============
Next question. How do I get a batch file to create, edit, and be able to read the text document?



Thanks Guys!


How long is a variable stored? Until the script ends or it's set to NUL.

To create a text document, you want to use your COUT thing, which is this:

>

You can use this like:

Code: Select all

@echo off
set NLM=^


set NL=^^^%NLM%%NLM%^%NLM%%NLM%

echo I love pie!%NL%A new line is here! > toast.txt


If this looks complex, all you really need to see is:

Code: Select all

echo Hello, World! > document.txt


Then to read a file, there are a few ways to do this. One way is this:

Code: Select all

@echo off
for /f %%a in (untitled1.cpp) do echo %%a
pause


That will output all the text in "untitled.cpp". And for each line "%%a" is set to the text of the line.

Another way to get the text in a file, although this only works for a line, is using your CIN thing.

<

So like,

Code: Select all

@echo off
set /p var=< document.txt
echo %var%
pause


If you just want to show the text of the file on the screen, the easiest way to do it is to:

Code: Select all

@echo off
find /v Untitled1.cpp "345243674534567"
pause


That's kind of a cheap way, and not efficient. But quicker if that's all you need.

"How do I get a batch file to create, edit, and be able to read the text document?"

I covered creating text documents, writing them, and reading them. But editing is a different thing. If you use the method I showed you to write text documents, it will clear the text then write it. I'm sure a more advanced user here may know how to append files, but the only way I think of doing this is first reading all the text in the file, then when it clears the text when you write it in, you first write the text you've read and then the new text. So it basically just adds the text at the end.

Did I answer these right? Please correct me if I'm wrong with anything, or there are more efficient methods I skipped over.

leftybeaver128
Posts: 11
Joined: 24 May 2011 13:20

Re: Variable Question

#3 Post by leftybeaver128 » 24 May 2011 16:58

That was very helpful. But one thing I am still confused on is this. By saying "echo Hello > document.txt", does that also create the document if it doesn't yet exist?

And by "read" I meant can it search for keywords in the document and execute accordingly?

nitt
Posts: 218
Joined: 22 Apr 2011 02:43

Re: Variable Question

#4 Post by nitt » 24 May 2011 18:25

leftybeaver128 wrote:That was very helpful. But one thing I am still confused on is this. By saying "echo Hello > document.txt", does that also create the document if it doesn't yet exist?

And by "read" I meant can it search for keywords in the document and execute accordingly?


That creates the file if it doesn't exist, yes.

And what do you mean? Do you just want it to return "true" is the text in the file is found, or list the files containing the string? I'm not sure about checking if a string contains another string, without using other codes. But I'm quite sure the other one can be done also with the FIND command.

leftybeaver128
Posts: 11
Joined: 24 May 2011 13:20

Re: Variable Question

#5 Post by leftybeaver128 » 24 May 2011 18:48

Okay. So say I have a file, "passwors.txt". This file contains the "Admin" password. Can my program,
read the contents of the file,
compare it to an existing variable,
and execute accordingly?

nitt
Posts: 218
Joined: 22 Apr 2011 02:43

Re: Variable Question

#6 Post by nitt » 24 May 2011 18:52

leftybeaver128 wrote:Okay. So say I have a file, "passwors.txt". This file contains the "Admin" password. Can my program,
read the contents of the file,
compare it to an existing variable,
and execute accordingly?


Yes.

Code: Select all

@echo off
set /p password=< password.txt
set /p input=Password:

if %password% EQU %input% goto start
if %password% NEQ %input% goto failed

:start
echo Correct!
pause
goto eof

:failed
echo Incorrect!
pause
goto eof

:eof

leftybeaver128
Posts: 11
Joined: 24 May 2011 13:20

Re: Variable Question

#7 Post by leftybeaver128 » 24 May 2011 18:55

Well thats simple... Haha thank you.

nitt
Posts: 218
Joined: 22 Apr 2011 02:43

Re: Variable Question

#8 Post by nitt » 24 May 2011 18:59

leftybeaver128 wrote:Well thats simple... Haha thank you.


Your welcome! :D

Batch is very simple, and very fun! Don't mean to advertise or anything, but check this out!

leftybeaver128
Posts: 11
Joined: 24 May 2011 13:20

Re: Variable Question

#9 Post by leftybeaver128 » 24 May 2011 19:22

Thanks! :) I'll be sure to download it! But I have a question about on of your previous enteries.


@echo off
set NLM=^


set NL=^^^%NLM%%NLM%^%NLM%%NLM%

echo I love pie!%NL%A new line is here! > toast.txt

In this, what is the whole set NLM=^ and set NL stuff about? It confuses me and I would like to know more.

nitt
Posts: 218
Joined: 22 Apr 2011 02:43

Re: Variable Question

#10 Post by nitt » 24 May 2011 19:28

leftybeaver128 wrote:Thanks! :) I'll be sure to download it! But I have a question about on of your previous enteries.


@echo off
set NLM=^


set NL=^^^%NLM%%NLM%^%NLM%%NLM%

echo I love pie!%NL%A new line is here! > toast.txt

In this, what is the whole set NLM=^ and set NL stuff about? It confuses me and I would like to know more.


Lol, I'm not 100% sure, actually. I know the ^ is an escape character. Such as, since you can't write echo "<" or ">" without getting an error, you can echo "^<" or "^>" to get the same thing. It has something to do with how they are put together. I found this on the web a while ago, and I kinda remembered it. :P

There are some more advanced people here than me, maybe they can help.

jeb
Expert
Posts: 1042
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Variable Question

#11 Post by jeb » 25 May 2011 00:16

To understand the line you should know that each lines is parsed in multiple phases

In the first phase the line is read until a linefeed character and the expansions of the percent variables are done.
In the next phase the carets "^" escapes the next character,
but also a "free" linefeed stops the parsing of the rest of the line,
normally there are never "free" <LF> in this phase, but you can force them with %LF% here.
So they have to be escaped the correct way.

Code: Select all

set LF=^


rem ** Two empty lines are required

This puts a single linefeed into the LF variable.
At the end of the line the caret have a special "feature", if the character after the caret it is removed, and the next character is escaped, independent which character is found.
In this case this is the linefeed character of line 2, but now the line didn't have an end, therefore the next line is appended (line3) and the complete multi-line has an end.

Now you could use the LF-variable like

Code: Select all

echo first line%LF%next line

But this fails as the linefeed acts as an immediately stop mark in phase 2.

This can be avoid by use of

Code: Select all

echo first line^%LF%%LF%next line

Now the line is handled exactly like the creation of the LF-variable.

So you need three characters here for a successful linefeed,
but to avoid to type always this sequence you could create your NL-variable which contains these three characters.
But to create them you have to create a single "^" and to <LF> characters

Code: Select all

set NL=^^^%LF%%LF%^%LF%%LF%

This can be separated
set NL=^^^%LF%%LF%^%LF%%LF%

Btw. with delayed expasion it is dramaticaly simple to use the LF-variable

Code: Select all

echo first line!LF!next line


For more information about the phases, try a look at The Secrets Behind Batch File Interpretation
jeb

leftybeaver128
Posts: 11
Joined: 24 May 2011 13:20

Re: Variable Question

#12 Post by leftybeaver128 » 25 May 2011 04:50

Incredibly helpful. Thank you. But does using %NF% work with echo Hello!%NF%How are you!? > document.txt??
Will that work?

leftybeaver128
Posts: 11
Joined: 24 May 2011 13:20

Re: Variable Question

#13 Post by leftybeaver128 » 25 May 2011 09:47

Also one more problem. This piece of code does not set it as the variable.

Code: Select all

set /p User_Settings_1=< C:\Comp_Diag\User\User_Settings_1.txt
set /p User_Settings_2=< C:\Comp_Diag\User\User_Settings_2.txt
set /p User_Settings_3=< C:\Comp_Diag\User\User_Settings_3.txt


In User_Settings_1.txt I have the word Color. It is supposed to set this as the variable "User_Settings_1" so that I can show this option later. It doesn't show this variable...

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

Re: Variable Question

#14 Post by dbenham » 25 May 2011 12:00

One of the answers to the original questions is imprecise and needs clarification:
nitt wrote:How long is a variable stored? Until the script ends or it's set to NUL.

By default, changes made to a variable persist after the batch file terminates. The changes are not lost until the "DOS" session ends.

Most batch files alter the default behavior by including a SETLOCAL command. SETLOCAL makes a copy of all defined variables (the environment) and all subsequent changes are done to the local copy. The changes are lost and the original environoment is restored when either of the following occurs:
  • the batch script (or the batch labeled function) that issued the SETLOCAL command terminates
  • The ENDLOCAL command is executed

SETLOCAL only works within a batch file - it does not work in an interactive command prompt.

When nitt says "Until [the variable] is set to NUL", he means until the variable is undefined. A variable can be undefined by setting it to nothing, as in:

Code: Select all

set var1=
set "var2="

The syntax with the quotes is preferred because it guarantees there is no hidden space. If there is a space after the = then the variable value is set to a <space> - the variable is not undefined.

Dave Benham

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: Variable Question

#15 Post by Liviu » 20 May 2013 21:04

jeb wrote:This can be avoid by use of

Code: Select all

echo first line^%LF%%LF%next line
Now the line is handled exactly like the creation of the LF-variable.

From the category of "too obvious to mention" ;-) phase 2 is quote dependent,
and when the quote-flag is "on" the ^ caret is no longer a special character,
and ^%LF%%LF% doesn't expand as expected. For example, the 2nd run below fails.

Code: Select all

C:\tmp>echo first line^%lf%%lf%next line
first line
next line

C:\tmp>echo "first line^%lf%%lf%next line"
"first line^

What does seem to work in either case is

Code: Select all

C:\tmp>for %u in (^"^%lf%%lf%^") do @echo first line%~unext line
first line
next line

C:\tmp>for %u in (^"^%lf%%lf%^") do @echo "first line%~unext line"
"first line
next line"
i.e. the same technique used elsewhere for the equally "obvious" safe return ;-)

Liviu

Post Reply