Advertisement

There's no such thing as a stupid question, but they're the easiest to answer.
Login
Search

Advertisement

DOS/Other DOS/Other
Search Search
Search for:
Tech Support Guy > > >

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


(!)

leonidis4ever's Avatar
leonidis4ever leonidis4ever is offline
Member with 8 posts.
THREAD STARTER
 
Join Date: Jun 2012
Experience: Beginner
12-Jun-2012, 05:52 PM #1
Question batch file: reading input.txt and giving out.txt
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)

Code:
A=apple B=cat     C=fwef sdf asdetg
A=head C=ehtrghr   B=tea G=tryr   yujg
C=gert A=chair tgrgd sfsf B=bed


now I want the output to be like this

Code:
-name apple -name2 cat
-name head -name2 tea
-name chair -name2 bed

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

Last edited by leonidis4ever; 13-Jun-2012 at 08:11 AM..
Squashman's Avatar
Trusted Advisor with 19,660 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
13-Jun-2012, 06:45 AM #2
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's Avatar
leonidis4ever leonidis4ever is offline
Member with 8 posts.
THREAD STARTER
 
Join Date: Jun 2012
Experience: Beginner
13-Jun-2012, 08:08 AM #3
Quote:
Originally Posted by Squashman View Post
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's Avatar
Trusted Advisor with 19,660 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
13-Jun-2012, 09:11 AM #4
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's Avatar
leonidis4ever leonidis4ever is offline
Member with 8 posts.
THREAD STARTER
 
Join Date: Jun 2012
Experience: Beginner
13-Jun-2012, 09:32 AM #5
Quote:
Originally Posted by Squashman View Post
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's Avatar
Trusted Advisor with 19,660 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
13-Jun-2012, 04:20 PM #6
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's Avatar
leonidis4ever leonidis4ever is offline
Member with 8 posts.
THREAD STARTER
 
Join Date: Jun 2012
Experience: Beginner
13-Jun-2012, 04:40 PM #7
Quote:
Originally Posted by Squashman View Post
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.
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's Avatar
Trusted Advisor with 19,660 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
13-Jun-2012, 06:11 PM #8
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's Avatar
Trusted Advisor with 19,660 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
13-Jun-2012, 06:41 PM #9
Based on your input and output example this does what you want.
Code:
@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
Code:
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>

Last edited by Squashman; 13-Jun-2012 at 06:53 PM..
leonidis4ever's Avatar
leonidis4ever leonidis4ever is offline
Member with 8 posts.
THREAD STARTER
 
Join Date: Jun 2012
Experience: Beginner
14-Jun-2012, 02:10 AM #10
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's Avatar
Trusted Advisor with 19,660 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
14-Jun-2012, 06:31 AM #11
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!
Quote:
Originally Posted by leonidis4ever View Post
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.

Last edited by Squashman; 14-Jun-2012 at 06:37 AM..
leonidis4ever's Avatar
leonidis4ever leonidis4ever is offline
Member with 8 posts.
THREAD STARTER
 
Join Date: Jun 2012
Experience: Beginner
14-Jun-2012, 07:02 AM #12
ok thanks , I am taking work from here

have a nice day sir
Squashman's Avatar
Trusted Advisor with 19,660 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
14-Jun-2012, 11:28 PM #13
This is your last gift.
Input.txt
Code:
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
Code:
@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
Code:
-name apple -name2 cat
-name head -name2 tea
-name  -name2 tree
-name chair -name2 bed
-name Fish -name2
leonidis4ever's Avatar
leonidis4ever leonidis4ever is offline
Member with 8 posts.
THREAD STARTER
 
Join Date: Jun 2012
Experience: Beginner
15-Jun-2012, 01:34 AM #14
thanks
that was generous from you
Squashman's Avatar
Trusted Advisor with 19,660 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
15-Jun-2012, 06:39 AM #15
Quote:
Originally Posted by leonidis4ever View Post
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.
As Seen On

BBC, Reader's Digest, PC Magazine, Today Show, Money Magazine
WELCOME TO TECH SUPPORT GUY!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.


(clock)
THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


WELCOME
You Are Using: Server ID
Trusted Website Back to the Top ↑