# Solved: Change values from .txt file



## fscldhppr (Mar 29, 2010)

If one variable in *file1.txt* matches identical variable in *file2.txt* , change that variable from *file1.txt* to the *"="* value from *text2.txt*, and save as corrected value(only)to *file3.txt* (keeping the variables in order from *file1.txt*)

Example:

*file1.txt* contains...

RED52 
GREEN66
YELLOW23
purple45

*file2.txt* contains...

purple45 = 555555
GREEN66 = 333333
RED52 = 888888
YELLOW23 = 222222

*output file*(*file3.txt*)

888888
333333
222222
555555

Thanks guys.


----------



## Squashman (Apr 4, 2003)

```
@echo off
FOR /F "Delims=" %%G IN (file1.txt) DO (
	FOR /F "Tokens=1,2 Delims==" %%H IN ('findstr /B /I "%%G" ^<file2.txt') DO >>file3.txt echo %%I
)
```


----------



## fscldhppr (Mar 29, 2010)

Thanks Squashman, sorry for the late reply.

I created a folder with file1.txt & file2.txt containing the information listed from the original post, along with the .bat file to try it out. Not too familiar with batch/script language...was just wondering if your .bat required other parameters to be entered, as-is I'm getting no output...pardon my lack of understanding.

BTW, is "The Outcaste" still around? He did some work for a MS Flight Simulator/traffic data project I had a few years back. I am somewhat getting back into the hobby after medical/other hiatus, and(when time permits)still use the batch file he created for sorting extracted PDF timetable data.


----------



## foxidrive (Oct 20, 2012)

fscldhppr said:


> I created a folder with file1.txt & file2.txt containing the information listed from the original post, along with the .bat file to try it out. Not too familiar with batch/script language...was just wondering if your .bat required other parameters to be entered, as-is I'm getting no output...pardon my lack of understanding.


FWIW the code works here with your data files.


----------



## fscldhppr (Mar 29, 2010)

OK, thanks foxidrive...I just copied Squashman's text into notepad and saved as a .bat. Clicked on the .bat and the cmd box just stays open(no file3.txt output), tried dropping file1.txt into the .bat, no output.

I figured I must not be doing something right, or simply not using it in the right context/format for which it is meant...sorry, complete novice at this.


----------



## foxidrive (Oct 20, 2012)

The problem might very well be that file2.txt does not have a trailing carriage return/line feed


----------



## Squashman (Apr 4, 2003)

List the text files in the directory.

```
C:\BatchFiles\FINDSTR>dir /b File*.txt
file1.txt
file2.txt
```
Listing the contents of file1.txt

```
C:\BatchFiles\FINDSTR>type file1.txt
RED52
GREEN66
YELLOW23
purple45
```
Listing the contents of file2.txt

```
C:\BatchFiles\FINDSTR>type file2.txt
purple45 = 555555
GREEN66 = 333333
RED52 = 888888
YELLOW23 = 222222
```
Listing the contents of the batch file

```
C:\BatchFiles\FINDSTR>type match.bat
@echo off
FOR /F "Delims=" %%G IN (file1.txt) DO (
        FOR /F "Tokens=1,2 Delims==" %%H IN ('findstr /B /I "%%G" ^<file2.txt') DO >>file3.txt echo %%I
)
```
running the batch file

```
C:\BatchFiles\FINDSTR>match.bat
```
Listing the contents of the output file named file3.txt

```
C:\BatchFiles\FINDSTR>type file3.txt
 888888
 333333
 222222
 555555
```


----------



## foxidrive (Oct 20, 2012)

I changed my post above Squashman - it's probably the findstr bug with no CR/LF on file2.txt


----------



## Squashman (Apr 4, 2003)

fscldhppr said:


> BTW, is "The Outcaste" still around? .


Unfortunately no. I miss him dearly. I wish I knew what happened to him. I hope to catch up with him some day. I hope he is ok.


----------



## Squashman (Apr 4, 2003)

foxidrive said:


> I changed my post above Squashman - it's probably the findstr bug with no CR/LF on file2.txt


Yikes! That is nasty. I just tested and the batch file just hangs!!!!


----------



## Squashman (Apr 4, 2003)

This seems to fix that problem

```
@echo off
FOR /F "Delims=" %%G IN (file1.txt) DO (
	FOR /F "Tokens=1,2 Delims==" %%H IN ('type file2.txt ^|findstr /B /I "%%G"') DO >>file3.txt echo %%I
)
```


----------



## Squashman (Apr 4, 2003)

Wow. You have been gone a long time. Two threads in 4 years.


----------



## fscldhppr (Mar 29, 2010)

...works great now, thank you!

Yeah, I've had to deal with some medical/other issues for myself and my family for quite some time, that took me away from a lot of things for a while.

The Outcaste had a lot of patience dealing with someone like me trying to explain what exactly I was trying to do with my Flight Sim project...someone also with little to no experience working with batch files/scripts/etc.... but he pretty much nailed it right off the bat(no pun), and did it so fast I couldn't believe it.

Probably wasn't much of a challenge compared to some of the things you guys do here, but I sure appreciated it.

Both of you made this novice feel comfortable asking "stupid questions" on that "txt/data" thread.

..thanks again Squashman...and thank you foxidrive.


----------

