1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

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

Discussion in 'DOS/Other' started by leonidis4ever, Jun 12, 2012.

Thread Status:
Not open for further replies.
Advertisement
  1. leonidis4ever

    leonidis4ever Thread Starter

    Joined:
    Jun 12, 2012
    Messages:
    8
    Hello, :)
    I have searched a lot in Internet but I couldn't find an answer about my question ,
    so I decide to ask here :rolleyes:.

    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:
    [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

    Code:
    [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
     
  2. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,731
    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.
     
  3. leonidis4ever

    leonidis4ever Thread Starter

    Joined:
    Jun 12, 2012
    Messages:
    8
    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 :D

    thanks for your time :eek:
     
  4. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,731
    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.
     
  5. leonidis4ever

    leonidis4ever Thread Starter

    Joined:
    Jun 12, 2012
    Messages:
    8
    :confused: 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

    :D I made it to look so simple but it isn't :( in real life
     
  6. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,731
    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.
     
  7. leonidis4ever

    leonidis4ever Thread Starter

    Joined:
    Jun 12, 2012
    Messages:
    8
    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
     
  8. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,731
    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.
     
  9. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,731
    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>
     
  10. leonidis4ever

    leonidis4ever Thread Starter

    Joined:
    Jun 12, 2012
    Messages:
    8
    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
     
  11. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,731
    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!
    So it looks like you need to start doing some of your own research.
     
  12. leonidis4ever

    leonidis4ever Thread Starter

    Joined:
    Jun 12, 2012
    Messages:
    8
    ok thanks , I am taking work from here

    have a nice day sir
     
  13. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,731
    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 
     
  14. leonidis4ever

    leonidis4ever Thread Starter

    Joined:
    Jun 12, 2012
    Messages:
    8
    thanks
    that was generous from you
     
  15. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,731
    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.
     
  16. Sponsor

As Seen On
As Seen On...

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.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1056838