# batch file: reading input.txt and giving out.txt



## leonidis4ever (Jun 12, 2012)

Hello, 
I have searched a lot in Internet but I couldn't find an answer about my question ,
so I decide to ask here .

I want to a batch file to edit a file.txt , lets say that I have input.txt which have information
and I want to take some information (not all of them) and put it in output.txt

description of input.txt:
1. each line have different information so I want the batch to do a loop for each line
2. I want to take some info and discard some info
3. file input has number of lines undefined could be 3 lines or 100 lines

example of input file , I want to take only A & B(in red) discard the rest in black(other words,spaces ,other symbols)



```
[COLOR=Red]A=apple B=cat[/COLOR]     C=fwef sdf asdetg
[COLOR=Red]A=head[/COLOR] C=ehtrghr   [COLOR=Red]B=tea[/COLOR] G=tryr   yujg
C=gert [COLOR=Red]A=chair[/COLOR] tgrgd sfsf [COLOR=Red]B=bed[/COLOR]
```
now I want the output to be like this


```
[SIZE=4]-name [COLOR=Red]apple[/COLOR] -name2 [COLOR=Red]cat[/COLOR]
-[/SIZE][SIZE=4]name [/SIZE][SIZE=4][COLOR=Red]head[/COLOR] [/SIZE][SIZE=4]-name2 [/SIZE][SIZE=4][COLOR=Red]tea[/COLOR]
[/SIZE][SIZE=4]-name [/SIZE][SIZE=4][COLOR=Red]chair[/COLOR] [/SIZE][SIZE=4]-name2 [/SIZE][SIZE=4][COLOR=Red]bed[/COLOR][/SIZE]
```
 
now I asked alot but they told me that its impossible to do in batch and that I need VBS file
but I dont know how to handle vbs files
also I wonder if there is a tool.exe that I can call from the batch file using commands to help me

I tried to be clear with examples
I hope to find answers here

thank you every body


----------



## Squashman (Apr 4, 2003)

Well your example makes it clear as mud but I have to assume that is not your real data. We just had a thread on another forum I belong to where a guy gave us a pretty simple input and output example but it wasn't his real data, but then when he ran the batch file it didn't work. Turned out his real data was way different then his example and he didn't realize that would be an issue. 

But, I am pretty sure all the advice you got is correct. I can't think of anyway to do this in Batch even with the simple example you gave.


----------



## leonidis4ever (Jun 12, 2012)

Squashman said:


> Well your example makes it clear as mud but I have to assume that is not your real data. We just had a thread on another forum I belong to where a guy gave us a pretty simple input and output example but it wasn't his real data, but then when he ran the batch file it didn't work. Turned out his real data was way different then his example and he didn't realize that would be an issue.
> 
> But, I am pretty sure all the advice you got is correct. I can't think of anyway to do this in Batch even with the simple example you gave.


yeah, its not my real data however its so similar to it, so I don't think it would be a big problem, sure there will be some issues but I can then work on them by my self.
the important thing is to begin with simple things and then develop them 

I am new to batch programming , so I was wondering what make the batch difficult to do ?? is it because of spaces or disorder of strings ? I am just too curious 

thanks for your time


----------



## Squashman (Apr 4, 2003)

The spaces and the order make it a big issue because you have to delimit each line into multiple variables and you would need to know which variable to access and there pretty much has to be some consistency with the number of variables in each line.


----------



## leonidis4ever (Jun 12, 2012)

Squashman said:


> The spaces and the order make it a big issue because you have to delimit each line into multiple variables and you would need to know which variable to access and there pretty much has to be some consistency with the number of variables in each line.


 there isn't a command to search for a string in windows ?? like in my example would be
find ( "A=" or "B=" ) then copy the string until the next space and put it in variable and save it

 I made it to look so simple but it isn't  in real life


----------



## Squashman (Apr 4, 2003)

What if your A or B was equal to two words?
A=Black Bird C=Blue Sea B=Window 7 D=6

You can certainly use the find or findstr command to find phrases in a text file but that will give you back the whole line of text. It is not going to tell you where the match is in the line of text.

I guess in theory you could parse the line character by character to find A= or B= but then you may run into issues if your file has any special characters when you do such a task.


----------



## leonidis4ever (Jun 12, 2012)

Squashman said:


> What if your A or B was equal to two words?
> A=Black Bird C=Blue Sea B=Window 7 D=6
> 
> You can certainly use the find or findstr command to find phrases in a text file but that will give you back the whole line of text. It is not going to tell you where the match is in the line of text.
> ...


yes sir, my A can be Black_Bird and B=Window_7 , I dont care about the names if it will help the process
still dont know how to use findstr and make it do what i want


----------



## Squashman (Apr 4, 2003)

If your A and B values are going to have multiple values assigned like in my example above I really not sure how you could accurately parse them to just pull out the values. As I told you in my previous post you will not be able to do what you want with FINDSTR. It will pull the whole line of whatever phrase you are searching for. Your data is extremely random with your values being in different orders and could have more than one word assigned to a single value which could further delimit the line.


