Variable Question
Moderator: DosItHelp
-
- Posts: 11
- Joined: 24 May 2011 13:20
Variable Question
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!
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!
Re: Variable Question
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.
-
- Posts: 11
- Joined: 24 May 2011 13:20
Re: Variable Question
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?
And by "read" I meant can it search for keywords in the document and execute accordingly?
Re: Variable Question
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.
-
- Posts: 11
- Joined: 24 May 2011 13:20
Re: Variable Question
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?
read the contents of the file,
compare it to an existing variable,
and execute accordingly?
Re: Variable Question
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
-
- Posts: 11
- Joined: 24 May 2011 13:20
Re: Variable Question
Well thats simple... Haha thank you.
Re: Variable Question
leftybeaver128 wrote:Well thats simple... Haha thank you.
Your welcome!
Batch is very simple, and very fun! Don't mean to advertise or anything, but check this out!
-
- Posts: 11
- Joined: 24 May 2011 13:20
Re: Variable Question
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.
@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.
Re: Variable Question
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.
There are some more advanced people here than me, maybe they can help.
Re: Variable Question
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.
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
But this fails as the linefeed acts as an immediately stop mark in phase 2.
This can be avoid by use of
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
This can be separated
set NL=^^^%LF%%LF%^%LF%%LF%
Btw. with delayed expasion it is dramaticaly simple to use the LF-variable
For more information about the phases, try a look at The Secrets Behind Batch File Interpretation
jeb
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
-
- Posts: 11
- Joined: 24 May 2011 13:20
Re: Variable Question
Incredibly helpful. Thank you. But does using %NF% work with echo Hello!%NF%How are you!? > document.txt??
Will that work?
Will that work?
-
- Posts: 11
- Joined: 24 May 2011 13:20
Re: Variable Question
Also one more problem. This piece of code does not set it as the variable.
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...
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...
Re: Variable Question
One of the answers to the original questions is imprecise and needs clarification:
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:
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:
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
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
Re: Variable Question
jeb wrote:This can be avoid by use ofNow the line is handled exactly like the creation of the LF-variable.Code: Select all
echo first line^%LF%%LF%next line
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"
Liviu