Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
Velpreso
- Posts: 18
- Joined: 28 Dec 2012 10:03
#1
Post
by Velpreso » 29 Dec 2012 12:53
So, if i made a text file with text where is gap, for example test=this it test and i use FOR to read variable test it will read only "this" and skip "it test" do you know how to fix it ? There is script. I hope you understand me.
Code: Select all
@echo off
IF exist iamtesting.txt goto 1
echo test=this is test > iamtesting.txt
exit
:1
for /f %%a in (iamtesting.txt) do set %%a
echo Text %test%
pause > nul
Sorry for my language and spelling mistakes.
-
billrich
- Posts: 70
- Joined: 24 Apr 2012 05:36
- Location: USA
#2
Post
by billrich » 29 Dec 2012 15:20
C:\test>type read3txt.bat
@echo off
setlocal enabledelayedexpansion
type iamtesting.txt
for /f "tokens=1-4 delims= " %%j in (iamtesting.txt) do (
set Text=%%j
echo Text=!Text!
set Text=%%j %%k %%l %%m
echo Text=!Text!
endlocal
)
Output:
C:\test>read3txt.bat
this is the test
Text=this
Text=this is the test
C:\test>
Last edited by
billrich on 29 Dec 2012 18:03, edited 1 time in total.
-
Velpreso
- Posts: 18
- Joined: 28 Dec 2012 10:03
#3
Post
by Velpreso » 29 Dec 2012 15:38
Output:
test=this is testtest=this
Text=
And even if I transform your script i still can't set variable for "this is test".
-
billrich
- Posts: 70
- Joined: 24 Apr 2012 05:36
- Location: USA
#4
Post
by billrich » 29 Dec 2012 15:54
C:\test>type read2txt.bat
@echo off
type iamtesting.txt
set /p Text=<iamtesting.txt
echo Text=%Text%
)
Output:
C:\test>read2txt.bat
this is the test
Text=this is the test
C:\test>
-
Squashman
- Expert
- Posts: 4486
- Joined: 23 Dec 2011 13:59
#5
Post
by Squashman » 29 Dec 2012 15:54
If all you want to do is set the first line of a text file to a variable then all you need to do is this.
Code: Select all
C:\Users\Squashman>echo this is test>iamtesting.txt
C:\Users\Squashman>set /p test=<iamtesting.txt
C:\Users\Squashman>echo %test%
this is test
C:\Users\Squashman>
If you want to do it with a for loop you need to use the tokens or delims option so that it doesn't separate out the words after the spaces. By default the FOR /F command delimits by a space and tab.
Code: Select all
FOR /F "delims=" %%G in (iamtesting.txt) do set test=%%G
-
Velpreso
- Posts: 18
- Joined: 28 Dec 2012 10:03
#6
Post
by Velpreso » 30 Dec 2012 05:15
I don't want to read all what is in the iamtesting.txt i want to set that test=this is test and when i write echo %test% it will show me just: this is test. Type command is not good because it show all in iamtesting.txt.
If i use:
Code: Select all
FOR /F "delims=" %%G in (iamtesting.txt) do set test=%%G
it will still showing me all from iamtesting.txt
only when i use:
Code: Select all
for /f %%a in (iamtesting.txt) do set %%a
and write echo %test% will by good but it will not show me all text (just "this")
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#7
Post
by foxidrive » 30 Dec 2012 06:24
These two work to display that text.
Code: Select all
@echo off
echo test=this is test>iamtesting.txt
set /p var=<iamtesting.txt
set %var%
echo %test%
del iamtesting.txt
pause
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#8
Post
by foxidrive » 30 Dec 2012 06:26
Code: Select all
@echo off
echo test=this is test>iamtesting.txt
for /f "tokens=2 delims==" %%a in (iamtesting.txt) do echo %%a
del iamtesting.txt
pause
-
abc0502
- Posts: 1007
- Joined: 26 Oct 2011 22:38
- Location: Egypt
#9
Post
by abc0502 » 30 Dec 2012 06:27
I don't understand why it didn't work for you till now unless the real data is not in the first line !
so, this is two case one of them should work .
Do you know in which line number the data you need to find, or you just know what is the data in the line you need?
First case [ you know in which line the data exist EX: line will be line number 5 ]
Code: Select all
@Echo Off
Setlocal EnableDelayedExpansion
Set C=0
For /F "tokens=1* delims==" %%A in ('Type "iamtesting.txt"') Do (
Set /a C+=1
:: Note %%A will be all data before the equal sign and %%B will be all data after equal sign
IF "!C!" EQU "5" Set "Text=%%B"
)
Echo %Text%
Pause
This code will skip any empty lines in the file, i think it won't affect you, but i just mention it in case you might need to know about.
Second case [ what is the data in the line you need EX: data will be the first word in the line "test=" or any unique text or sentence that you can recognize that line with.]
search will be cse sensetiveCode: Select all
@Echo Off
For /F "delims=" %%A in ('FINDstr /C:"test=" "iamtesting.txt"') Do set "Text=%%A"
:: note the ~5 is to skip the first 5 characters which is test= that you don't need
Echo %Text:~5%
Pause
and bill, the out-put will be the lines the OP's need to see. "this is test"
-
Squashman
- Expert
- Posts: 4486
- Joined: 23 Dec 2011 13:59
#10
Post
by Squashman » 30 Dec 2012 12:09
I don't think the OP is giving us all the information.
Both of my examples should work based on the information they have given.
-
billrich
- Posts: 70
- Joined: 24 Apr 2012 05:36
- Location: USA
#11
Post
by billrich » 30 Dec 2012 12:35
foxidrive wrote:Code: Select all
@echo off
echo test=this is test>iamtesting.txt
for /f "tokens=2 delims==" %%a in (iamtesting.txt) do echo %%a
del iamtesting.txt
pause
works very well. How do I print the second token? Is it %%b?
-
Squashman
- Expert
- Posts: 4486
- Joined: 23 Dec 2011 13:59
#12
Post
by Squashman » 30 Dec 2012 13:12
billrich wrote:foxidrive wrote:Code: Select all
@echo off
echo test=this is test>iamtesting.txt
for /f "tokens=2 delims==" %%a in (iamtesting.txt) do echo %%a
del iamtesting.txt
pause
works very well. How do I print the second token? Is it %%b?
You are already printing the 2nd token because you specified tokens=2.
-
Velpreso
- Posts: 18
- Joined: 28 Dec 2012 10:03
#13
Post
by Velpreso » 31 Dec 2012 01:53
foxidrive wrote:These two work to display that text.
Code: Select all
@echo off
echo test=this is test>iamtesting.txt
set /p var=<iamtesting.txt
set %var%
echo %test%
del iamtesting.txt
pause
That working grate till I set test2 in iamtesting.txt
foxidrive wrote:Code: Select all
@echo off
echo test=this is test>iamtesting.txt
for /f "tokens=2 delims==" %%a in (iamtesting.txt) do echo %%a
del iamtesting.txt
pause
This one working great even as i set test 2
Thanks for your help and all posts
-
Squashman
- Expert
- Posts: 4486
- Joined: 23 Dec 2011 13:59
#14
Post
by Squashman » 31 Dec 2012 06:55
What is TEST 2?
You never said anything about a 2nd variable needed or what you needed in that variable.
What you need to do is try and explain the best you can what your EXACT input is! And I mean exact to the very letter and all of it. No more of this pseudo input we have been dealing with. Copy and past the entire input here if you have to. Show us the real input you are working with. Then show us the exact output you want!
-
billrich
- Posts: 70
- Joined: 24 Apr 2012 05:36
- Location: USA
#15
Post
by billrich » 31 Dec 2012 09:19
C:\test>type vel2.bat
Code: Select all
@echo off
echo test=this is test>iamtesting.txt
set /p var=<iamtesting.txt
set %var%
echo var=%var%
echo %test%
del iamtesting.txt
pause
Output:
C:\test>vel2.bat
var=test=this is test
this is test
Press any key to continue . . .
C:\test>
P.S. Please explain how the following works:
set %var%
---------------------------------
The OP and the Expert are the same person