----------



## Squashman (Apr 4, 2003)

Based on your input and output example this does what you want.

```
@echo off
IF EXIST Output.txt DEL /Q Output.txt
FOR /F "TOKENS=* DELIMS=" %%G In (Input.txt) DO CALL :LABEL1 %%G

GOTO :EOF

:LABEL1
SET _OUTPUT=
:ILoop
IF "%1"=="A" SET _OUTPUT=-name %2%_OUTPUT%
IF "%1"=="B" SET _OUTPUT=%_OUTPUT% -name2 %2
SHIFT
IF "%2"=="" (
	ECHO %_OUTPUT%>>Output.txt
	GOTO :EOF
)
GOTO :ILoop
```
Execution

```
C:\Batch\Parse>parse.bat

C:\Batch\Parse>type Output.txt
-name apple -name2 cat
-name head -name2 tea
-name chair -name2 bed

C:\Batch\Parse>
```


----------



## leonidis4ever (Jun 12, 2012)

ok it did work thank you very much , but I have other questions

1. I noticed when there are special characters in a line, for example & or | , in output 
he will ignore the line and will just pass it like they don't exist
is there a solution for this problem to just copy these characters like normal letters ?
how many special characters are they ? and can you give me a list of these characters ?

2. I noticed that in input if a line don't have both A= and B= , in output he give me *echo disabled *in that line , can you change it to give me just an empty line(echo.) instead of *echo disabled

*3. also i noticed that if one of A= or B= is messing ,so lets say the B= is missing , he will ignore B= 
well I want him to give -name with empy after it , so if input was only A=chair then I want output to be -name chair -name2 

thank you Sir


----------



## Squashman (Apr 4, 2003)

This is why I don't like writing batch files when I don't have the real data!!!!!!

And let me just go back an quote YOU!


leonidis4ever said:


> yeah, its not my real data however its so similar to it, so I don't think it would be a big problem, sure there will be some issues but I can then work on them by my self.


So it looks like you need to start doing some of your own research.


----------



## leonidis4ever (Jun 12, 2012)

ok thanks , I am taking work from here 

have a nice day sir


----------



## Squashman (Apr 4, 2003)

This is your last gift.
Input.txt

```
A=apple B=cat     C=fwef sdf asdetg
G=cattle run    C=fwef sdf asdetg
A=head C=ehtrghr   B=tea G=tryr   yujg
C=ehtrghr   B=tree G=ball   base
C=gert A=chair tgrgd sfsf B=bed
C=fruit A=Fish home run
```
Parse.bat

```
@echo off
IF EXIST Output.txt DEL /Q Output.txt
FOR /F "TOKENS=* DELIMS=" %%G In (Input.txt) DO CALL :LABEL1 %%G

GOTO :EOF

:LABEL1
SET _VARA=
SET _VARB=
:ILoop
IF "%1"=="A" SET _VARA=%2
IF "%1"=="B" SET _VARB=%2
SHIFT
IF "%2"=="" (
	IF "%_VARA%%_VARB%"=="" GOTO :EOF
	ECHO -name %_VARA% -name2 %_VARB%>>Output.txt
	GOTO :EOF
)
GOTO :ILoop
```
Output.txt

```
-name apple -name2 cat
-name head -name2 tea
-name  -name2 tree
-name chair -name2 bed
-name Fish -name2
```


----------



## leonidis4ever (Jun 12, 2012)

thanks
that was generous from you


----------



## Squashman (Apr 4, 2003)

leonidis4ever said:


> thanks
> that was generous from you


I hope you learned from this experience.
I do data processing for a living and in order to do my job on a daily basis I need accurate instructions. BLACK and WHITE. When the data changes or the results you want changes 99% of the time the programming is going to be changed.
I told you that in my very first post. Next time you ask for a script you may want to actually post the real data.


----------



## leonidis4ever (Jun 12, 2012)

Squashman said:


> I hope you learned from this experience.
> I do data processing for a living and in order to do my job on a daily basis I need accurate instructions. BLACK and WHITE. When the data changes or the results you want changes 99% of the time the programming is going to be changed.
> I told you that in my very first post. Next time you ask for a script you may want to actually post the real data.


well the problem i am new to batch, i did study in college "C" language , and I have to say that things isn't that complicated in "C" so I thought that modifying little things wouldn't be so hard , well it seems that using batch isn't that simple and isn't that clear , sure you can do simple comands but not clear as in "C"
anyway I did learn my lesson, and by the way yesterday I did modify the script to my real data I had to add some IF statements and GOTO but at the end it did work

by the way I would ask you a question (not asking you to do me a script ) , how I can read a string of xml like <name>sandy</name>, is it possible to take "sandy" ? , it isn't a complicated xml file , its just all file like this

<name>sandy</name>
25/06/2011
<age>55</age>

just tell me if is it possible ?

thanks


----------



## Squashman (Apr 4, 2003)

Reading XML is possible in batch but gets really complicated because of the redirection symbols.


----------

